我想对我的项目进行修改,现在项目状态是.....它正在搜索可用的 WiFi 网络并显示列表,其中包含正常工作的网络信息。现在我想搜索并查看详细信息连接到网络的设备。有没有办法找到这些设备?你的评论对我有用,谢谢。
问问题
6644 次
2 回答
12
您可以遍历 IP 范围,并“ping”它们。它不是最好/最快的方法(UDP 更好),但它在许多情况下都有效。下面的示例代码返回连接到当前网络的 IP 地址列表。
private int LoopCurrentIP = 0;
public ArrayList<InetAddress> getConnectedDevices(String YourPhoneIPAddress) {
ArrayList<InetAddress> ret = new ArrayList<InetAddress>();
LoopCurrentIP = 0;
String IPAddress = "";
String[] myIPArray = YourPhoneIPAddress.split("\\.");
InetAddress currentPingAddr;
for (int i = 0; i <= 255; i++) {
try {
// build the next IP address
currentPingAddr = InetAddress.getByName(myIPArray[0] + "." +
myIPArray[1] + "." +
myIPArray[2] + "." +
Integer.toString(LoopCurrentIP));
// 50ms Timeout for the "ping"
if (currentPingAddr.isReachable(50)) {
ret.add(currentPingAddr);
}
} catch (UnknownHostException ex) {
} catch (IOException ex) {
}
LoopCurrentIP++;
}
return ret;
}
于 2012-01-06T18:36:31.963 回答