我想查找连接到本地WIFI网络的每台设备。如果设备响应ping,我将查看它是否是Android设备。如果是,地址将被添加到arraylist。但由于某些原因,Android设备不响应命令,在Windows控制台中,它们可以,但不在代码中。检测到房子里的所有计算机,但是我的2部电话没有,它们不处于待机状态可能是个问题。 我查了一下,我看到API无法在Android设备上使用该方法,但这看起来有点奇怪。这是我正在使用的代码:
try {
//To change body of implemented methods use File | Settings | File Templates.
InetAddress localhost = InetAddress.getLocalHost();
// this code assumes IPv4 is used
byte[] ip = localhost.getAddress();
for (int i = this.startIp; i <= this.startIp + 10; i++) {
ip[3] = (byte)i;
InetAddress address = InetAddress.getByAddress(ip);
if (address.isReachable(250)) {
System.out.println(address + " machine is turned on and can be pinged");
}
else if (!address.getHostAddress().equals(address.getHostName())) {
System.out.println(address + " machine is known in a DNS lookup");
if(address.getHostName().contains("android")) {
androidIpAdresses.add(address);
}
}
else {
System.out.println(address + " the host address and host name are equal, meaning the host name could not be resolved");
}
}
答案 0 :(得分:0)
isReachable()可能使用ICMP PING,或者它可能使用TCP ECHO(端口7),在这种情况下某些设备可能无法响应。使用wireshark查看是哪种情况。