在 android 中获取错误的网络接口?

Getting Wrong Network Interface in android?

我已经将我的手机更新到 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里做一些组播操作

iswifi连接方式

 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" />

我认为问题在于 getInetAddresses() returns 接口列表,而 getActiveNetworkInterface 无论如何只返回第一个。

因此您获得的界面取决于 getInetAddresses() returns.

列表的顺序

话虽如此,解决方案之一可能是:使用 isUp() 查看 up 接口。 http://developer.android.com/reference/java/net/NetworkInterface.html#isUp()

Android 会在连接到 wifi 网络时关闭蜂窝连接以节省电量,因此仅查看 UP 接口,您应该能够确定 WiFi 接口。

您正在使用 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;
    }
}