'Android 10: add WiFi network programmatically
I'm trying to connect my App to a specific WiFi network when the user presses a button. The connection needs to be saved and available beyond the use of my App.
- For Android <= 9, I can use
val netId = wifiManager.addNetwork(wifiConfig)
wifiManager.enableNetwork(netId, true)
wifiManager.reconnect()
and it works fine.
- For Android >= 11, I can use ACTION_WIFI_ADD_NETWORKS
val bundle = Bundle()
bundle.putParcelableArrayList(Settings.EXTRA_WIFI_NETWORK_LIST, suggestions)
val intent = Intent(Settings.ACTION_WIFI_ADD_NETWORKS)
intent.putExtras(bundle)
context.startActivity(intent)
and it works fine as well.
The problem is with Android 10:
the old method used previously (Android <=9) doesn't work because the API is deprecated, but
ACTION_WIFI_ADD_NETWORKS is only available from Android 11 forward.
On this OS version I was only able to create a temporary connection with WifiNetworkSuggestion, but I need to create a stable one that works even when my App is not being used. The ideal case would be the network being saved and available to the users to connect as they like.
Does anyone have any suggestion?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
