Android Q always reusing the first entered credentials to connect to the WiFi network

♀尐吖头ヾ 提交于 2021-02-04 07:00:48

问题


I am trying to connect to WiFi using the WifiNetworkSpecifier on Android Q. The problem which I run into is Android always using the last valid credentials to connect to the AP, even after I manually provide the new, updated credentials to the WifiNetworkSpecifier.

This is the flow:

  1. Successfully connect to the AP.
  2. Change the password of the WiFi network.
  3. Run the app, try to connect again with the updated credentials (new, valid password provided) but the app can't connect to the WiFi (authentication fails).

If I reverse the logic, the same thing happens:

  1. Successfully connect to the WiFi.
  2. Run the app, provide it with the updated credentials (new, false password), but the app manages to connect to the WiFi.

The point is, once it connects to the SSID with the valid credentials, whatever I put to the WifiNetworkSpecifier doesn't seem to make any difference, it just uses this last valid credentials over and over again.

This happens on Google Pixel 2 and on Huawei P30 Lite. However, the things work normal on Samsung Galaxy S9 (the app connects to the WiFi network if I update the WiFi network password and provide the app with the new, valid password; and it won't connect to the WiFi if I provide it with false password, both assuming we previously successfully connected to the WiFi).

The problem on Pixel and Huawei gets "resolved" after the device is restarted, but when I connect to the WiFi once again (valid credentials are entered), we get back to the same issue.

I'm not sure if I'm missing something trivial here, but any help would be highly appreciated! Thanks!

EDIT: It turns out you don't have to enter valid credentials to trigger this loop. For instance, if you tried to connect to the AP with a false password for the first time and after you try to connect with the right password, the system uses this wrong password (the first entered password) to connect. Again, this happens on the Pixel 2 and Huawei P30 Lite, but works fine on Galaxy S9 and S9+. Restarting the phone still clears this "state".

This is the code:

    @RequiresApi(api = Build.VERSION_CODES.Q)
    private fun connectToWiFiOnQ(context: Context, wifiCredentials: WifiCredentials) {
        val connectivityManager: ConnectivityManager =
            context.applicationContext.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager

        val networkSpecifier = WifiNetworkSpecifier.Builder()
            .setSsid(wifiCredentials.networkSSID)
            .setWpa2Passphrase(wifiCredentials.password)
            .setIsHiddenSsid(wifiCredentials.isSSIDHidden)
            .build()

        val request = NetworkRequest.Builder()
            .addTransportType(NetworkCapabilities.TRANSPORT_WIFI)
            .setNetworkSpecifier(networkSpecifier)
            .build()

        networkCallback = object : ConnectivityManager.NetworkCallback() {
            override fun onAvailable(network: Network) {
                super.onAvailable(network)
                connectivityManager.bindProcessToNetwork(network)
            }
        }

        networkCallback?.let {
            connectivityManager.requestNetwork(request, it)
        }

    }

来源:https://stackoverflow.com/questions/61393636/android-q-always-reusing-the-first-entered-credentials-to-connect-to-the-wifi-ne

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