Network Interface names for Android devices
Asked Answered
T

1

3

Maybe I'm asking the wrong question, but I can't seem to find what I'm looking for, so I'll try ask it here instead of google.

Basically, I have the following code, from which I can glean if I am on wifi, 3G or something else (tethering comes to mind).

    // 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();
                }
            }
        }

I believe the important part here is sInterfaceName = intf.getName();

Now on the Galaxy S and Galaxy S Tab, this seems to return "eth0" when you are connected to WiFi and "pdp0" when connected to 3G

That said, I've only really been able to test with 1 x Galaxy S and 1 x Galaxy S Tab as they are the only Android devices I have. I imagine the network interface name is set somewhere in the kernel by the device manager. I could probably troll through the kernel for each device, but I figured someone must have found this info out already, any suggestions on where to look or what to search in google?

Tend answered 10/8, 2011 at 12:38 Comment(0)
H
0

You should use easier approach. Call ConnectivityManager's function that returns current active network - getActiveNetworkInfo(). Having an instance of NetworkInfo you can call getType() and getSubtype() to get network type that is currently in use.


Here is an example:

NetworkInfo info = m_connectivityManager.getActiveNetworkInfo();
int netType = info.getType();
int netSubtype = info.getSubtype();

if (netType == ConnectivityManager.TYPE_WIFI || netType == ConnectivityManager.TYPE_WIMAX)  
{
   //no restrictions, do some networking
}
else if (netType == ConnectivityManager.TYPE_MOBILE && 
     netSubtype == TelephonyManager.NETWORK_TYPE_UMTS)
{
   //3G connection
   if(!m_telephonyManager.isNetworkRoaming())
   {
      //do some networking
   }      
}
Honan answered 10/8, 2011 at 13:6 Comment(1)
Useful, but doesn't answer the question. He wants the name of the interface, not current network type.Meditate

© 2022 - 2024 — McMap. All rights reserved.