为Android

时间:2015-11-24 10:24:07

标签: java android android-wifi wifimanager

我尝试为单SIM设备配置EAP-SIM并成功连接。现在我想在双SIM卡设备上配置特定的SIM卡。

我的观察是在单SIM设备中配置EAP时,没有规定SIM身份。那么如何使用特定的SIM配置EAP?

为单SIM设备配置EAP-SIM:

    WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
    WifiEnterpriseConfig enterpriseConfig = new WifiEnterpriseConfig();
    WifiConfiguration wifiConfig = new WifiConfiguration();
    wifiConfig.SSID = "\"" + ssid + "\"";
    wifiConfig.allowedKeyManagement.set(KeyMgmt.WPA_EAP);
    wifiConfig.allowedKeyManagement.set(KeyMgmt.IEEE8021X);
    enterpriseConfig.setEapMethod(WifiEnterpriseConfig.Eap.SIM);
    wifiConfig.enterpriseConfig = enterpriseConfig;
    int nid = wifi.addNetwork(wifiConfig);
    wifi.saveConfiguration();
    wifi.enableNetwork(nid, true);

1 个答案:

答案 0 :(得分:1)

这个答案对我很有帮助。

How to programmatically create and read WEP/EAP WiFi configurations in Android?

这是通过EAP Sim配置Wifi的方式。

        private fun getWifiConfigurationForAirtelEAP(ssId: String,sim:String):WifiConfiguration{
            val wifiConfiguration = WifiConfiguration()
            wifiConfiguration.SSID = String.format("\"%s\"", ssId);
            wifiConfiguration.hiddenSSID = false
            wifiConfiguration.BSSID = null

            wifiConfiguration.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN)

            wifiConfiguration.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP)
            wifiConfiguration.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP)
            wifiConfiguration.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP104)
            wifiConfiguration.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40)

            wifiConfiguration.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_EAP)
            wifiConfiguration.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.IEEE8021X)

            wifiConfiguration.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP)
            wifiConfiguration.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP)

            wifiConfiguration.allowedProtocols.set(WifiConfiguration.Protocol.RSN)
            wifiConfiguration.allowedProtocols.set(WifiConfiguration.Protocol.WPA)

            wifiConfiguration.enterpriseConfig = getWifiEnterpriseConfig(sim)

            return wifiConfiguration
        }

        private fun getWifiEnterpriseConfig(sim: String): WifiEnterpriseConfig {
            val enterpriseConfig = WifiEnterpriseConfig()
            enterpriseConfig.eapMethod = WifiEnterpriseConfig.Eap.SIM
            enterpriseConfig.anonymousIdentity = "NULL"
            val field = enterpriseConfig.javaClass.getDeclaredField("mFields");
            field.isAccessible =true
            val values = (field.get(enterpriseConfig) as HashMap<String,String>)
            values.put(SIM_NUM,"\""+sim+"\"")
            values.put(CLIENT_CERT,"NULL")
            values.put(KEY_ID,"NULL")
            values.put(ENGINE,"0")
            values.put(ENGINE_ID,"NULL")
            values.put(IDENTITY,"NULL")
            values.put(CA_PATH,"NULL")
            values.put(DOMAIN_SUFFIX_MATCH,"NULL")
            values.put(CA_CERT,"NULL")
            values.put(PASSWORD,"NULL")
            values.put(ANONYMOUS_IDENTITY,"NULL")
            return enterpriseConfig
        }