我已经将我的手机更新到Android Lollipop,在更新到Lollipop之前它运行正常。现在我遇到网络接口问题。
if (isWifiConnected()) {
Log.d(TAG,"Wifi is connected");
mNetIf = Utils.getActiveNetworkInterface();
String name = mNetIf.getName();
Log.d(TAG, "network interface in constructor" + NetworkInterface.getByName(name));
//do some multicast Operations.
}
如果连接了WiFi,我应该在WiFi中进行一些多播操作。
iswifiConnected方法
public boolean isWifiConnected(){
ConnectivityManager connManager = (ConnectivityManager)mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo mWifi = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
if (mWifi.isConnected()) {
return true;
}
return false;
}
Utils.getActiveNetworkInterface
public static NetworkInterface getActiveNetworkInterface() {
Enumeration<NetworkInterface> interfaces = null;
try {
interfaces = NetworkInterface.getNetworkInterfaces();
} catch (SocketException e) {
return null;
}
while (interfaces.hasMoreElements()) {
NetworkInterface iface = interfaces.nextElement();
Enumeration<InetAddress> inetAddresses = iface.getInetAddresses();
/* Check if we have a non-local address. If so, this is the active
* interface.
*
* This isn't a perfect heuristic: I have devices which this will
* still detect the wrong interface on, but it will handle the
* common cases of wifi-only and Ethernet-only.
*/
while (inetAddresses.hasMoreElements()) {
InetAddress addr = inetAddresses.nextElement();
if (!(addr.isLoopbackAddress() || addr.isLinkLocalAddress())) {
return iface;
}
}
}
return null;
}
日志
Wifi is connected
network interface in constructor**[rmnet0][3]t** [/fe80::32e3:daf0:ba51:f971%rmnet0%3][/27.57.104.11]//3g network interface
我想知道该应用程序如何获得3g界面。它应该得到wlan接口,它适用于所有其他移动设备。这是一个错误吗?
权限:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_MULTICAST_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
答案 0 :(得分:3)
您正在使用ConnectivityManger但是,要在Android中使用WiFi网络,最好的选择是使用WifiManager:它有许多专用于Wifi的功能。特别是,如果要获取引用WiFi接口的NetworkInterface对象,可以使用以下方法:
public static NetworkInterface getActiveWifiInterface(Context context) throws SocketException, UnknownHostException {
WifiManager wifiManager = (WifiManager)context.getSystemService(Context.WIFI_SERVICE);
//Return dynamic information about the current Wi-Fi connection, if any is active.
WifiInfo wifiInfo = wifiManager.getConnectionInfo();
if(wifiInfo == null) return null;
InetAddress address = intToInet(wifiInfo.getIpAddress());
return NetworkInterface.getByInetAddress(address);
}
public static byte byteOfInt(int value, int which) {
int shift = which * 8;
return (byte)(value >> shift);
}
public static InetAddress intToInet(int value) {
byte[] bytes = new byte[4];
for(int i = 0; i<4; i++) {
bytes[i] = byteOfInt(value, i);
}
try {
return InetAddress.getByAddress(bytes);
} catch (UnknownHostException e) {
// This only happens if the byte array has a bad length
return null;
}
}
答案 1 :(得分:1)
我认为问题是getInetAddresses()
返回一个接口列表,而getActiveNetworkInterface
只返回第一个接口,无论如何。
因此,您获得的界面取决于getInetAddresses()
返回的列表的顺序。
话虽这么说,其中一个解决方案可能是:使用isUp()
查看up界面。
http://developer.android.com/reference/java/net/NetworkInterface.html#isUp()
Android会在连接到wifi网络时关闭蜂窝连接以节省电量,因此只需查看UP
接口,您就可以确定WiFi接口的位置。