WiFi 状态变化 CONNECTED/CONNECTING/DISCONNECTED

WiFi state change CONNECTED/CONNECTING/DISCONNECTED

我正在开发一个使用 WiFi 的应用程序。我使用下面的代码连接到 WiFi 网络。我想要的是,当 WiFi 正在连接时,设置状态为连接,当它连接时,将其设置为连接,对于断开连接也是如此。状态将保存在一个 Class 中,它保留在整个应用程序中使用的变量。

那么如果connecting/connected/disconnected如何将其保存在变量中呢?

连接代码:

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 应该是保持当前 WiFi 状态的变量。

好的,我是这样做的:添加一个监听 NETWORK_STATE_CHANGED_ACTION 的广播接收器,当你捕捉到这个动作时,你可以像我在下面的代码片段中所做的那样设置状态。

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";
    }
}

您可以在这篇文章中阅读更多状态 NetworkInfo.DetailedState