Determine if public WiFi has network connectivity

176 views Asked by At

Is it possible to determine if the public WiFi network that the user has connected to has active connectivity?

Eg: You walk into a starbucks where you can connect to their free WiFi. The caveat being that you need to accept their terms & conditions to use it.

Put it in another way, is there a way to determine that even though the device is connected to the Starbucks WiFi, is he/she actually able to use the internet? I looked up a bunch of different SO posts:

How to check internet is available in wifi(Though Wifi is connected )

how to check that internet on local wifi is available or not?

None of them work. Apparently, despite connecting to the aforementioned WiFi network, the http status code received in the response is still 200 for me.

Am i missing something here?

3

There are 3 answers

0
Marc Johnston On

if there are terms and conditions ... then you won't be able to tell until they are accepted. If there are no preconditions ... then automatically connecting and pinging a known external server would give a good indicator of such.

4
ALAN WARD On

The network is probably connected using a Transparent Proxy. If this is the case, there is no direct connexion with the Internet, i.e. no packets are simply passed back or forth.

The TP intercepts HTTP requests, and makes them on your behalf on the real network. This is why they can insert a screen requesting user login or a similar action, that the user gets through his/her web navigator.

The TP will probably be configured to respond only to connexion requests using the HTTP and HTTPs protocols.

1
mike20132013 On

I know you have tried to ping the server and got the response code as 200 that's for a okay situation but when you get the response as 200, it may depend on various factors.

Reference : http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html

Now, what I believe is that if the data packets are sent back to the server and if you do get the packets in the response, there is a connection success.

However, you may want to wait for the server to respond or set some connection or read timeout to make sure that there is no bad response.

This is a sample code I have used in the past to determine the internet connectivity. I am using a Process to determine the check and tried to get the return value as 0.

public Boolean isConnectionAvailable() {
        try {
            Process process = java.lang.Runtime.getRuntime().exec("ping -c 1 www.google.com");
            int returnVal = process.waitFor();
            boolean reachable = (returnVal == 0);
            if (reachable) {

                Log.i(TAG, "Connection Successful");
                return reachable;
            } else {

                Log.e(TAG, "No Internet access");
            }

        } catch (Exception e) {

            e.printStackTrace();
        } finally {
            process.destroy();
        }
        return false;
    }

In the above code, I have google server as a medium for the check. However, you can use 8.8.8.8 as well like this :

Process process = java.lang.Runtime.getRuntime().exec("ping -c 1 8.8.8.8");

When the process executes, it will give a return value and that will determine the connection failure and success.

The key element is use the waitFor() method from the java class Process.class that causes the calling thread to wait for the native process associated with this object to finish executing..

Reference : http://developer.android.com/reference/java/lang/Process.html#waitFor%28%29

Give it a shot and I will be happy if this helps. If there's a better answer, I am glad to accept it.. Thanks .. :)