androidwifimanagerandroid-10.0

Turning on wifi using WifiManager stops to work on Android 10


I have following code which used to work well pre Android 10. But it is not able to turn wifi on in Android 10 devices.

WifiManager wifiMgr = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
boolean res = wifiMgr.setWifiEnabled(true);
//res value is set to false above because setWifiEnabled returns false on Android 10

Following are my permissions in AndroidManifest.xml

<uses-permission android:name=\"android.permission.ACCESS_WIFI_STATE\"/>
<uses-permission android:name=\"android.permission.CHANGE_WIFI_STATE\"/>
<uses-permission android:name=\"android.permission.CHANGE_NETWORK_STATE\"/>
<uses-permission android:name=\"android.permission.INTERNET\"/>
<uses-permission android:name=\"android.permission.ACCESS_NETWORK_STATE\"/>

I am even dynamically requesting for these permissions. But that doesn't seem to help as well.

Question:
Has anything changed on Android 10? Should I do something more to turn on wifi programatically from my app?


Solution

  • public boolean 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.

    According to documentation, Apps will not be able to turn Wi-Fi OFF/ON anymore from Android-10 API level 29[Until google provides an alternative solution].

    For more information see official documentation.

    And there is an issue 128554616 has been created in google issuetracker about this.