Search code examples
androidandroid-wifiwifimanager

WiFi state change CONNECTED/CONNECTING/DISCONNECTED


I am working on an app that uses WiFi. I use the code bellow to connect to a WiFi network. What i want is that when the WiFi is Connecting, to set state Connecting, when it is Connected, to set it Connected and the same for Disconnected. The state will be saved in a Class which keeps variables used in the entire app.

So how to i get if is connecting/connected/disconnected to save it in variables?

Connect Code:

public boolean ConnectToWiFi(String SSID, String BSSID, String Security, String Password, WifiManager Manager) {

        GlobalActivity.WiFiConnectionStatus = GlobalActivity.STATE_CONNECTING;

        String securityType = Security;

        WifiConfiguration conf = new WifiConfiguration();
        conf.SSID = "\"" + SSID + "\"";
        conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);

        Manager.addNetwork(conf);
        conf.BSSID = BSSID;
        Manager.updateNetwork(conf);
        Manager.saveConfiguration();

        if (GlobalActivity.SECURITY_WEP.equalsIgnoreCase(securityType)) {
            conf.wepKeys[0] = Password;
            conf.wepTxKeyIndex = 0;
            conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
            conf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);
        } else if (GlobalActivity.SECURITY_NONE.equalsIgnoreCase(securityType)) {
            conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
        } else if (GlobalActivity.SECURITY_WPA.equalsIgnoreCase(securityType)
                || GlobalActivity.SECURITY_WPA2.equalsIgnoreCase(securityType)
                || GlobalActivity.SECURITY_WPA_WPA2.equalsIgnoreCase(securityType)) {
            conf.preSharedKey = GlobalActivity.BACKSLASH + Password + GlobalActivity.BACKSLASH;
            conf.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
            conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
            conf.status = WifiConfiguration.Status.ENABLED;
            conf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
            conf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
            conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
            conf.allowedPairwiseCiphers
                    .set(WifiConfiguration.PairwiseCipher.TKIP);
            conf.allowedPairwiseCiphers
                    .set(WifiConfiguration.PairwiseCipher.CCMP);
            conf.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
            conf.allowedProtocols.set(WifiConfiguration.Protocol.WPA);

            String wlanAdditionalSecurity = "";

            if (GlobalActivity.ADDITIONAL_SECURITY_TKIP
                    .equalsIgnoreCase(wlanAdditionalSecurity)) {
                conf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
                conf.allowedPairwiseCiphers
                        .set(WifiConfiguration.PairwiseCipher.TKIP);
            } else if (GlobalActivity.ADDITIONAL_SECURITY_AES
                    .equalsIgnoreCase(wlanAdditionalSecurity)) {
                conf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
                conf.allowedPairwiseCiphers
                        .set(WifiConfiguration.PairwiseCipher.CCMP);
            } else if (GlobalActivity.ADDITIONAL_SECURITY_WEP
                    .equalsIgnoreCase(wlanAdditionalSecurity)) {
                conf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP104);
                conf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);
            } else if (GlobalActivity.ADDITIONAL_SECURITY_NONE
                    .equalsIgnoreCase(wlanAdditionalSecurity)) {
                conf.allowedPairwiseCiphers
                        .set(WifiConfiguration.PairwiseCipher.NONE);
            }

            for (WifiConfiguration wifiConfiguration : Manager.getConfiguredNetworks()) {

                if (wifiConfiguration.SSID.equals("\"" + SSID + "\"")) {

                    Manager.disconnect();
                    wifiConfiguration.BSSID = BSSID;
                    Manager.updateNetwork(wifiConfiguration);
                    Manager.enableNetwork(wifiConfiguration.networkId, true);


                    int res = Manager.addNetwork(conf);
                    Manager.disconnect();
                    Manager.reconnect();
                    if (true) {
                        Manager.enableNetwork(res, true);
                        Manager.saveConfiguration();
                        Manager.setWifiEnabled(true);

                        GlobalActivity.WiFiConnectionStatus = GlobalActivity.STATE_CONNECTED;

                        return true;
                    }

                    else{
                        GlobalActivity.WiFiConnectionStatus = GlobalActivity.STATE_NONE;
                    }

                }

            }
        }

        return false;
    }

GlobalActivity.WiFiConnectionStatus should be the variable which will keep the current WiFi Status.


Solution

  • Okay, this is how I do it: Add a broadcast receiver that listens for NETWORK_STATE_CHANGED_ACTION and when you catch the action, you can set the state like I have done in the following snippet of code.

    if(WifiManager.NETWORK_STATE_CHANGED_ACTION.equals(action))
    {
        NetworkInfo netInfo = intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO);
        Log.d(TAG,"Network state changed action" + netInfo.getDetailedState().name());
        if(netInfo.getDetailedState().equals(NetworkInfo.DetailedState.CONNECTED))
        {
            _state = "Connected";
        }
        else if(netInfo.getDetailedState().equals(NetworkInfo.DetailedState.CONNECTING))
        {
            _state = "Connecting";
        }
        else if(netInfo.getDetailedState().equals(NetworkInfo.DetailedState.DISCONNECTED))
        {
            _state = "Disconnected";
        }
    }
    

    You can read about more states in this article NetworkInfo.DetailedState.