问题描述
我需要确定是否在Android设备连接到无线网络,如果是的话,获得其无线IP地址。
我知道如何使用ConnectivityManager确定活动网络是WiFi网络,我知道如何使用java.net.NetworkInterface遍历可用的网络接口,并获得他们的IP地址。
我不知道叫什么怎么做的是确定哪些IP地址属于Wifi网络,如果发现多个地址。有什么建议?
感谢。
公共字符串getIpAddr(){
WifiManager wifiManager =(WifiManager)getSystemService(WIFI_SERVICE);
WifiInfo wifiInfo = wifiManager.getConnectionInfo();
INT IP = wifiInfo.getIpAddress();
字符串ipString =的String.Format(
%D,%D,%D%D,
(IP&安培;为0xFF)
(IP>→8&安培; 0xff的),
(IP>> 16安培;为0xFF)
(IP>> 24&安培;为0xFF));
返回ipString;
}
请注意:您需要添加 android.permission.INTERNET对
和 android.permission.ACCESS_WIFI_STATE
在的AndroidManifest.xml
为<使用者-许可/>
访问$ C $ ℃。
<使用-权限的Android:名称=android.permission.INTERNET对/>
<使用-权限的Android:名称=android.permission.ACCESS_WIFI_STATE/>
I need to determine if the Android device is connected to Wifi, and if so, obtain its Wifi IP address.
I know how to use ConnectivityManager to determine whether the active network is a Wifi network, and I know how to use java.net.NetworkInterface to iterate over the available network interfaces and get their IP addresses.
What I don't know how to do is determine which IP address belongs to the Wifi network, if there is more than one address found. Any advice?
Thanks.
public String getIpAddr() {
WifiManager wifiManager = (WifiManager) getSystemService(WIFI_SERVICE);
WifiInfo wifiInfo = wifiManager.getConnectionInfo();
int ip = wifiInfo.getIpAddress();
String ipString = String.format(
"%d.%d.%d.%d",
(ip & 0xff),
(ip >> 8 & 0xff),
(ip >> 16 & 0xff),
(ip >> 24 & 0xff));
return ipString;
}
Please Note: You need to add android.permission.INTERNET
and android.permission.ACCESS_WIFI_STATE
in your AndroidManifest.xml
as <user-permission/>
to access the code.
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
这篇关于检测无线IP地址在Android?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!