After the tremendous success of my first Library MayI :P I decided to make another one. In general I like to make stuff I wanna use in my personal projects but before I get to that step, I always do a research first in the abundance of open source libraries to find something that could fit my needs. So that's what I did but to my surprise I only came across one or two outdated libraries that wouldn't fulfill my expectations.
Therefore I would like to introduce to you WiFi Utils. A library that provides a set of convenience methods for managing WiFi State, WiFi Scan, And
WiFi Connection to Hotspots. If you have ever worked with
WifiManager you should know how painful it is to make a simple wifi network scan or even worse
to connect to a hotspot programmatically. So that's what my new library is all about. To make it easier for me and hopefully for other developers as well
to do those kind of tasks from Java code. So lets jump right in some code examples.
NOTE: All of the examples presented here, use Retrolambda plugin to make the whole code more compact. You are not required to use it if you don't want to.
turn on device's wifi using the following:
checkResult could be a custom-defined method of your own that would deal accordingly in each situation. For Example:
If you don't want to deal with call backs you can just pass
enableWifi method like so.
Similarly you can turn off the wifi using this:
Connecting to WiFi Networks
Now lets get to the interesting stuff. You can connect to any WiFi network programmatically knowing only SSID and WPA/WPA2 key:
Again checkResult could be something like:
There are also a few other options that would allow you to do the same job: For example you can connect using SSID, BSSID and WPA/WPA2 Key:
Lastly WifiUtils can also connect using a specified
scanResult after a WiFi Scan is complete, for example:
The above example will perform a WiFi Scan and
connectWithScanResult will return a
List<ScanResult> scanResults with all the available WiFi networks
around. The method then expects you to Return a single
scanResult out of the list of results of your choice so that it can try to connect to it. The rest is
pretty much the same.
Canceling an ongoing connection
You have two options to cancel a connection in progress.
- If Connection takes too long to complete and just hangs in there without calling back
onConnectionResultYou can specify a TimeOut in milliseconds.
The Connection will fail in 15 seconds. The default timeOut is 30 seconds.
- You can also cancel an ongoing connection immediately using the following:
Connecting with WPS keys.
On Androids 5.0 and greater there is also an option to connect using WPS keys. This library makes it easier and safer to connect using WPS than the stock android API.
If you want to receive some extra logging info comming from WiFi Utils you can enable its logging capabilities with
Damn You are required to set a few permissions in order for this lib to work correctly :(
Add it to your project
Add the following to your app module
Apps using this library
My app of course GWPA Finder Duh :P
There are a few more things to cover in this tutorial. Hopefully I will improve upon this in the future.
Feel free to add/correct/fix something to this library, I will be glad to improve it with your help.