Android 11 - 如何使用WifiNetworkSuggestion连接到WPA2企业EAP PEAP?

2 人关注

我正在开发一个应用程序,以连接到WPA2企业EAP PEAP网络,这样用户就不必输入他的证书了。

对于安卓11设备,我正在使用WifiNetworkSuggestion,因为我认为这是最好的可用选项。 问题是,当我试图连接时,会出现这个异常。

java.lang.IllegalArgumentException。企业配置是不安全的 at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33) at android.net.wifi.WifiNetworkSuggestion$Builder.setWpa2EnterpriseConfig(WifiNetworkSuggestion.java:275)

这就是我初始化配置的方式。

    private fun initWpa2EnterpriseWifiSuggestion(wifiConnection: WifiConnectionEntity) =
        if (wifiConnection.user != null && wifiConnection.password != null) {
            val networkSuggestionBuilder = WifiNetworkSuggestion.Builder()
            networkSuggestionBuilder.setSsid(wifiConnection.ssid)
            if (wifiConnection.sharedKey != null) {
                networkSuggestionBuilder.setWpa2Passphrase(wifiConnection.sharedKey)
            val enterpriseConfig = WifiEnterpriseConfig()
            enterpriseConfig.identity = wifiConnection.user
            enterpriseConfig.password = wifiConnection.password
            enterpriseConfig.eapMethod = WifiEnterpriseConfig.Eap.PEAP
            enterpriseConfig.phase2Method = WifiEnterpriseConfig.Phase2.MSCHAPV2
            networkSuggestionBuilder.setWpa2EnterpriseConfig(enterpriseConfig)
            Pair(networkSuggestionBuilder.build(), null)
        } else {
            Pair(
                null,
                if (wifiConnection.user != null) ERROR_USER_NOT_PROVIDED else ERROR_PASSWORD_NOT_PROVIDED

而我发起的建议是。

val wifiManager = appContext.getSystemService(Context.WIFI_SERVICE) as WifiManager
                val status = wifiManager.addNetworkSuggestions(listOf(wifiSuggestion.first))
                if (status != WifiManager.STATUS_NETWORK_SUGGESTIONS_SUCCESS) {
                    trySend(WifiManagementDataResult.Error(null, ERROR_NETWORK_SUGGESTION))
                } else {
                    trySend(WifiManagementDataResult.Success(null, null))

我一直在阅读https://developer.android.com/guide/topics/connectivity/wifi-suggest我需要指定ca证书(setCaCertificate方法)和服务器域名(setAltSubjectMatch方法)。

我没有找到太多的相关信息。 CA证书必须作为应用程序的资源,并将其转换为X509Certificate模型? 有没有人以其他方式连接?