在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中进行一些多播操作。

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" />
朱塞佩·伯通

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章