在Android中以编程方式连接wpa2企业wifi连接

时间:2018-08-08 09:30:08

标签: android wifi android-wifi wifimanager wificonfiguration

我只是在Android中尝试了几个用于wpa2企业连接的代码,但是没有任何连接,我想要一个正确的代码来连接正确的网络。目前,我已经使用过此answer,但由于该answer很老,我需要澄清。在这里,我附上一些有关连接澄清的屏幕截图。 在此screenshot中,您可以看到身份密码

  WifiConfiguration wifiConfiguration = new WifiConfiguration();
        wifiConfiguration.SSID = "\"" + networkSSID + "\"";
        wifiConfiguration.BSSID = Bssid;
        wifiConfiguration.hiddenSSID = true;
        wifiConfiguration.status = WifiConfiguration.Status.DISABLED;
        wifiConfiguration.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_EAP);
        wifiConfiguration.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.IEEE8021X);
        wifiConfiguration.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
        wifiConfiguration.allowedProtocols.set(WifiConfiguration.Protocol.WPA);
        wifiConfiguration.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.SHARED);
        wifiConfiguration.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
        wifiConfiguration.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
        wifiConfiguration.enterpriseConfig.setIdentity(identity);
        wifiConfiguration.enterpriseConfig.setPassword(password);

        wifiConfiguration.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
        wifiConfiguration.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);

       if (networkPasskey.matches("^[0-9a-fA-F]+$")) {
            wifiConfiguration.wepKeys[0] = networkPasskey;
        } else {
            wifiConfiguration.wepKeys[0] = "\"".concat(networkPasskey).concat("\"");
        }
        wifiConfiguration.wepTxKeyIndex = 0;

我在wifi配置中发现了enterprice功能,用于设置身份和密码。

    wifiConfiguration.enterpriseConfig.setIdentity(identity);
    wifiConfiguration.enterpriseConfig.setPassword(password);

但是这个有什么用。当我们拥有身份和密码时。

if (networkPasskey.matches("^[0-9a-fA-F]+$")) {
        wifiConfiguration.wepKeys[0] = networkPasskey;
    } else {
        wifiConfiguration.wepKeys[0] = "\"".concat(networkPasskey).concat("\"");
    }
    wifiConfiguration.wepTxKeyIndex = 0;

我正在使用 BSSID ,因为我的AP具有相同的ssid,所以我想使用 BSSID

连接正确的网络

1 个答案:

答案 0 :(得分:3)

借助link

的帮助,我解决了它
  WifiConfiguration config = new WifiConfiguration();
    config.SSID = "\"" + networkSSID + "\"";
    config.BSSID = Bssid;
    config.priority = 1;
 String networkIdentity = "";
         networkPasskey = "";
        config.status = WifiConfiguration.Status.ENABLED;
        WifiEnterpriseConfig enterpriseConfig = new WifiEnterpriseConfig();
        config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_EAP);
        config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.IEEE8021X);
        enterpriseConfig.setIdentity(networkIdentity);
        enterpriseConfig.setPassword(networkPasskey);
        enterpriseConfig.setEapMethod(WifiEnterpriseConfig.Eap.PEAP);
        enterpriseConfig.setPhase2Method(WifiEnterpriseConfig.Phase2.MSCHAPV2);
        config.enterpriseConfig = enterpriseConfig;
   WifiManager myWifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
    int id = myWifiManager.addNetwork(config);
    Log.d("addNetwork", "# addNetwork returned " + id);

    myWifiManager.enableNetwork(id, true);

但是此方法将一直有效,直到android 8.0中的android 7.0限制了许多功能,我们无法手动添加wifi配置。

当您尝试使用 wpa2企业版

进行配置时,我们必须知道的三件事
  1. 您必须知道 EAP 方法,可以使用任何方法,但是大多数情况下,他们将使用 PEAP

2。您必须了解 Phase2 方法,大多数情况下他们会使用 MSCHAPV2

  1. 证书这一最大值不验证