0

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?

Community
  • 1
  • 1
user2511882
  • 9,022
  • 10
  • 51
  • 59

3 Answers3

1

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 .. :)

mike20132013
  • 5,357
  • 3
  • 31
  • 41
  • Good attempt for the OP. Doesn't always work though. Acts real wonky in some situations. Also getting the runTime environment and spawning a new process is a quite a risky approach, from my reading. – user2511882 Jun 18 '15 at 21:43
0

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.

Marc Johnston
  • 1,276
  • 1
  • 7
  • 16
0

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.

ALAN WARD
  • 1,072
  • 9
  • 4
  • are you suggesting that with the Transparent Proxy in place, the user is already online, only the fact that the login is required to actually let the TP make calls on your behalf? – user2511882 Jun 17 '15 at 19:31
  • @user2511882 "there is no direct connexion" The user is connected only to the proxy. The login mechanism may or not be required, or it may be simply there to make the user aware of the existence of terms and conditions. If you do see it, however, it does tell you that something is messing around with the connexion between your device and the webserver. – ALAN WARD Jun 17 '15 at 20:19
  • even to be connected to the proxy doesnt the user need to be online since the proxy itself is hosted at some IP address? – user2511882 Jun 17 '15 at 20:32
  • No, the proxy is on the local network, usually the outbound router. The proxy has two interfaces, one to the Internet. But it doesn't let your traffic use it directly. – ALAN WARD Jun 17 '15 at 21:11