7

How i can know device is connected to Wifi or 3G, programmatically

Thanks

praveenb
  • 10,549
  • 14
  • 61
  • 83

2 Answers2

8

you can use WifiManager class as mentioned here

Edit: by calling getConnectionInfo() function of WifiManager class you will get WifiInfo object

WifiInfo has function getBSSID() which gives you connected AP's name

if its null that means it is not connected to any AP via Wifi ( Wifi is not enabled )

btw while looking for more info, i found this which should answer all your questions about mobile connectivity and wifi connectivity

Community
  • 1
  • 1
N30
  • 3,463
  • 4
  • 34
  • 43
  • The link shows about wifi connectivity is available or not. How i can know about 3G/2G is used to connect. Thank you, – praveenb Aug 11 '10 at 18:11
  • I see this word in that link "if the device is connected via mobile" what is that means? that means via 3G/2G? Please let me know Thank you – praveenb Aug 11 '10 at 18:23
  • not sure about how to find whether its connected to 3G/2G using sdk – N30 Aug 11 '10 at 18:41
  • On my T-Mobile G2X, this method is not reliable. It returns the last connected network(I think) even though it is not currently connected. The link the author of this answer provides in his last paragraph works. Can we assume that its consistent across the majority of the devices? – Ivan Dec 27 '11 at 20:11
0

here is my working sample:

public boolean isNetworkTypeMobile() {
    final ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    return (cm!=null && cm.getActiveNetworkInfo()!=null && isNetworkTypeMobile(cm.getActiveNetworkInfo().getType()));
}

public static boolean isNetworkTypeMobile(int networkType) {
    switch (networkType) {
        case ConnectivityManager.TYPE_MOBILE: //0
        case ConnectivityManager.TYPE_MOBILE_MMS: //2
        case ConnectivityManager.TYPE_MOBILE_SUPL: //3
        case ConnectivityManager.TYPE_MOBILE_DUN: //4
        case ConnectivityManager.TYPE_MOBILE_HIPRI: //5
        case 10:
        case 11:
        case 12:
        case 14:
            return true;
        default:
            return false;
    }
}
djdance
  • 3,110
  • 27
  • 33