Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dialog in Wi-Fi Network Request API

I'm using the new API 29 for connecting the device to a wifi network in an Android 10 device:

    private val connectivityManager: ConnectivityManager by inject()

    override fun connectToNetwork(ssid: String, password: String) {
        val networkRequest = buildNetworkRequest(ssid, password)

        val networkCallback =
            object : NetworkCallback() {
                override fun onAvailable(network: Network) {
                    super.onAvailable(network)
                    Timber.d("Connected to network $ssid")
                }

                override fun onUnavailable() {
                    super.onUnavailable()
                    Timber.e("Unable to connect to network $ssid")
                }
            }

        connectivityManager.requestNetwork(networkRequest, networkCallback, CONNECTION_TIME_OUT)

    }

    private fun buildNetworkRequest(ssid: String, password: String) =
        NetworkRequest.Builder()
            .addTransportType(NetworkCapabilities.TRANSPORT_WIFI)
            .removeCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)
            .setNetworkSpecifier(buildWifiNetworkSpecifier(ssid, password))
            .build()

    private fun buildWifiNetworkSpecifier(ssid: String, password: String) =
        WifiNetworkSpecifier.Builder()
            .setSsid(ssid)
            .setWpa2Passphrase(password)
            .build()

A dialog appears with a "Device to use with " with the specified wifi network listed. The dialog has two buttons for "cancel" and "connect". When I click "connect", the device connects to the wifi network (I can see that in the system settings) and the connect button is disabled.

But the dialog does not go away and none of the methods in the requestNetwork callback is invoked. Eventually I reach the specified timeout and another dialog comes with "Something came up. The application has cancelled the request to choose a device".

What is happening here? I want to connect to a network and have the 'onAvailable' or 'onUnavailable' methods in the callback invoked.

like image 553
Rui Avatar asked Oct 14 '25 14:10

Rui


1 Answers

After trying almost everything, I finally managed to make this work as expected. Don't know why, but when I went to the device's system settings and forgot the wifi network that I was trying to connect programatically, this started to work.

I'm glad I got rid of the problem, but I don't know what was causing it, and it's a risk that the same thing happens to a future user.

like image 74
Rui Avatar answered Oct 18 '25 03:10

Rui