Java Code Examples for android.net.NetworkInfo#DetailedState

The following examples show how to use android.net.NetworkInfo#DetailedState . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Utils.java    From RPiCameraViewer with MIT License 6 votes vote down vote up
public static String getLocalIpAddress()
{
	String address = "";
	WifiManager manager = (WifiManager)App.getContext().getApplicationContext().getSystemService(Context.WIFI_SERVICE);
	if (manager.isWifiEnabled())
	{
		WifiInfo wifiInfo = manager.getConnectionInfo();
		if (wifiInfo != null)
		{
			NetworkInfo.DetailedState state = WifiInfo.getDetailedStateOf(wifiInfo.getSupplicantState());
			if (state == NetworkInfo.DetailedState.CONNECTED || state == NetworkInfo.DetailedState.OBTAINING_IPADDR)
			{
				int ip = wifiInfo.getIpAddress();
				address = Formatter.formatIpAddress(ip);
			}
		}
	}
	return address;
}
 
Example 2
Source File: Utils.java    From RPiCameraViewer with MIT License 6 votes vote down vote up
public static String getNetworkName()
{
	String ssid = "";
	WifiManager manager = (WifiManager)App.getContext().getApplicationContext().getSystemService(Context.WIFI_SERVICE);
	if (manager.isWifiEnabled())
	{
		WifiInfo wifiInfo = manager.getConnectionInfo();
		if (wifiInfo != null)
		{
			NetworkInfo.DetailedState state = WifiInfo.getDetailedStateOf(wifiInfo.getSupplicantState());
			if (state == NetworkInfo.DetailedState.CONNECTED || state == NetworkInfo.DetailedState.OBTAINING_IPADDR)
			{
				ssid = wifiInfo.getSSID();
				if (ssid == null) ssid = "";
				ssid = ssid.replaceAll("^\"|\"$", "");
				if (ssid.equals("<unknown ssid>"))
				{
					ssid = App.getStr(R.string.unknown_network);
				}
			}
		}
	}
	return ssid;
}
 
Example 3
Source File: JoH.java    From xDrip with GNU General Public License v3.0 6 votes vote down vote up
public static String getWifiSSID() {
    try {
        final WifiManager wifi_manager = (WifiManager) xdrip.getAppContext().getApplicationContext().getSystemService(Context.WIFI_SERVICE);
        if (wifi_manager.isWifiEnabled()) {
            final WifiInfo wifiInfo = wifi_manager.getConnectionInfo();
            if (wifiInfo != null) {
                final NetworkInfo.DetailedState wifi_state = WifiInfo.getDetailedStateOf(wifiInfo.getSupplicantState());
                if (wifi_state == NetworkInfo.DetailedState.CONNECTED
                        || wifi_state == NetworkInfo.DetailedState.OBTAINING_IPADDR
                        || wifi_state == NetworkInfo.DetailedState.CAPTIVE_PORTAL_CHECK) {
                    String ssid = wifiInfo.getSSID();
                    if (ssid.equals("<unknown ssid>")) return null; // WifiSsid.NONE;
                    if (ssid.charAt(0) == '"') ssid = ssid.substring(1);
                    if (ssid.charAt(ssid.length() - 1) == '"')
                        ssid = ssid.substring(0, ssid.length() - 1);
                    return ssid;
                }
            }
        }
    } catch (Exception e) {
        Log.e(TAG, "Got exception in getWifiSSID: " + e);
    }
    return null;
}
 
Example 4
Source File: JoH.java    From xDrip with GNU General Public License v3.0 6 votes vote down vote up
public static String getWifiSSID() {
    try {
        final WifiManager wifi_manager = (WifiManager) xdrip.getAppContext().getApplicationContext().getSystemService(Context.WIFI_SERVICE);
        if (wifi_manager.isWifiEnabled()) {
            final WifiInfo wifiInfo = wifi_manager.getConnectionInfo();
            if (wifiInfo != null) {
                final NetworkInfo.DetailedState wifi_state = WifiInfo.getDetailedStateOf(wifiInfo.getSupplicantState());
                if (wifi_state == NetworkInfo.DetailedState.CONNECTED
                        || wifi_state == NetworkInfo.DetailedState.OBTAINING_IPADDR
                        || wifi_state == NetworkInfo.DetailedState.CAPTIVE_PORTAL_CHECK) {
                    String ssid = wifiInfo.getSSID();
                    if (ssid.equals("<unknown ssid>")) return null; // WifiSsid.NONE;
                    if (ssid.charAt(0) == '"') ssid = ssid.substring(1);
                    if (ssid.charAt(ssid.length() - 1) == '"')
                        ssid = ssid.substring(0, ssid.length() - 1);
                    return ssid;
                }
            }
        }
    } catch (Exception e) {
        Log.e(TAG, "Got exception in getWifiSSID: " + e);
    }
    return null;
}
 
Example 5
Source File: JoH.java    From xDrip-plus with GNU General Public License v3.0 6 votes vote down vote up
public static String getWifiSSID() {
    try {
        final WifiManager wifi_manager = (WifiManager) xdrip.getAppContext().getApplicationContext().getSystemService(Context.WIFI_SERVICE);
        if (wifi_manager.isWifiEnabled()) {
            final WifiInfo wifiInfo = wifi_manager.getConnectionInfo();
            if (wifiInfo != null) {
                final NetworkInfo.DetailedState wifi_state = WifiInfo.getDetailedStateOf(wifiInfo.getSupplicantState());
                if (wifi_state == NetworkInfo.DetailedState.CONNECTED
                        || wifi_state == NetworkInfo.DetailedState.OBTAINING_IPADDR
                        || wifi_state == NetworkInfo.DetailedState.CAPTIVE_PORTAL_CHECK) {
                    String ssid = wifiInfo.getSSID();
                    if (ssid.equals("<unknown ssid>")) return null; // WifiSsid.NONE;
                    if (ssid.charAt(0) == '"') ssid = ssid.substring(1);
                    if (ssid.charAt(ssid.length() - 1) == '"')
                        ssid = ssid.substring(0, ssid.length() - 1);
                    return ssid;
                }
            }
        }
    } catch (Exception e) {
        Log.e(TAG, "Got exception in getWifiSSID: " + e);
    }
    return null;
}
 
Example 6
Source File: JoH.java    From xDrip-plus with GNU General Public License v3.0 6 votes vote down vote up
public static String getWifiSSID() {
    try {
        final WifiManager wifi_manager = (WifiManager) xdrip.getAppContext().getApplicationContext().getSystemService(Context.WIFI_SERVICE);
        if (wifi_manager.isWifiEnabled()) {
            final WifiInfo wifiInfo = wifi_manager.getConnectionInfo();
            if (wifiInfo != null) {
                final NetworkInfo.DetailedState wifi_state = WifiInfo.getDetailedStateOf(wifiInfo.getSupplicantState());
                if (wifi_state == NetworkInfo.DetailedState.CONNECTED
                        || wifi_state == NetworkInfo.DetailedState.OBTAINING_IPADDR
                        || wifi_state == NetworkInfo.DetailedState.CAPTIVE_PORTAL_CHECK) {
                    String ssid = wifiInfo.getSSID();
                    if (ssid.equals("<unknown ssid>")) return null; // WifiSsid.NONE;
                    if (ssid.charAt(0) == '"') ssid = ssid.substring(1);
                    if (ssid.charAt(ssid.length() - 1) == '"')
                        ssid = ssid.substring(0, ssid.length() - 1);
                    return ssid;
                }
            }
        }
    } catch (Exception e) {
        Log.e(TAG, "Got exception in getWifiSSID: " + e);
    }
    return null;
}
 
Example 7
Source File: RxNetwork.java    From Rx.Network with Apache License 2.0 6 votes vote down vote up
/**
 * Returns an observable that observes connectivity changes to the active network.
 *
 * @param context Context required to observe system broadcasts.
 * @param connectivityManager Connectivity Manager from which we'll get the active network.
 * @return NetworkInfo.DetailedState
 */
public static Observable<NetworkInfo.DetailedState> detailedStateChanges(final Context context, final ConnectivityManager connectivityManager)
{
    return ContentObservable.fromBroadcast(context, new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION))
            .map(new Func1<Intent, NetworkInfo.DetailedState>()
            {
                @Override
                public NetworkInfo.DetailedState call(Intent ignored)
                {
                    NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
                    if (networkInfo != null)
                    {
                        return networkInfo.getDetailedState();
                    }

                    return null;
                }
            })
            .distinctUntilChanged();
}
 
Example 8
Source File: WifiActivity.java    From PMCADemo with MIT License 6 votes vote down vote up
protected void networkStateChanged(NetworkInfo.DetailedState state) {
    String ssid = wifiManager.getConnectionInfo().getSSID();
    switch (state) {
        case CONNECTING:
            if (ssid != null)
                log(ssid + ": Connecting");
            break;
        case AUTHENTICATING:
            log(ssid + ": Authenticating");
            break;
        case OBTAINING_IPADDR:
            log(ssid + ": Obtaining IP");
            break;
        case CONNECTED:
            wifiConnected();
            break;
        case DISCONNECTED:
            log("Disconnected");
            break;
        case FAILED:
            log("Connection failed");
            break;
    }
}
 
Example 9
Source File: WiFiUtil.java    From DeviceConnect-Android with MIT License 6 votes vote down vote up
/**
 * 現在のSSIDを取得する.
 * 
 * @param context コンテキストオブジェクト
 * @return SSID文字列。WiFiでは無い場合はnullを返す。
 */
public static String getCurrentSSID(final Context context) {

    String ssid = null;

    if (isOnWiFi(context)) {
        WifiManager manager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
        if (manager.isWifiEnabled()) {
            WifiInfo wifiInfo = manager.getConnectionInfo();
            if (wifiInfo != null) {
                NetworkInfo.DetailedState state = WifiInfo.getDetailedStateOf(wifiInfo.getSupplicantState());
                if (state == NetworkInfo.DetailedState.CONNECTED || state == NetworkInfo.DetailedState.OBTAINING_IPADDR) {
                    // ダブルクォーテーションを含んでいるので外す
                    ssid = wifiInfo.getSSID().replaceAll("\"", "");
                }
            }
        }
    }

    return ssid;
}
 
Example 10
Source File: UtilityServiceTest.java    From openwebnet-android with MIT License 6 votes vote down vote up
private void toggleNetwork(int networkType, NetworkInfo.DetailedState detailedState) {
    NetworkInfo networkInfo = null;
    switch (detailedState) {
        case CONNECTED:
            networkInfo = ShadowNetworkInfo.newInstance(NetworkInfo.DetailedState.CONNECTED, networkType, 0, true, true);
            break;
        case DISCONNECTED:
            networkInfo = ShadowNetworkInfo.newInstance(NetworkInfo.DetailedState.DISCONNECTED, networkType, 0, true, false);
            break;
    }

    ConnectivityManager cmMock = (ConnectivityManager) RuntimeEnvironment.application.getSystemService(Context.CONNECTIVITY_SERVICE);
    ShadowConnectivityManager shadowConnectivityManager = shadowOf(cmMock);

    shadowConnectivityManager.setNetworkInfo(networkType, networkInfo);
    shadowConnectivityManager.setActiveNetworkInfo(shadowConnectivityManager.getNetworkInfo(networkType));
}
 
Example 11
Source File: WifiWizard2.java    From WifiWizard2 with Apache License 2.0 5 votes vote down vote up
@Override
protected String[] doInBackground(Object... params) {
  this.callbackContext = (CallbackContext) params[0];
  int networkIdToConnect = (Integer) params[1];

  final int TIMES_TO_RETRY = 15;
  for (int i = 0; i < TIMES_TO_RETRY; i++) {

    WifiInfo info = wifiManager.getConnectionInfo();
    NetworkInfo.DetailedState connectionState = info
        .getDetailedStateOf(info.getSupplicantState());

    boolean isConnected =
        // need to ensure we're on correct network because sometimes this code is
        // reached before the initial network has disconnected
        info.getNetworkId() == networkIdToConnect && (
            connectionState == NetworkInfo.DetailedState.CONNECTED ||
                // Android seems to sometimes get stuck in OBTAINING_IPADDR after it has received one
                (connectionState == NetworkInfo.DetailedState.OBTAINING_IPADDR
                    && info.getIpAddress() != 0)
        );

    if (isConnected) {
      return new String[]{ null, "NETWORK_CONNECTION_COMPLETED" };
    }

    Log.d(TAG, "WifiWizard: Got " + connectionState.name() + " on " + (i + 1) + " out of " + TIMES_TO_RETRY);
    final int ONE_SECOND = 1000;

    try {
      Thread.sleep(ONE_SECOND);
    } catch (InterruptedException e) {
      Log.e(TAG, e.getMessage());
      return new String[]{ "INTERRUPT_EXCEPT_WHILE_CONNECTING", null };
    }
  }
  Log.d(TAG, "WifiWizard: Network failed to finish connecting within the timeout");
  return new String[]{ "CONNECT_FAILED_TIMEOUT", null };
}
 
Example 12
Source File: ShadowNetworkInfo.java    From JobSchedulerCompat with MIT License 5 votes vote down vote up
public static NetworkInfo newInstance(
        NetworkInfo.DetailedState detailedState, int type, int subType, boolean isAvailable, boolean isConnected,
        boolean isRoaming) {
    NetworkInfo networkInfo = org.robolectric.shadows.ShadowNetworkInfo.newInstance(
            detailedState, type, subType, isAvailable,
            isConnected ? NetworkInfo.State.CONNECTED : NetworkInfo.State.DISCONNECTED);
    ShadowNetworkInfo info = (ShadowNetworkInfo) shadowOf(networkInfo);
    info.setRoaming(isRoaming);
    return networkInfo;
}
 
Example 13
Source File: DeviceTestUtils.java    From JobSchedulerCompat with MIT License 5 votes vote down vote up
@SuppressWarnings("deprecation")
public static void setNetworkInfo(Context context, boolean isConnected, boolean isRoaming, boolean isWifi) {
    ConnectivityManager manager = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo.DetailedState detailedState =
            isConnected ? NetworkInfo.DetailedState.CONNECTED : NetworkInfo.DetailedState.DISCONNECTED;
    int type = isConnected ? (isWifi ? ConnectivityManager.TYPE_WIFI : ConnectivityManager.TYPE_MOBILE) : -1;
    NetworkInfo networkInfo =
            ShadowNetworkInfo.newInstance(detailedState, type, 0, isConnected, isConnected, isRoaming);
    shadowOf(manager).setActiveNetworkInfo(networkInfo);
}
 
Example 14
Source File: WifiSetupActivity.java    From STGUploader with MIT License 5 votes vote down vote up
private void updateWifiSwitch() {
    boolean wifiEnabled = isWifiEnabled();
    String summary;
    if (wifiEnabled) {
        NetworkInfo networkInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
        WifiInfo wifiInfo = wifiManager.getConnectionInfo();
        if (networkInfo.isConnected()) {
            summary = res.getString(R.string.connectionStateConnectedTo) + " " + wifiInfo.getSSID();
            startLoginActivity();
        } else {
            NetworkInfo.DetailedState state = WifiInfo.getDetailedStateOf(wifiInfo.getSupplicantState());
            switch (state) {
                case SCANNING:
                    summary = res.getString(R.string.connectionStateWifiScanning);
                    break;
                case AUTHENTICATING:
                case CONNECTING:
                case OBTAINING_IPADDR:
                    summary = res.getString(R.string.connectionStateConnecting);
                    break;
                default:
                    summary = res.getString(R.string.connectionStateWifiEnabled);
            }
        }
    } else {
        summary = res.getString(R.string.connectionStateWifiDisabled);
    }
    textViewWifiState.setText(summary);
}
 
Example 15
Source File: ConnectivityHelper.java    From libcommon with Apache License 2.0 5 votes vote down vote up
@SuppressLint("NewApi")
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
private static boolean isNetworkReachable(
	@NonNull final NetworkCapabilities capabilities,
	@NonNull final NetworkInfo info) {

	if (DEBUG) Log.v(TAG, "isNetworkReachable:capabilities=" + capabilities);
	if (DEBUG) Log.v(TAG, "isNetworkReachable:info=" + info);
	final NetworkInfo.DetailedState state = info.getDetailedState();
	final boolean isConnectedOrConnecting
		= (state == NetworkInfo.DetailedState.CONNECTED)
			|| (state == NetworkInfo.DetailedState.CONNECTING);
	final boolean hasCapability;
	if (BuildCheck.isAPI28()) {
		hasCapability = capabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)	// API>=21
			&& capabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_VALIDATED)			// API>=23
			&& (capabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_NOT_SUSPENDED)	// API>=28
				|| capabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_FOREGROUND));	// API>=28
	} else if (BuildCheck.isAPI23()) {
		hasCapability = capabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)	// API>=21
			&& capabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_VALIDATED);		// API>=23
	} else {
		hasCapability = capabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET);// API>=21
	}
	if (DEBUG) Log.v(TAG, "isNetworkReachable:isConnectedOrConnecting="
		+ isConnectedOrConnecting + ",hasCapability=" + hasCapability
		+ ",NOT_SUSPENDED=" + capabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_NOT_SUSPENDED)
		+ ",FOREGROUND=" + capabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_FOREGROUND));
	return isConnectedOrConnecting && hasCapability;
}
 
Example 16
Source File: JacksonActivity.java    From My-MVP with Apache License 2.0 5 votes vote down vote up
@Nullable
    private String getWifiSSID() {

        WifiManager wifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);


        if (wifiManager != null && wifiManager.isWifiEnabled()) {
            WifiInfo wifiInfo = wifiManager.getConnectionInfo();
            if (wifiInfo != null) {
                NetworkInfo.DetailedState state = WifiInfo.getDetailedStateOf(wifiInfo.getSupplicantState());
                if (state == NetworkInfo.DetailedState.CONNECTED || state == NetworkInfo.DetailedState.OBTAINING_IPADDR) {
                    return wifiInfo.getSSID();
                }
            }
        }

//        Optional.ofNullable(wifiManager).ifPresent((WifiManager wifiManager1) -> {
//            if (wifiManager1.isWifiEnabled()) {
//                WifiInfo wifiInfo = wifiManager1.getConnectionInfo();
//                Optional.ofNullable(wifiInfo).ifPresent((Consumer<WifiInfo>) (WifiInfo wifiInfo1) -> {
//                    NetworkInfo.DetailedState detailedState = WifiInfo.getDetailedStateOf(wifiInfo1.getSupplicantState());
//                    if (detailedState == NetworkInfo.DetailedState.CONNECTED || detailedState == NetworkInfo.DetailedState.OBTAINING_IPADDR) {
//
//                        return wifiInfo1.getSSID();
//                    }
//                });
//            }
//
//
//        });
        return null;
    }