android addNetworkSuggestion 不起作用

android addNetworkSuggestion doesn't works

我正在编写一个 POC 以连接到特定的 WIFI 它适用于小于 29 的设备,但不适用于其他设备

我在 logcat 中收到“已添加建议”,但屏幕上没有显示任何内容,我正在使用 android MI 9T pro

相同的测试适用于 android 28 上的设备,所以我的凭据没问题:)

            val networkSSID = "XXX"
            val networkPass = "XXX"
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
                // doesn't show suggestion even if have "Suggestion Added" in the log
                val networkSuggestion1 = WifiNetworkSuggestion.Builder()
                    .setSsid(networkSSID)
                    .setWpa2Passphrase(networkPass)
                    .build()

                val suggestionsList = listOf(networkSuggestion1)
                var status = wifiManager?.addNetworkSuggestions(suggestionsList)
                Log.i("WifiNetworkSuggestion", "Adding Network suggestions status is $status")
                if (status == WifiManager.STATUS_NETWORK_SUGGESTIONS_ERROR_ADD_DUPLICATE) {
                    Log.d(TAG, "Suggestion Update Needed")
                    status = wifiManager?.removeNetworkSuggestions(suggestionsList)
                    Log.i("WifiNetworkSuggestion", "Removing Network suggestions status is $status")
                    status = wifiManager?.addNetworkSuggestions(suggestionsList)
                }
                if (status == WifiManager.STATUS_NETWORK_SUGGESTIONS_SUCCESS) {
                    Log.d(TAG, "Suggestion Added")
                }
            } else {
                // Works perfectly good
                val wifiManager = context?.getSystemService(WIFI_SERVICE) as WifiManager?
                if (havePermission) {
                    var found = false
                    for (config in wifiManager?.configuredNetworks!!) {
                        if (config.SSID == String.format("\"%s\"", wifi.SSID)) {
                            wifiManager?.enableNetwork(config.networkId, true)
                            found = true
                            break
                        }
                    }

                    if (!found) {
                        val wifiConfiguration = WifiConfiguration()
                        wifiConfiguration.SSID = String.format("\"%s\"", networkSSID)
                        wifiConfiguration.preSharedKey = String.format("\"%s\"", networkPass)

                        val wifiID = wifiManager?.addNetwork(wifiConfiguration)
                        if (wifiID != null) {
                            wifiManager?.enableNetwork(wifiID, true)
                        }
                    }
                }
            }

好吧,我成功地将 addNetworkSuggestion 更改为 requestNetwork 库

这是我的最终代码:

            val networkSSID = "XXX"
            val networkPass = "XXX"
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
                val wifiNetworkSpecifier: WifiNetworkSpecifier?

                if (networkPass.isEmpty()) {
                    wifiNetworkSpecifier =
                        WifiNetworkSpecifier.Builder().setSsid(networkSSID).build()
                } else {
                    wifiNetworkSpecifier = WifiNetworkSpecifier.Builder().setSsid(networkSSID)
                        .setWpa2Passphrase(networkPass).build()
                }

                val networkRequest = NetworkRequest.Builder()
                    .addTransportType(NetworkCapabilities.TRANSPORT_WIFI)
                    .setNetworkSpecifier(wifiNetworkSpecifier)
                    .build()

                val connectivityManager =
                    context?.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager?

                val networkCallback = object : ConnectivityManager.NetworkCallback() {
                    override fun onUnavailable() {
                        super.onUnavailable()
                    }

                    override fun onLosing(network: Network, maxMsToLive: Int) {
                        super.onLosing(network, maxMsToLive)

                    }

                    override fun onAvailable(network: Network) {
                        super.onAvailable(network)
                        connectivityManager?.bindProcessToNetwork(network)
                    }

                    override fun onLost(network: Network) {
                        super.onLost(network)

                    }
                }
                connectivityManager?.requestNetwork(networkRequest, networkCallback)


            } else {
                val wifiManager = context?.getSystemService(WIFI_SERVICE) as WifiManager?
                if (havePermission) {
                    var found = false
                    for (config in wifiManager?.configuredNetworks!!) {
                        if (config.SSID == String.format("\"%s\"", wifi.SSID)) {
                            wifiManager?.enableNetwork(config.networkId, true)
                            found = true
                            break
                        }
                    }

                    if (!found) {
                        val wifiConfiguration = WifiConfiguration()
                        wifiConfiguration.SSID = String.format("\"%s\"", networkSSID)
                        wifiConfiguration.preSharedKey = String.format("\"%s\"", networkPass)
                        val wifiID = wifiManager?.addNetwork(wifiConfiguration)
                        if (wifiID != null) {
                            wifiManager?.enableNetwork(wifiID, true)
                        }
                    }
                }
            }