Disconnect Wifi programmatically on Android 10

Viewed 1547

I use WifiNetworkSuggestion to connect programmatically my app to my hotspot, but when I tried to disconnect from this connection programmatically I used removeNetworkSuggestions like the doc mention

Remove some or all of the network suggestions that were previously provided by the app. If one of the suggestions being removed was used to establish connection to the current network, then the device will immediately disconnect from that network.

I found that it's a bug https://issuetracker.google.com/issues/140398818, so my question is : there's no way right now to disconnect from a wifi programmatically on Android 10 ??

Thank's

4 Answers

You can't do that, But you can open WIFI Settings from your app.

There is Settings.Panel in Android 10+, where you can show popup intents to prompt user to change settings like Wifi, NFC etc. quickly without opening up the settings menu.

You can simply show the WiFi popup as follows.

startActivityForResult(Intent(Settings.Panel.ACTION_WIFI), ENABLE_WIFI_REQUEST)

From here, user can toggle WiFi as well as connect to a different network without having to leave the app.

It's a little bit late, but I am also searching for a solution to disconnect correctly.
My workaround is very dirty, because to cancel my connection I am using System.exit(0) when I stop my application for Android Q devices.

Maybe that helps until you find a better solution.

This is working:

_connectivityManager.UnregisterNetworkCallback(_callback);
Related