Intent Filter

Tuesday, 23 August 2016

Android platform is continuously growing and coming up with new concepts and features that are optimized to save battery or provide better user experience. Each new Android update comes up with new APIs along with changes in older ones. At times these changes might need developers to handle additional scenarios to provide features that are supported in newer versions.

One of such change was seen with an upgrade of Android OS to Lollipop and Marshmallow. With these upgrades the system changed how it handles connection to Networks. Starting from Lollipop, if a device is connected to both 3G and WiFi where WiFi network doesn't have internet connectivity, the device uses 3G network to send data. This was done so that devices don't automatically start using WiFi network that has restrictions or has a captive portal to connect to internet.

With start in active development in IoT field, there might be use cases where a device might want to connect to a local WiFi network that doesn't provide internet. There might be other use cases where your app might want to connect to a WiFi network that is behind captive portal and for automatically logging in to the network you might need to route your internet traffic through WiFi to make API calls needed to authenticate.

For such scenarios, Android does provide mechanisms for your app to explicitly use a particular network with specific capabilities to route all or parts of it's network traffic.

Here is a scenario where an app wants to connect to a WiFi network (which is behind a captive portal) using it's SSID and switch to start using WiFi network.*App on Marshmallow needs ACCESS_COARSE_LOCATION or ACCESS_FINE_LOCATION permission and Location services should be enabled to get wifi scan results

Steps to follow (Assuming that WiFi on device is enabled):

Scan for new networks should be initiated to get latest available networks. But in this case we would assume that a particular network is already available, found during scan initiated by the system automatically. Create a network request and register for callback in which app should bind to network when available. This would ensure that all the network traffic for current app is routed through this network.

Now the callback would be received when device is connected to required network.

Create a open WifiConfiguration with required SSID to which your app needs to connect.

Add the network to the list of configured networks by calling WifiManager.addNetwork(configuration).

A restriction that was put starting from Marshmallow was that the app can only modify those WiFi networks that it has created. The system doesn't allow modification of networks that other apps or users have created. So, adding the network might fail if the network already exists in the list (the system might auto connect to network once it's detected). Additional steps are needed to handle such a scenario:

Check networkId returned by addNetwork(), in case its -1 (unable to add network with configuration), try finding network with same SSID among list of existing networks.

In case any network with same SSID already exists, get networkId of that network.

Ultimately, only call enableNetwork(networkId, true) if a valid networkId exists. I would never call enableNetwork(-1, true) on Marshmallow.

Calling connectToSSID() will connect the device to network with given SSID. This will in turn provide a callback to onAvailable() of NetworkCallback in which the app would bind to this network. After binding is done, listener.onNetworkBound() is called in which a login request could be initiated.Once login is done, to tell the device that internet connectivity is available now in this network so that it becomes an active network and all android network traffic is routed through this network, you would need to call

This will make the OS to check if there is internet connectivity available in the network to which app is bound. If there is, OS would switch to make it an active network. Lastly, app should unbind from the network.If you are looking to solve similar problems or need to connect to WiFi networks in your project for similar reasons, there is an open source android library that provides all these functionalities and is easy to use. I would recommend you to give it a shot: android-wificonnect