giuseppe_para
giuseppe_para

Reputation: 69

Android 10 requestNetwork() takes too long displaying specified network

I'm trying to connect my Android 10 device to a WiFi network. I'm using the WiFiNetworkSpecifier API to describe the network properties I want to connect to. The connection goes well, but I see often that the prompt shown to the user is taking too long (from 2 to 28 seconds) to display the network I described with WifiNetworkSpecifier object.

Here is my code (it is the same as the Google example linked here -> https://developer.android.com/guide/topics/connectivity/wifi-bootstrap)

ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkRequest networkRequest = new NetworkRequest.Builder()
                .addTransportType(NetworkCapabilities.TRANSPORT_WIFI)
                .removeCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)
                .setNetworkSpecifier(
                        new WifiNetworkSpecifier.Builder()
                                .setSsid(SSID)
                                .setWpa2Passphrase(psw)
                                .build()
                )
                .build();

            networkCallback = new ConnectivityManager.NetworkCallback() {
               @Override
               public void onAvailable(@NonNull Network network) {
                   if (WiFiCoordinator.this.listner != null){
                       WiFiCoordinator.this.listner.onConnected();
                   }

                   cm.bindProcessToNetwork(network);
               }

               @Override
               public void onUnavailable() {
                   super.onUnavailable();
                   listner.onTestNetworkNotAvailable();
               }
            };
            cm.requestNetwork(networkRequest, networkCallback);

The connection has no problem, but the time spent by the OS looking for the requested network is not ok for me. Is there any problem in my code?

Thanks a lot

Upvotes: 2

Views: 924

Answers (1)

山脊之
山脊之

Reputation: 26

Through comparative experiments, it is found that Android 11 can work as well as Android 9

I guess it's the Android 10 bug. Comparing the code of Android 11 and Android 10, we find that there is a difference in the request network process.

Upvotes: 1

Related Questions