I am trying to create a function that lists all connected devices on a local network. What I am doing is ping any address from the address space xxx0 to xxx255, but it does not work properly. Can someone explain or extend my code? I get a response from the phone (10.0.0.17) and the default gateway (10.0.0.138). The latter should not even be there (in fact, I do not know what the default gateway is, but ignore it). I miss IP from this computer though.
public ArrayList<InetAddress> getConnectedDevices(String YourPhoneIPAddress) {
ArrayList<InetAddress> ret = new ArrayList<InetAddress>();
LoopCurrentIP = 0;
String[] myIPArray = YourPhoneIPAddress.split("\\.");
InetAddress currentPingAddr;
for (int i = 0; i <= 255; i++) {
try {
currentPingAddr = InetAddress.getByName(myIPArray[0] + "." +
myIPArray[1] + "." +
myIPArray[2] + "." +
Integer.toString(LoopCurrentIP));
if (currentPingAddr.isReachable(50)) {
if(currentPingAddr.getHostAddress() != YourPhoneIPAddress){
ret.add(currentPingAddr);
}
}
} catch (UnknownHostException ex) {
} catch (IOException ex) {
}
LoopCurrentIP++;
}
return ret;
}
rtc11 source
share