在 Android 中为双 SIM 卡设备配置 EAP-SIM

Configure EAP-SIM for dual SIM devices in Android

我已经尝试为单 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);

这个回答对我帮助很大

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
        }