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

Nir*_*mal 3 android wifi wifimanager android-wifi

我只是在 android 中尝试了一些用于 wpa2 企业连接的代码,但没有任何连接,我想要一个正确的代码来连接正确的网络。现在我已经使用了这个答案,但我需要一些澄清,因为这个答案是非常旧的。我在这里附上一些有关连接说明的屏幕截图。在这个截屏你可以看到身份密码

  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;
Run Code Online (Sandbox Code Playgroud)

我在wificonfiguration中找到了enterprice功能来设置身份和密码。

    wifiConfiguration.enterpriseConfig.setIdentity(identity);
    wifiConfiguration.enterpriseConfig.setPassword(password);
Run Code Online (Sandbox Code Playgroud)

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

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

我正在使用BSSID,因为我的 AP 有相同的 ssid,所以我想使用BSSID连接正确的网络

Nir*_*mal 6

我已经通过此链接解决了这个问题

  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);
Run Code Online (Sandbox Code Playgroud)

但是这个方法直到android 7.0都有效,在android 8.0中他们限制了很多功能我们不能手动添加wifi配置。

当您尝试使用WPA2 Enterprise进行配置时我们必须了解三件事

  1. 你必须知道EAP方法,它可以是任何方法,但大多数情况下他们会使用PEAP

2.你必须了解Phase2方法,大多数都会使用MSCHAPV2

  1. 证明这一点最多不验证