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

connect wpa2 enterprise wifi connection programmatically in android

我刚刚在 android 中尝试了几个用于 wpa2 企业连接的代码,但没有任何连接,我想要一个正确的代码来连接正确的网络。现在我用过这个 answer but i need few clarification because this answer 是很旧的。在这里我附上了一些关于连接澄清的截图。 在这个 中你可以看到 identitypassword

  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;

我在 wificonfiguration 中找到了用于设置身份和密码的 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[=16= 连接正确的网络]

我已经在这个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 7.0 和 android 8.0 之前都有效,他们限制了很多功能,我们不能手动添加 wifi 配置。

当您尝试使用 wpa2 enterprise

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

2.you 肯定知道 Phase2 方法,他们大多会用 MSCHAPV2

  1. 证书这一项最多不验证.