Android: Manipulating wifi using the WifiManager
In none almost every single project I've ever worked on, i have had to deal with network connections, whether it is connected to 4G, wifi, or if there's 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