Android: Manipulating wifi using the WifiManager

2 min read
In none almost every single project I’ve ever worked on, i have had to deal with network connections, wether it is connected to 4G, wifi, or if theres any connection at all, but i never really had to programmatically turn the device wifi on/off or change the wifi connection either until recently. When i did, i thought it was pretty cool.

Heres how i did.

First and foremost…permissions of course.

These 4 permissions are required to allow you to change wifi states.

Next i created a broadcast receiver which gets started by a service, i will not show this piece of code since its trivial.

Note: the intent actions for the BroadcastReceiver are custom.

The actions

These are the 3 actions the receiver listens to

Heres the onReceive method of the WifiConnectionReceiver

I added some comments and put it all in one place so that it is easier to see and understand for this post.

But just incase lets step through it.

Line 54–59 are pretty self explanatory. Line56 just creates an instance of the WifiManager class. Line 61 is a very simple method that checks to make sure that the action text is not null or empty. Pretty simple right?

Inside of that check (lines 61–84) check for each action type and react accordingly. The WifiManager is pretty straight forward to use when it comes to enabling/disabling wifi. We just need to pass in either true or false to the setWifiEnabled() method.

The else if that checks for ACTION_CONNECT_TO_WIFI is the most complex part of all this, and its still easy. First we grab the wifi SSID and password. Then we check if both are not null or empty, if so throw an error, otherwise we try and connect to the wifi.

Here is the connectToWifi() method

Alright, lines 97–100 are self explanatory. Check if the wifi is on. If not turn it on and proceed.

Line 102 creates a WifiConfiguration object which represents a configured Wifi network, including the security configurations for the Wifi network.

Line 103 and 104are public calls to the SSID and the preSharedKey variables to set the Wifi SSID and password to the configurations. Both the SSID and the password need to be enclosed around quotes, hence the use of the String.format() method to do so.

Next, online 106 adds the network configurations to the WifiManager, which then returns the network id which is used to specify the network to be acted upon.

Line 107 disassociates from the currently active access point

Line 108 does the actual connection to the Wifi network by calling enableNetwork() and passing in the network id we got previously, and passing in true to let the WifiManager know to disable every other network.

Line 109 is a safe check incase the initial connection failed. What the reconnect() method does is

Reconnect to the currently active access point, if we are currently
disconnected.

And thats pretty much it. To test this, you can use adb by doing one of the following while the device is connected:

adb shell am broadcast -a android.intent.action.WIFI_ON 

adb shell am broadcast -a android.intent.action.WIFI_OFF

adb shell am broadcast -a android.intent.action.CONNECT_TO_WIFI -e ssid {wifi_ssid} -e password {pwd}

Note: do not add brackets when you enter the wifi) SSID or the password

You can find the complete broadcast receiver code HERE!!

Thanks for reading!