android turning on wifi programmatically

You can turn on/off wifi using following instructions :

WifiManager wManager = (WifiManager)this.getApplicationContext().getSystemService(Context.WIFI_SERVICE);
wManager.setWifiEnabled(booleanValue); //true or false

Set following permissions to your manifest:

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

This code is deprecated in API 29 or over

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.

Source : Link


You need to create wifiLock with WIFI_MODE_FULL_HIGH_PERF mode, based on the docs it will only work with the following constraints:

  1. The lock is only active when the device is connected to an access point.
  2. The lock is only active when the screen is on.
  3. The lock is only active when the acquiring app is running in the foreground.