Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check the internet connectivity within the network in Android (using internet of some other device through HOTSPOT)

I have a requirement where I want to check whether there is any internet connectivity when I am connected with the network.

For example, I have device A and device B.

Device A is connected with hotspot with that of device B. In device A, I get it as connected with Wi-Fi and in device B - one device connected with hotspot.

Now, if I remove the internet from device B (not the tethering hotspot), then in device A, it still shows - connected with Wi-Fi but there is no internet connectivity.

Classes like ConnectivityManager help in determining whether a device is connected with the network not about the internet connectivity.

I want to track this issue. Is there any way to achieve this?

like image 596
android developer Avatar asked Sep 26 '14 09:09

android developer


People also ask

Can someone see what I do on my phone through Wi-Fi?

Yes. If you use a smartphone to surf the Internet, your WiFi provider or a WiFi owner can see your browsing history. Except for browsing history, they can also see the following information: Apps you were using.

Can you use your Android phone as a hotspot while connected to Wi-Fi?

But there is one big problem – due to Android restriction, you can not share WiFi while connected to the WiFi network. Yes, Android does come with a Portable WiFi Hotspot option, which when turned ON, will create a new WiFi hotspot. But the key here is – you can share the internet from your mobile data to WiFi.


1 Answers

It can be a ridiculous solution but i think also it could be real solution:

public boolean isOnline() {
    ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo netInfo = cm.getActiveNetworkInfo();
    if (netInfo != null && netInfo.isConnected()) {
        try {
            URL url = new URL("http://www.google.com");
            HttpURLConnection urlc = (HttpURLConnection) url.openConnection();
            urlc.setConnectTimeout(3000);
            urlc.connect();
            if (urlc.getResponseCode() == HttpURLConnection.HTTP_OK) {
                return new Boolean(true);
            }
        } catch (MalformedURLException mue) {
            // TODO Auto-generated catch block
            mue.printStackTrace();
        } catch (IOException ie) {
            // TODO Auto-generated catch block
            ie.printStackTrace();
        }
    }
    return false;
}
like image 150
Batuhan Coşkun Avatar answered Oct 20 '22 08:10

Batuhan Coşkun