簡體   English   中英

Android:如何知道IP地址是一個Wifi IP地址?

[英]Android: How to Know an IP Address is a Wifi IP Address?

我想知道Android設備的IP地址是數據IP還是Wifi IP。


1)設備首先連接到3G,現在設備將被分配到網絡IP。
2)后來設備連接到WIFI,現在設備將被分配到WIFI IP。
3)任何能讓我們知道IP地址的Android API都是Wifi IP地址或網絡IP?

在2.3.5中使用下面的東西很好,但在4.0.3 ICS有一些問題..

   /**
 * 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及以下中沒有看到。

謝謝

您無法檢測基於IP地址的連接類型,因為您的移動網絡和家庭WiFi網絡都可以分配私有IP地址。

您需要做的是首先檢測您是否擁有移動網絡或WiFi連接,然后根據該信息獲取該連接的IP地址。

試試這個代碼

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;

import android.app.Activity;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.widget.Toast;

import com.blundell.tut.R;

public class MainActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        checkAvailableConnection();
    }

    void checkAvailableConnection() {
        ConnectivityManager connMgr = (ConnectivityManager) this
                .getSystemService(Context.CONNECTIVITY_SERVICE);

        final android.net.NetworkInfo wifi = connMgr
                .getNetworkInfo(ConnectivityManager.TYPE_WIFI);

        final android.net.NetworkInfo mobile = connMgr
                .getNetworkInfo(ConnectivityManager.TYPE_MOBILE);

        if (wifi.isAvailable()) {

            WifiManager myWifiManager = (WifiManager) getSystemService(WIFI_SERVICE);
            WifiInfo myWifiInfo = myWifiManager.getConnectionInfo();
            int ipAddress = myWifiInfo.getIpAddress();
            System.out.println("WiFi address is "
                    + android.text.format.Formatter.formatIpAddress(ipAddress));

        } else if (mobile.isAvailable()) {

            GetLocalIpAddress();
            Toast.makeText(this, "3G Available", Toast.LENGTH_LONG).show();
        } else {
            Toast.makeText(this, "No Network Available", Toast.LENGTH_LONG)
                    .show();
        }
    }

    private String GetLocalIpAddress() {
        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 (!inetAddress.isLoopbackAddress()) {
                        return inetAddress.getHostAddress().toString();
                    }
                }
            }
        } catch (SocketException ex) {
            return "ERROR Obtaining IP";
        }
        return "No IP Available";
    }
}

找到了訣竅.. http://developer.android.com/reference/java/net/NetworkInterface.html#getName()

WiFi的getName將以wlan開頭。使用此驗證WiFi IP。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM