我正在制作一个 Android 应用程序,需要能够查看本地网络设备(名称或 IP)。目前我可以扫描网络并找到设备的本地IP。然而,由于时间太长,用户在搜索网络时会看到黑屏加载几分钟。
这是我当前正在使用的代码:
private ArrayList<String> scanSubNet(String subnet) {
ArrayList<String> hosts = new ArrayList<String>();
InetAddress inetAddress = null;
for (int i = 1; i < 100; i++) {
try {
inetAddress = InetAddress.getByName(subnet + String.valueOf(i));
if (inetAddress.isReachable(1000)) {
hosts.add(inetAddress.getHostName());
Log.d("ERRORID", inetAddress.getHostName());
}
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
return hosts;
}
必须有一种更快的方法来搜索设备的网络,对吧?
请检查使用网络服务发现 http://developer.android.com/training/connect-devices-wirelessly/nsd.html并在AsyncTask中执行任务。当新节点加入您的本地网络时,继续更新 UI。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)