java Android:如何知道 IP 地址是 Wifi IP 地址?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10683495/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android: How to Know an IP Address is a Wifi IP Address?
提问by NitZRobotKoder
I want to know the whether IP address of an Android device is Data IP or Wifi IP.
我想知道Android设备的IP地址是数据IP还是Wifi 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??
1) 设备 as 首先连接到 3G,现在设备将被分配到网络 IP。
2) 后来 Device 连接到 WIFI ,现在 Device 将被分配到 WIFI IP。
3)任何可以让我们知道 IP 地址是 Wifi IP 地址或网络 IP 的 Android API?
Was Using below in 2.3.5 and things were fine, but in 4.0.3 ICS has some issues..
在 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;
}
Pls Help
请帮助
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.
4) 当我关闭移动数据网络并且 Wifi 开启时,我仍然得到一些有效的 IPv4 地址,这在 2.3.5 及以下版本中没有看到。
Thanks
谢谢
采纳答案by NitZRobotKoder
Found the trick.. http://developer.android.com/reference/java/net/NetworkInterface.html#getName()
找到了诀窍.. http://developer.android.com/reference/java/net/NetworkInterface.html#getName()
getName for WiFi will start with wlan..using this validate WiFi IP.
WiFi 的 getName 将以 wlan.. 开头,使用此验证 WiFi IP。
回答by waqaslam
You cant detect connection type based on IP address because your mobile network and home WiFi network, both can assign private IP address.
您无法根据 IP 地址检测连接类型,因为您的移动网络和家庭 WiFi 网络都可以分配私有 IP 地址。
What you need to do is to first detect either you have mobile network or WiFi connection, and then based on that info get the IP address of that connection.
您需要做的是首先检测您是否有移动网络或 WiFi 连接,然后根据该信息获取该连接的 IP 地址。
回答by Aerrow
Try this code
试试这个代码
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";
}
}