【问题标题】:connect wpa2 enterprise wifi connection programmatically in android在android中以编程方式连接wpa2企业wifi连接
【发布时间】:2019-01-15 12:17:36
【问题描述】:

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

  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

连接正确的网络

【问题讨论】:

    标签: android android-wifi wifimanager wifi


    【解决方案1】:

    我在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. 证书这一最大值不验证

    【讨论】:

      猜你喜欢
      • 2018-08-18
      • 1970-01-01
      • 1970-01-01
      • 2017-06-25
      • 1970-01-01
      • 1970-01-01
      • 2022-08-23
      • 2013-11-01
      • 1970-01-01
      相关资源
      最近更新 更多