0

I am looking for a piece of code that would check network connectivity, of an Android device, including tethering, not only the wireless channels.

I have spent hours online looking for such a thing, but without any result! All the functions I found out manage only wireless net tests. Nothing with tethering. So is it at least possible? Do you have some corresponding?

midhunhk
  • 5,560
  • 7
  • 52
  • 83
Steph68
  • 197
  • 4
  • 13
  • Do you want to know if the device has access to the internet, or if it is connected to a network? (The two may be different). If it is the former, just try and hit google.com. – cjk Nov 20 '12 at 13:27
  • Check this answer before asking ... http://stackoverflow.com/a/9570292/1838457 – Android Developer Nov 20 '12 at 13:34
  • @cjk: both in fact, since if a shutdown the wifi (the case when I develop my app) the app still runs with tethering "network". So I want to be able to know if I can access the web, whatever the way (wifi, 3g, ... or tethering). – Steph68 Nov 20 '12 at 13:59
  • @Nancy: and you please READ the questions before answering. – Steph68 Nov 20 '12 at 13:59
  • @Steph68 All other answers are always the same, they use the inbuilt APIs to check if you have an active connection, not if that connection is any good. I would just try to get an HTTP200 response from http://google.com and if successful, you have internet access, through any type of connection. My phone is often connected to 3G, but something in the mobile network doesn't actually let me get to the internet, so most checks say my internet is on when actually it is not. – cjk Nov 20 '12 at 16:08
  • Ok, thanks for that explanation. It sounds quite logical, I thought so too... even if it bothers me. But as all my http requests are already timeouted (5s), I am gonna let down that idea to check http before requesting. If the only way to check network availability is another http test request (even with only a 1s timeout), it is not worth, since it would slow down my app. Thanks for your answer. ;-) – Steph68 Nov 21 '12 at 08:12

2 Answers2

2

this checks if Tethering is enabled. Cut from a class I changed to get adbWireless working over a tethered connection:

    // check if tethering is on
    try {
        Method[] wmMethods = mWifiManager.getClass().getDeclaredMethods();
        for(Method method: wmMethods)
            if("isWifiApEnabled".equals(method.getName()))
                return (Boolean) method.invoke(mWifiManager);
        return false;
    } catch (Exception x) {return false;}

And you also need android.permission.ACCESS_WIFI_STATE

koem
  • 554
  • 4
  • 24
0

Check this out:

    public static boolean isOnline(Context context) {
    boolean isOnline = false;
    try {
        final ConnectivityManager conMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        final NetworkInfo activeNetwork = conMgr.getActiveNetworkInfo();
        if(activeNetwork != null && activeNetwork.isConnected() && activeNetwork.isAvailable()) {
            isOnline = true;
        } else {
            isOnline = false;
        } 
    } catch(Exception e) {
        Log.e(Config.LOG_TAG, e.toString());
        isOnline = false;
    }
    return isOnline;
}

Also add such permission into AndroidManifest.xml:

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
Yuriy
  • 16
  • Thanks Yuriy, but this is one of the 12 625 741 things I already tried, BUT it does not manage a TETHERING connexion. :( – Steph68 Nov 20 '12 at 14:03