StackTips

Turn on and off Wi-Fi using Android API

stacktips avtar

Written by

Editorial,  1 min read,  3.06K views, updated on Sept. 17, 2023

Use the following code snippets to turn on and off wifi on Android devices.

public void toggleWiFi(boolean status) {
	WifiManager wifiManager = (WifiManager) this.getSystemService(Context.WIFI_SERVICE);
	if (status == true && !wifiManager.isWifiEnabled()) {
		wifiManager.setWifiEnabled(true);
	} else if (status == false && wifiManager.isWifiEnabled()) {
		wifiManager.setWifiEnabled(false);
	}
}

For changing wifi state, you need the following permissions in your manifest file.

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