I know there is way to find out whether a WiFi network is a captive portal by checking the response of something like "http://clients3.google.com/generate_204"
.
But my question is somewhat different. We all know that on Android when we connect to a WiFi network the connection cycle goes through a number of state mentioned in the class NetworkInfo.DetailedState, like AUTHENTICATING
, OBTAINING_IPADDR
, VERIFYING_POOR_LINK
etc. Also, one of the states is CAPTIVE_PORTAL_CHECK
and android system check whether the network which is being connected is captive or not. For this Android uses the CaptivePortalTracker#isCaptivePortal(InetAddress server) which results in a boolean
. So we are certain that Android knows whether the connection is restricted (captive) or not
So, my question is does Android system gives some callback or some state by which we can get to know the Network is captive portal without checking it manually? Since Android system have already checked the network for captive portal, do we need to do the same thing again?
P.S. I'm only targeting API 19 (KitKat 4.4.2).
Captive portals were detected using cleartext HTTP probes to known destinations (such as connectivitycheck.gstatic.com ), and if the probe received an HTTP redirect, the device assumed that the network was a captive portal.
A captive portal is a web page displayed to newly connected users of a WiFi network. It requires some sort of interaction before granting access to network resources. In other words – if you want to connect to a network that runs that, you need to go through a captive portal first.
On the internet, a walled garden is an environment that controls the user's access to network-based content and services. In effect, the walled garden directs the user's navigation within particular areas to enable access to a selection of material or prevent access to other material.
Click Wireless > Settings. Click the pencil symbol to the right of the SSID that you want to edit. Click Enable Captive Portal. If you want to redirect users to a website after they view the captive portal, click the Redirect URL slider and enter a redirect URL in the Redirect URL field.
You can get the networkcapabilities from the network and check if it has the NET_CAPABILITY_CAPTIVE_PORTAL
:
API 23
ConnectivityManager connectivityManager = (ConnectivityManager) getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE);
Network activeNetwork=connectivityManager.getActiveNetwork();
NetworkCapabilities networkCapabilities=connectivityManager.getNetworkCapabilities(activeNetwork);
if(networkCapabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_CAPTIVE_PORTAL)){
//DO SOMETHING
}
http://developer.android.com/intl/es/reference/android/net/NetworkCapabilities.html
NetworkCapabilities was introduced in API 21, so you can do this for
API >= 21
ConnectivityManager connectivityManager = (ConnectivityManager) getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE);
Network[] activeNetworks=connectivityManager.getAllNetworks();
for(Network network:activeNetworks){
if(connectivityManager.getNetworkInfo(network).isConnected()){
NetworkCapabilities networkCapabilities=connectivityManager.getNetworkCapabilities(network);
if(networkCapabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_CAPTIVE_PORTAL)){
//DO SOMETHING
}
break;
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With