Turning on wifi using WifiManager stops to work on Android 10

混江龙づ霸主 提交于 2019-11-26 18:35:50

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/58075596/turning-on-wifi-using-wifimanager-stops-to-work-on-android-10

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!