相关疑难解决方法(0)

Android在托管热点时查找设备的IP地址

我需要在托管热点时找到设备的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();
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

这工作得很好但是NetworkInterface某些设备上的wifi 名称不同.所以我必须首先找到设备的wifi NetworkInterface名称(针对其热点).我怎么能找到这个名字?或者是否有更好的方法来查找设备的IP地址?

///通过MAC查找正确的IP地址似乎也不起作用

android ip-address wifi android-networking android-wifi

10
推荐指数
2
解决办法
2万
查看次数