4

In android, I have found network interface names & IP address associated with that interface by below code.

// Iterate over all network interfaces.
for (Enumeration<NetworkInterface> en = 
    NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();)
    {
        NetworkInterface intf = en.nextElement();
        // Iterate over all IP addresses in each network interface.
        for (Enumeration<InetAddress> enumIPAddr = 
            intf.getInetAddresses(); enumIPAddr.hasMoreElements();)
        {
            InetAddress iNetAddress = enumIPAddr.nextElement();
            // Loop back address (127.0.0.1) doesn't count as an in-use IP address.
            if (!iNetAddress.isLoopbackAddress())
            {
                sLocalIP = iNetAddress.getHostAddress().toString();
                sInterfaceName = intf.getName();
            }
        }
    }

Because of network interface name may vary from manufacturer to manufacturer and also it may vary in different devices of same manufacturer.

How to determine which network interface is for wifi, 3G, 4G(LTE) & VPN in Android?

Priyank Patel
  • 12,244
  • 8
  • 65
  • 85
  • Please Have a look at ConnectivityManager.java in android there he specified the type of connections please compare with those things.http://developer.android.com/reference/android/net/ConnectivityManager.html – android_dev Jul 09 '14 at 07:18
  • `ConnectivityManager` doesn't provide any information about network interface. – Priyank Patel Jul 09 '14 at 07:20

1 Answers1

4

With this methods you can identify if it's Wifi or Mobile connection:

private static boolean isConnectedWifi(Context context) {
        NetworkInfo info = ConnectionUtil.getNetworkInfo(context);
        return (info != null && info.isConnected() && info.getType() == ConnectivityManager.TYPE_WIFI);
    }

private static boolean isConnectedMobile(Context context) {
        NetworkInfo info = ConnectionUtil.getNetworkInfo(context);
        return (info != null && info.isConnected() && info.getType() == ConnectivityManager.TYPE_MOBILE);
}

If it's a mobile connection, you can know more with info.getSubType(). Using method like this:

 private static String getConnectionType(int type, int subType) {
        if (type == ConnectivityManager.TYPE_WIFI) {
            return "TYPE_WIFI";
        } else if (type == ConnectivityManager.TYPE_MOBILE) {
            switch (subType) {
                case TelephonyManager.NETWORK_TYPE_UNKNOWN:
                    return "TYPE_UNKNOWN";
                case TelephonyManager.NETWORK_TYPE_1xRTT:
                    return "TYPE_1XRTT"; // ~ 50-100 kbps
                case TelephonyManager.NETWORK_TYPE_CDMA:
                    return "TYPE_CDMA"; // ~ 14-64 kbps
                case TelephonyManager.NETWORK_TYPE_EDGE:
                    return "TYPE_EDGE"; // ~ 50-100 kbps
                case TelephonyManager.NETWORK_TYPE_EVDO_0:
                    return "TYPE_EVDO_0"; // ~ 400-1000 kbps
                case TelephonyManager.NETWORK_TYPE_EVDO_A:
                    return "TYPE_EVDO_A"; // ~ 600-1400 kbps
                case TelephonyManager.NETWORK_TYPE_GPRS:
                    return "TYPE_GPRS"; // ~ 100 kbps
                case TelephonyManager.NETWORK_TYPE_HSDPA:
                    return "TYPE_HSDPA"; // ~ 2-14 Mbps
                case TelephonyManager.NETWORK_TYPE_HSPA:
                    return "TYPE_HSPA"; // ~ 700-1700 kbps
                case TelephonyManager.NETWORK_TYPE_HSUPA:
                    return "TYPE_HSUPA"; // ~ 1-23 Mbps
                case TelephonyManager.NETWORK_TYPE_UMTS:
                    return "TYPE_UMTS"; // ~ 400-7000 kbps
                case TelephonyManager.NETWORK_TYPE_IDEN: // API level 8
                    return "TYPE_IDEN"; // ~25 kbps


                // Unknown
                default:
                    return "TYPE_UNKNOWN";
            }
        } else {
            return "TYPE UNKNOWN";
        }
    }

Hope it helps you :)

Marcel Bro
  • 4,907
  • 4
  • 43
  • 70