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>