Android设备的网络接口名称

时间:2011-08-10 12:38:26

标签: android

也许我问的是错误的问题,但我似乎无法找到我正在寻找的东西,所以我会尝试在这里而不是谷歌。

基本上,我有以下代码,如果我使用wifi,3G或其他东西,我可以从中收集(想到网络共享)。

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

我相信这里的重要部分是sInterfaceName = intf.getName();

现在在Galaxy S和Galaxy S Tab上,当你连接到WiFi时,这似乎返回“eth0”,当连接到3G时,它似乎返回“pdp0”

那就是说,我只能用1 x Galaxy S和1 x Galaxy S Tab进行测试,因为它们是我唯一拥有的Android设备。我想网络接口名称是由设备管理器在内核中设置的。我可能会为每个设备浏览内核,但我认为有人必须已经发现这个信息,有关在哪里查看或在谷歌搜索什么的任何建议?

1 个答案:

答案 0 :(得分:0)

你应该使用更简单的方法。调用返回当前活动网络的ConnectivityManager函数 - getActiveNetworkInfo()。拥有NetworkInfo的实例,您可以调用getType()getSubtype()来获取当前正在使用的网络类型。


以下是一个例子:

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
   }      
}