【问题标题】:WiFi state change CONNECTED/CONNECTING/DISCONNECTEDWiFi状态改变CONNECTED/CONNECTING/DISCONNECTED
【发布时间】:2015-08-28 02:33:44
【问题描述】:

我正在开发一个使用 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 状态的变量。

【问题讨论】:

    标签: android android-wifi wifimanager


    【解决方案1】:

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

    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阅读更多的状态。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-12-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-15
      相关资源
      最近更新 更多