Home > Blockchain >  WifiManager not working on API 29 and higher using Android Studio
WifiManager not working on API 29 and higher using Android Studio

Time:03-02

So I tried to use the code for wifi manager to disable/enable wifi, my app's target device are for API 29 and higher only. I just found out that WifiManager only works for older version. Is there other way on how to do it that will work on higher versions?

For reference only:

<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission>

WifiManager wifiManager = (WifiManager) getApplicationContext().getSystemService(WIFI_SERVICE);

wifiManager.setWifiEnabled(true);

CodePudding user response:

setWifiEnabled is deprecated in Android 10 due to security reasons

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 fail and return false. If apps are targeting an older SDK (Build.VERSION_CODES.P or below), they can continue to use this API.

now your only way is to pass user to Settings GUI, in which Wi-Fi may be enabled manually only

Intent settingsIntent = new Intent(Settings.Panel.ACTION_INTERNET_CONNECTIVITY);
startActivityForResult(settingsIntent);
  • Related