2011-08-10 54 views
3

也许我问的是错误的问题,但我似乎无法找到我在找什么,所以我会试着在这里问它而不是谷歌。Android设备的网络接口名称

基本上,我有以下代码,从中我可以收集如果我在无线上,3G或其他东西(tethering想到)。

// 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的标签,这似乎当连接到3G时,您连接到WiFi和“pdp0”返回“的eth0”这就是说,我真的只能测试1个Galaxy S和1个Galaxy S Tab,因为他们是我唯一的Android设备。我想象网络接口名称是由设备管理器在内核的某个地方设置的。我可能会通过内核为每个设备,但我觉得有人必须已经找到这个信息了,有什么建议在哪里寻找或搜索谷歌?

回答

1

你应该使用更简单的方法。致电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 
    }  
} 
+4

有用,但不回答这个问题。他想要接口的名称,而不是当前的网络类型。 – Andy