connect wpa2 enterprise wifi connection programmatically in android

吃可爱长大的小学妹 提交于 2019-12-06 05:20:59

问题


I just tried few codes for wpa2 enterprise connection in android but nothing is connecting i want a right code to connect the right network. right now i have used this answer but i need few clarification because this answer is very old one. here i am attaching some screenshot about connection clarification. In this you can see identity and password

  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;

i have found enterprice function in wificonfiguration to set identity and password.

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

but what is the use of this one. when we have identity and password.

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

i am using BSSID because my AP have same ssid so i want to connect the right network by using BSSID


回答1:


I have solved it by the help of this 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);

but this method will work till android 7.0 in android 8.0 they have restricted many function we can not add wifi configuration manually.

three things we must know when you are trying to configure with wpa2 enterprise

  1. you must know the EAP method it can be anything but mostly they will use PEAP

2.you must know about the Phase2 method, mostly they will use MSCHAPV2

  1. Certificate this one maximum Do Not validate.


来源:https://stackoverflow.com/questions/51743186/connect-wpa2-enterprise-wifi-connection-programmatically-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!