3

Is it possible to toggle WiFi radio (On/Off) programmatically on android phones?

Sam
  • 7,252
  • 16
  • 46
  • 65
Shatazone
  • 2,422
  • 6
  • 28
  • 38

3 Answers3

7

Have a look at the WifiManager: http://developer.android.com/reference/android/net/wifi/WifiManager.html

Specifically:

boolean setWifiEnabled(boolean enabled)
    Enable or disable Wi-Fi.

aioobe
  • 413,195
  • 112
  • 811
  • 826
2

To Enable WiFi:

WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
wifi.setWifiEnabled(true);

Note: To access with WiFi state, we have to add following permissions inside the AndroidManifest.xml file:

android.permission.ACCESS_WIFI_STATE
android.permission.UPDATE_DEVICE_STATS 
android.permission.CHANGE_WIFI_STATE
Valeh Ağayev
  • 556
  • 4
  • 12
0

To actually toggle, meaning, switching the state, use:

WifiManager wm = ((WifiManager) activity.getSystemService(Context.WIFI_SERVICE));
wm.setWifiEnabled(!wm.isWifiEnabled());

and add the permissions:

android.permission.ACCESS_WIFI_STATE
android.permission.UPDATE_DEVICE_STATS 
android.permission.CHANGE_WIFI_STATE
Oded Breiner
  • 28,523
  • 10
  • 105
  • 71