Enable Disable WiFi on Android 29
Asked Answered
B

5

10
public boolean WifiManager.setWifiEnabled (boolean enabled)

This method was deprecated in API level 29. Starting with Build.VERSION_CODES#Q, applications are not allowed to enable/disable Wi-Fi. Compatibility Note: For applications targeting Build.VERSION_CODES.Q or above, this API will always return false and will have no effect. If apps are targeting an older SDK ( Build.VERSION_CODES.P or below), they can continue to use this API.

How can we disable WiFi on Android 29?

Bainmarie answered 19/9, 2019 at 7:57 Comment(1)
this is the intended behavior and will not be fixed, this sucks, android 10 sucks overall. Scoped storage being the most annoying commonsware.com/blog/2019/03/29/…Unborn
F
4

In Android Q (Android 10, API level 29) you can't enable/disable wifi programmatically anymore. Use Settings Panel to toggle wifi connectivity:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
    val panelIntent = Intent(Settings.Panel.ACTION_INTERNET_CONNECTIVITY)
    startActivityForResult(panelIntent, 0)
} else {
    // add appropriate permissions to AndroidManifest file (see https://mcmap.net/q/235102/-android-how-to-enable-disable-wifi-or-internet-connection-programmatically/61289575)
    (this.context?.getSystemService(Context.WIFI_SERVICE) as? WifiManager)?.apply { isWifiEnabled = true /*or false*/ }
}
Fabrikoid answered 18/4, 2020 at 13:25 Comment(0)
L
4

This is a much simpler way of detecting the operating system version:

if (Build.VERSION.SDK_INT<Build.VERSION_CODES.Q)
   wifiManager.setWifiEnabled(status);
else
{
   Intent panelIntent = new Intent(Settings.Panel.ACTION_WIFI);
   startActivityForResult(panelIntent,1);
}

Basically, if the OS version is less than Android Q, use the WifiManager class object to enable/disable the use of Wi-Fi; otherwise, use implicit Intents to disable Wi-Fi.

Lovesome answered 5/2, 2021 at 5:1 Comment(1)
If targetSdkVersion < 29, wifiManager.setWifiEnabled works as intended on ALL android version, including newer ones. If targetSdkVersion >= 29, wifiManager.setWifiEnabled fails and returns false for ALL android version, including older ones. So what's the point of this if-else logic?Brinn
L
3

solved!

private void setWifiEnabled(boolean enabled) {
    try {
        Runtime.getRuntime().exec(new String[] { "su", "-c", "svc wifi", enabled ?
                "enable" :
                "disable" });
    }
    catch (IOException e) {
        e.printStackTrace();
    }
}

happily works on api 29

obviously, this requires root

does NOT require android.permission.CHANGE_WIFI_STATE

Lepto answered 22/7, 2020 at 19:45 Comment(1)
Another variant to this at https://mcmap.net/q/832213/-turning-on-wifi-using-wifimanager-stops-to-work-on-android-10.Gazpacho
O
1

Android Q has restricted this and developers can't disable wifi programmatically. your app will continue to work and can disable wifi if your targetSdkVersion <= 28

Outsole answered 18/4, 2020 at 13:30 Comment(0)
M
0

Toggling WiFi will not be allowed by apps starting from Android Q according to Google.

Here's the issue on their issuetracker: https://issuetracker.google.com/issues/128554616

Mooneye answered 19/9, 2019 at 8:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.