Java Code Examples for android.net.wifi.WifiInfo#getDetailedStateOf()

The following examples show how to use android.net.wifi.WifiInfo#getDetailedStateOf() . 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: 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 8
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 9
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 10
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;
    }
 
Example 11
Source File: BaseActivity.java    From OpenMemories-AppStore with MIT License 5 votes vote down vote up
public WifiState getWifiState() {
    if (Environment.isEmulator()) {
        return WifiState.CONNECTED;
    } else {
        switch (wifiManager.getWifiState()) {
            case WifiManager.WIFI_STATE_ENABLED:
                if (connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI).isConnected()) {
                    return WifiState.CONNECTED;
                } else {
                    switch (WifiInfo.getDetailedStateOf(wifiManager.getConnectionInfo().getSupplicantState())) {
                        case SCANNING:
                            return WifiState.SCANNING;
                        case AUTHENTICATING:
                        case CONNECTING:
                        case OBTAINING_IPADDR:
                            return WifiState.CONNECTING;
                        default:
                            return WifiState.ENABLED;
                    }
                }
            case WifiManager.WIFI_STATE_ENABLING:
                return WifiState.ENABLING;
            default:
                return WifiState.DISABLED;
        }
    }
}
 
Example 12
Source File: SipService.java    From CSipSimple with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Ask to take the control of the wifi and the partial wake lock if
 * configured
 */
private synchronized void acquireResources() {
	if(holdResources) {
		return;
	}
	
	// Add a wake lock for CPU if necessary
	if (prefsWrapper.getPreferenceBooleanValue(SipConfigManager.USE_PARTIAL_WAKE_LOCK)) {
		PowerManager pman = (PowerManager) getSystemService(Context.POWER_SERVICE);
		if (wakeLock == null) {
			wakeLock = pman.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "com.csipsimple.SipService");
			wakeLock.setReferenceCounted(false);
		}
		// Extra check if set reference counted is false ???
		if (!wakeLock.isHeld()) {
			wakeLock.acquire();
		}
	}

	// Add a lock for WIFI if necessary
	WifiManager wman = (WifiManager) getSystemService(Context.WIFI_SERVICE);
	if (wifiLock == null) {
		int mode = WifiManager.WIFI_MODE_FULL;
		if(Compatibility.isCompatible(9) && prefsWrapper.getPreferenceBooleanValue(SipConfigManager.LOCK_WIFI_PERFS)) {
			mode = 0x3; // WIFI_MODE_FULL_HIGH_PERF 
		}
		wifiLock = wman.createWifiLock(mode, "com.csipsimple.SipService");
		wifiLock.setReferenceCounted(false);
	}
	if (prefsWrapper.getPreferenceBooleanValue(SipConfigManager.LOCK_WIFI) && !wifiLock.isHeld()) {
		WifiInfo winfo = wman.getConnectionInfo();
		if (winfo != null) {
			DetailedState dstate = WifiInfo.getDetailedStateOf(winfo.getSupplicantState());
			// We assume that if obtaining ip addr, we are almost connected
			// so can keep wifi lock
			if (dstate == DetailedState.OBTAINING_IPADDR || dstate == DetailedState.CONNECTED) {
				if (!wifiLock.isHeld()) {
					wifiLock.acquire();
				}
			}
		}
	}
	holdResources = true;
}
 
Example 13
Source File: InformationCollector.java    From open-rmbt with Apache License 2.0 4 votes vote down vote up
private void getWiFiInfo()
    {
        initNetwork();
        if (wifiManager != null)
        {
            final WifiInfo wifiInfo = wifiManager.getConnectionInfo();
            fullInfo.setProperty("WIFI_SSID",
            String.valueOf(Helperfunctions.removeQuotationsInCurrentSSIDForJellyBean(wifiInfo.getSSID())));
            		
            /*
             * fullInfo.setProperty("WIFI_LINKSPEED",
             * String.valueOf(wifiInfo.getLinkSpeed()));
             */
            fullInfo.setProperty("WIFI_BSSID", String.valueOf(wifiInfo.getBSSID()));
            fullInfo.setProperty("WIFI_NETWORK_ID", String.valueOf(wifiInfo.getNetworkId()));
            /*
             * fullInfo.setProperty("WIFI_RSSI",
             * String.valueOf(wifiInfo.getRssi()));
             */
            final SupplicantState wifiState = wifiInfo.getSupplicantState();
            fullInfo.setProperty("WIFI_SUPPLICANT_STATE", String.valueOf(wifiState.name()));
            final DetailedState wifiDetail = WifiInfo.getDetailedStateOf(wifiState);
            fullInfo.setProperty("WIFI_SUPPLICANT_STATE_DETAIL", String.valueOf(wifiDetail.name()));
            
            if (getNetwork() == NETWORK_WIFI)
            {
                
                final int rssi = wifiInfo.getRssi();
                if (rssi != -1 && rssi >= ACCEPT_WIFI_RSSI_MIN)
                {
                    int linkSpeed = wifiInfo.getLinkSpeed();
                    if (linkSpeed < 0) {
                        linkSpeed = 0;
                    }
                    
                    final SignalItem signalItem = SignalItem.getWifiSignalItem(linkSpeed, rssi);
                    if (this.collectInformation) {
                        signals.add(signalItem);	
                    }
                    lastSignalItem.set(signalItem);
                    signal.set(rssi);
                    signalType.set(SINGAL_TYPE_WLAN);
//                    Log.i(DEBUG_TAG, "Signals1: " + signals.toString());
                }
            }
        }
    }