【发布时间】:2014-03-15 07:42:32
【问题描述】:
我需要在设备托管热点时找到设备的 IP 地址。到目前为止,我已经使用过这段代码:
//if is using Hotspot
for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) {
NetworkInterface intf = en.nextElement();
if (intf.getName().contains("wlan")) {
for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) {
InetAddress inetAddress = enumIpAddr.nextElement();
if (!inetAddress.isLoopbackAddress() && (inetAddress.getAddress().length == 4)) {
return inetAddress.getHostAddress();
}
}
}
}
这很好用,但某些设备上的 wifi NetworkInterface 名称不同。所以我必须先找到设备的 wifi NetworkInterface 名称(对于它的热点)。我怎样才能找到这个名字?还是有更好的方法来查找设备的 IP 地址?
/// 通过MAC查找正确的IP地址似乎也不起作用
【问题讨论】:
标签: android wifi ip-address android-wifi android-networking