Is it possible to add a network configuration on Android Q? Is it possible to add a network configuration on Android Q? android android

Is it possible to add a network configuration on Android Q?


I stuck with same issue, but somehow I reached a reproducible state for connecting a desired network and I want to share my findings it may helps.

As a summary:You have to disable all auto connection before applying WifiNetworkSuggestion logic

For more details, Please read the following:

I used the following code (Similar to what you use):

private fun connectUsingNetworkSuggestion(ssid: String, password: String) {    val wifiNetworkSuggestion = WifiNetworkSuggestion.Builder()        .setSsid(ssid)        .setWpa2Passphrase(password)        .build()    // Optional (Wait for post connection broadcast to one of your suggestions)    val intentFilter =        IntentFilter(WifiManager.ACTION_WIFI_NETWORK_SUGGESTION_POST_CONNECTION);    val broadcastReceiver = object : BroadcastReceiver() {        override fun onReceive(context: Context, intent: Intent) {            if (!intent.action.equals(WifiManager.ACTION_WIFI_NETWORK_SUGGESTION_POST_CONNECTION)) {                return            }            showToast("Connection Suggestion Succeeded")            // do post connect processing here        }    }    registerReceiver(broadcastReceiver, intentFilter)    lastSuggestedNetwork?.let {        val status = wifiManager.removeNetworkSuggestions(listOf(it))        Log.i("WifiNetworkSuggestion", "Removing Network suggestions status is $status")    }    val suggestionsList = listOf(wifiNetworkSuggestion)    var status = wifiManager.addNetworkSuggestions(suggestionsList)    Log.i("WifiNetworkSuggestion", "Adding Network suggestions status is $status")    if (status == WifiManager.STATUS_NETWORK_SUGGESTIONS_ERROR_ADD_DUPLICATE) {        showToast("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) {        lastSuggestedNetwork = wifiNetworkSuggestion        lastSuggestedNetworkSSID = ssid        showToast("Suggestion Added")    }}

So here are the steps:

  1. Install fresh version / Or remove all suggestion you added before
  2. Make sure that you forgot all surrounding networks so your device won't auto-connect
  3. Add wifi network suggestions list
  4. Go to Wifi Settings to scan networks Or wait until next scan is running
  5. A notification prompt will appear :

Notification prompt 6. When you Press "Yes" the system will auto-connect with it via your app and internet will work normally. See the following:

Connected to Suggested Network

Please note the following:

  1. If you disconnect the network from Wifi Settings (i.e press disconnect bin icon in the following image) your network will be blocked for 24 hours from auto-connect even if you removed the suggested network using wifiManager.removeNetworkSuggestions(listOf(it)) and add it again. And even if you uninstall and install your app again

Connected Wifi details

Unfortunately, this is limitation added by Android System as described here:

If the user uses the Wi-Fi picker to explicitly disconnect from one of the network suggestions when connected to it, then that network is blacklisted for 24 hours. During the blacklist period, that network will not be considered for auto-connection, even if the app removes and re-adds the network suggestion corresponding to the network.

  1. If you uninstall the application while connected to suggested WiFi, the system will close the connection automatically.
  2. In case you have multiple suggestion you can priorities them by using WifiNetworkSuggestion.Builder().setPriority(<Priority Integer>) as mentioned here:

Specify the priority of this network among other network suggestions provided by the same app (priorities have no impact on suggestions by different apps). The higher the number, the higher the priority (i.e value of 0 = lowest priority).

  1. In case you pressed "No" in notification prompt, you can change it from (Settings > Apps & notifications > Special App access > Wi-Fi Control > App name) as described here:

A user declining the network suggestion notification removes the CHANGE_WIFI_STATE permission from the app. The user can grant this approval later by going into the Wi-Fi control menu (Settings > Apps & notifications > Special App access > Wi-Fi Control > App name).


I wish I had answers to all of your questions because I'm currently struggling with similar issues.

After many hours I was finally able to connect to the desired network using this approach:

val wifiNetworkSpecifier = WifiNetworkSpecifier.Builder()    .setSsid(ssid)    .setWpa2Passphrase(passphrase)    .setBssid(mac)    .build()val networkRequest = NetworkRequest.Builder()    .addTransportType(NetworkCapabilities.TRANSPORT_WIFI)    .setNetworkSpecifier(wifiNetworkSpecifier)    .build()val connectivityManager = applicationContext.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager?connectivityManager?.requestNetwork(networkRequest, ConnectivityManager.NetworkCallback())

You can receive a whole host of events through the ConnectivityManager.NetworkCallback().


Looks like they've added support in Android 11(API 30) for adding network configuration that persists outside of the application scope and is saved as a system network configuration just like it was done with the deprecated WiFiManager method addNetwork. All you need to do is to use ACTION_WIFI_ADD_NETWORKS to show a system dialog that asks a user if he wants to proceed with adding a new Wifi suggestion to the system. This is how we start that dialog:

// used importsimport android.provider.Settings.ACTION_WIFI_ADD_NETWORKSimport android.provider.Settings.EXTRA_WIFI_NETWORK_LISTimport android.app.Activityimport android.content.Intentimport android.net.wifi.WifiNetworkSuggestion// show system dialog for adding new network configuration    val wifiSuggestionBuilder = WifiNetworkSuggestion.Builder()                .setSsid("network SSID")                .build()    val suggestionsList = arraylistOf(wifiSuggestionBuilder)    val intent = new Intent(ACTION_WIFI_ADD_NETWORKS)    intent.putParcelableArrayListExtra(EXTRA_WIFI_NETWORK_LIST, suggestionsList);    activity.startActivityForResult(intent, 1000)

The dialog looks like this:

And then we just need to handle a result in onActivityResult method like this:

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {    if (requestCode == 1000) {        if (resultCode == Activity.RESULT_OK) {            // network succesfully added - User pressed Save        } else if (resultCode == Activity.RESULT_CANCELED) {            // failed attempt of adding network to system - User pressed Cancel        }    }}

But as I've tested this code on Android devices that have older Android versions(lower then API30) installed I've got a crash every time I want it to show that dialog for adding a new network configuration. This is the crash:

java.lang.RuntimeException: Unable to start activity: android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.settings.WIFI_ADD_NETWORKS (has extras) }

Looks like the new way is not back-supported out of the box. So, for API30 we can use a new Intent action, for API 28 and below we can still use the old way of adding Networks, but for API29 we have some kind of gray area where I was not able to find a good solution yet. If anyone has an idea what else to do please share it with me. ;)