Victor Kochetkov
Victor Kochetkov

Reputation: 221

Can't enable Wifi programmatically on some devices

I have this code:

WifiManager wifiManager = (WifiManager) this.getSystemService(Context.WIFI_SERVICE); 
wifiManager.setWifiEnabled(true);

Is it correct that this code won't properly working on absolutly any device? Are there any ways to acheive it?

Upvotes: 0

Views: 58

Answers (2)

ankur
ankur

Reputation: 1

It should work for any device provided that the app manifest has the following permissions:

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

Upvotes: 0

Arka Prava Basu
Arka Prava Basu

Reputation: 2560

From the docs

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.

Also make sure you have the requested the permission Manifest.permission.CHANGE_WIFI_STATE

Upvotes: 2

Related Questions