WiFi状态更改已连接/已连接/已断开连接

时间:2015-08-25 11:49:11

标签: android android-wifi wifimanager

我正在开发一款使用WiFi的应用。我使用下面的代码连接到WiFi网络。我想要的是,当WiFi是连接时,设置状态连接,当它是连接时,设置它已连接,并且对于断开连接。状态将保存在一个类中,该类保留整个应用程序中使用的变量。

那么如果连接/连接/断开连接以保存在变量中我该怎么办?

连接代码:

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状态的变量。

1 个答案:

答案 0 :(得分:1)

好的,我就是这样做的:添加一个侦听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中了解更多状态。

相关问题