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

1

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?

Pettit answered 9/7, 2014 at 7:7 Comment(2)
Please Have a look at ConnectivityManager.java in android there he specified the type of connections please compare with those things.developer.android.com/reference/android/net/…Preciousprecipice
ConnectivityManager doesn't provide any information about network interface.Pettit
D
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 :)

Dorey answered 9/7, 2014 at 7:19 Comment(5)
What if wifi & LTE both are On, In that case two connection & IP exist simultaneously ?Pettit
I think it is not possible to have two active connections simultaneously.Crenelation
If your device has both wifi & LTE data on then it contains two IP address simultaneously.Pettit
interesting. But, Do you can connect with both simultaneously?Crenelation
That I don't know, But when I search for network interfaces of device using my code given in question then it gives two interface & IP address available... but how can I determine which IP address is for Wifi & which IP address is for LTE ?Pettit

© 2022 - 2024 — McMap. All rights reserved.