问题描述
我需要找到设备的IP地址时,它的承载热点。我用这个code到目前为止:
I need to find the device's ip address when it's hosting a hotspot. I've used this code so far :
//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();
}
}
}
}
这工作得很好,但在无线的NetworkInterface
名称不同,在某些设备上。所以,我一定要找到该设备的WiFi 的NetworkInterface
名称(其热点)第一。我如何才能找到这个名字呢?还是有更好的方法来找到设备的IP地址?
This works quite fine but the wifi NetworkInterface
name differs on some devices. So I have to find the device's wifi NetworkInterface
name (for its hotspot) first. How can I find this name? Or is there a better approach to find the device's ip address?
///查找的MAC正确的IP地址也似乎不工作
/// Finding the right ip address by the MAC also doesn't seem to work
推荐答案
最近我想通了,在WifiAP IP地址硬codeD的Android系统。除非用户手动修改这个值(我认为这是非常罕见的),使用硬codeD值是绝对足够了。我认为这是最好的一段路要走。 IP地址为192.168.43.1:<一href="https://github.com/CyanogenMod/android_frameworks_base/blob/cm-10.1/wifi/java/android/net/wifi/WifiStateMachine.java?source=c#L1299">https://github.com/CyanogenMod/android_frameworks_base/blob/cm-10.1/wifi/java/android/net/wifi/WifiStateMachine.java?source=c#L1299
I recently figured out that the WifiAP ip address is hardcoded in Android. Unless a user has changed this value manually (I think that is very uncommon) using the hardcoded value is absolutely sufficient. I think this is the best way to go. The IP address is "192.168.43.1" : https://github.com/CyanogenMod/android_frameworks_base/blob/cm-10.1/wifi/java/android/net/wifi/WifiStateMachine.java?source=c#L1299
这篇关于Android的查找时,它的承载热点设备的IP地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!