Android的:如何知道一个IP地址是一个无线IP地址? [英] Android: How to Know an IP Address is a Wifi IP Address?

查看:150
本文介绍了Android的:如何知道一个IP地址是一个无线IP地址?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道Android设备的是否IP地址数据IP或无线IP。

I want to know the whether IP address of an Android device is Data IP or Wifi IP.


1)设备,目前该设备将被分配到网络IP。
2)连接到WIFI后来设备,目前该设备将被分配到WIFI的IP。
3)任何Android的API,这将让我们知道一个IP地址是一个无线IP地址或网络IP ??


1) Device as is connected to 3G first, now the Device will be assigned to Network IP.
2) Later Device connected to WIFI , now the Device will be assigned to WIFI IP.
3)Any Android API which will let us know an IP Address is a Wifi IP Address or Network IP??

在2.3.5参考下,事情都很好,但是在4.0.3 ICS有一些问题。

Was Using below in 2.3.5 and things were fine, but in 4.0.3 ICS has some issues..

   /**
 * Is the IP Address a a Wifi Ip Address.
 * @param ipAddr
 * @return boolean
 */
public boolean isWifiIp(byte[] ipAddr){
    try{
        WifiManager mgr = (WifiManager)mCxt.getSystemService(Context.WIFI_SERVICE);
        int wifiIP = mgr.getConnectionInfo().getIpAddress();
        int reverseWifiIP = Integer.reverseBytes(wifiIP);  
        int byteArrayToInt = byteArrayToInt(ipAddr,0);

        if(byteArrayToInt == wifiIP || byteArrayToInt == reverseWifiIP)
            return true;
    }catch (Exception e) {
        Logger.d(TAG, e);
    }
    return false;
}


/**
 * Convert IP Address in bytes to int value.
 * @param arr
 * @param offset
 * @return int
 */
public static final int byteArrayToInt(byte[] arr, int offset) {
    if (arr == null || arr.length - offset < 4)
        return -1;

    int r0 = (arr[offset] & 0xFF) << 24;
    int r1 = (arr[offset + 1] & 0xFF) << 16;
    int r2 = (arr[offset + 2] & 0xFF) << 8;
    int r3 = arr[offset + 3] & 0xFF;
    return r0 + r1 + r2 + r3;
}

/**
 *  Fetches the IP Address of the Client. There is Delay of 2 Seconds for the API to return.
 */
public String getClientIpAddress() {
    try {
        for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) {
            NetworkInterface intf = en.nextElement();
            for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) {
                InetAddress inetAddress = enumIpAddr.nextElement();
                if(isWifiIp(inetAddress.getAddress())){
                    Logger.d(TAG, "-------- Local IP Address; Not Valid: "+inetAddress.getHostAddress());
                    continue;
                }
                if (!inetAddress.isLoopbackAddress()) {
                    String ipAddress = Formatter.formatIpAddress(inetAddress.hashCode());
                    Logger.d(TAG, "-------- Some Valid IPv4 is ---"+ipAddress);
                    return ipAddress;
                }
            }
        }
    } catch (SocketException ex) {
        Logger.e(TAG, ex.toString());
    }
    return null;
}

请帮助


4)当我关闭移动数据网络和WiFi是对了,我还是得到了一些有效的IPv4地址,这是没有看到在2.3.5及以下。


4) When i turn off Mobile Data Network and Wifi Is On,I still get a Some Valid IPv4 Address, which is not seen in 2.3.5 and Below.

感谢

推荐答案

发现的伎俩.. 的http://developer.android.com/reference/java/net/NetworkInterface.html#getName()

的getName支持WiFi将开始与wlan..using此验证无线网络IP。

getName for WiFi will start with wlan..using this validate WiFi IP.

这篇关于Android的:如何知道一个IP地址是一个无线IP地址?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆