Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The perfect function to check Android internet connectivity including bluetooth pan

My application is working perfect in wifi and mobile networks, but fails to detect when connected through bluetooth tethering.

public boolean isNetworkAvailable() {
    ConnectivityManager cm = (ConnectivityManager) 
      getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo networkInfo = cm.getActiveNetworkInfo();

    if (networkInfo != null && networkInfo.isConnected()) {
        return true;
    }
    return false;
}

I tried running a few other applications . they also shows no network connectivity, But google applications works perfect and so as some other apps like whatsap. Wondering how they are doing it, and why most of the applications missing this point..

Can anyone tell me a way to check the internet connectivity in android, through all means available,including bluetooth pan and proxy,etc.

Any help will be appreciated. Thanks in advance..

like image 715
JIthin Avatar asked Jan 21 '15 04:01

JIthin


People also ask

How do I use Bluetooth PAN on Android?

To enable Bluetooth Tethering, go to Settings >> More >> Tethering & Portable hotspot >> select Bluetooth Tethering. The main challenge of Bluetooth tethering, like with USB tethering, is that it only supports one device at a time. That is the device that's connected to your Android via Bluetooth.

What is Bluetooth pan used for?

In the PAN, the internet connection of one of the devices can be shared with the other device through Bluetooth tethering. Using this Bluetooth technology, you can pair a payment terminal with an Android or iOS tablet or smartphone that functions as a mobile POS system.

What is Pan service Android?

PANS provides additional granularity of control specific to network traffic routing in Android. For example, OEMs can optimally define a logical network topology for the routing of application-level traffic.


1 Answers

Try connecting to an "always available" website. if any connection exist, this should return true:

protected static boolean hasInternetAccess()
{
    try
    {
        URL url = new URL("http://www.google.com");

        HttpURLConnection urlc = (HttpURLConnection) url.openConnection();
        urlc.setRequestProperty("User-Agent", "Android Application:1");
        urlc.setRequestProperty("Connection", "close");
        urlc.setConnectTimeout(1000 * 30);
        urlc.connect();

        // http://www.w3.org/Protocols/HTTP/HTRESP.html
        if (urlc.getResponseCode() == 200 || urlc.getResponseCode() > 400)
        {
            // Requested site is available
            return true;
        }
    }
    catch (Exception ex)
    {
        // Error while trying to connect
        return false;
    }
    return false;
}
like image 67
Muzikant Avatar answered Oct 14 '22 13:10

Muzikant