0

In my app, I am attempting to call ConnectivityManager.requestNetwork(networkRequest, networkCallback) to prompt the user to connect to a particular WiFi network. Normally this works as expected; the system dialog with the text Device to use with MyApp is displayed, and the WiFi network is found. The user can connect to the network (or cancel), and the network connects successfully. All is working as expected.

However, this only seems to work properly if the device has not previously connected to the network. If the device had already connected to that network in the past, the Device to use with MyApp dialog is displayed over and over. When it is shown the first time, I can connect to the network and the onAvailable callback is called. However, immediately after it is connected, the dialog is shown again. If the user tapped Cancel instead, the dialog dismisses and does not show a second time. But, assuming they connect to the network, the dialog is always displayed again (even before the onAvailable callback is complete), and the pattern repeats ad infinitum.

I am only ever calling requestNetwork once, and even if I put a breakpoint in the onAvailable callback, by the time that breakpoint is hit, the second dialog is already shown. Once again, this only happens if the network is saved to the device; if the device hasn't already connected to the network, the dialog is only displayed once.

My code is below, though I don't think it's doing anything out of the ordinary. Is there something else I need to do to prevent this from happening?

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
    final NetworkSpecifier specifier =
            new WifiNetworkSpecifier.Builder()
                    .setSsid(ssid)
                    .build();
    final NetworkRequest request =
            new NetworkRequest.Builder()
                    .addTransportType(NetworkCapabilities.TRANSPORT_WIFI)
                    .removeCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)
                    .setNetworkSpecifier(specifier)
                    .build();

    mAndroidQNetworkCallback = new ConnectivityManager.NetworkCallback() {
        @Override
        public void onAvailable(Network network) {
            Log.d(TAG, "Connected to "+ssid+"!");
        }
        @Override
        public void onUnavailable() {
            Log.d(TAG, "NOT Connected to "+ssid+"!");
        }
    };
    mConnectivityManager = (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
    mConnectivityManager.requestNetwork(request, mAndroidQNetworkCallback);
}
Cowabunghole
  • 191
  • 1
  • 8
  • Some examples I've seen bind the process to the connectivity manager in the `onAvailable` callback, e.g. `mConnectivityManager.bindProcessToNetwork(network)` – SoftWyer Jan 28 '21 at 06:57
  • MIght also be a duplicate [of this issue](https://stackoverflow.com/questions/60660236/android-q-wifi-connection-via-wifinetworkspecifier-lose-connection-immediately-a) – SoftWyer Jan 28 '21 at 07:09

0 Answers0