0

we were wanting to build a functionality where if a user is connected to a WiFi network, we can display the details of other devices connected to the same WiFi. How do we go about it?

Right now, we are able to achieve it by:

  1. pinging all the IP addresses on the current network(looping from 1-255) - this is the most time consuming step (code snippet below)
  2. for the IP addresses that responded, fetching their MAC addresses and finally
  3. fetching the manufacturer for the MAC addresses using an external API

we have success in this but the issue is that it takes way too long - around 4-5 minutes to do this. I have a feeling that someone can point us towards a better, faster solution.

There was a similar question(although it was about iOS) but didn't find any answer, hence posting this again. Please pardon if that was against the rules. Any help in this would be highly appreciated.

Here's the snippet of code which is taking too long to give the results back(step 1)

for (int i = 0; i < 255; i++) {
String host = "";
try {
String subnet = "192.168.2";
host = subnet + "." + i;

Process exec = Runtime.getRuntime().exec(String.format(CMD, host));
int i1 = exec.waitFor();
if (i1 == 0) {
InetAddress a = InetAddress.getByName(host);
Log.i("TAG", "run: " + a.getHostAddress());
} else {
throw new IOException("Unable to get ping from runtime");
}
} catch (IOException | InterruptedException e) {
try {
InetAddress a = InetAddress.getByName(host);
if (a.isReachable(200)) {
Log.i("TAG", "run: " + a.getHostAddress());
}
} catch (IOException ioe) {
ioe.printStackTrace();
}

} catch (Exception e) {
e.printStackTrace();
}
Community
  • 1
  • 1
awkward101
  • 77
  • 7
  • Found this link, the answer for this question also provides some other links for information. Might be of help to you. http://stackoverflow.com/questions/3345857/how-to-get-a-list-of-ip-connected-in-same-network-subnet-using-java – Thraydor Jan 24 '17 at 09:07
  • @Thraydor checking, thans for this – awkward101 Jan 24 '17 at 09:10
  • How do you know the network is a `/24` network? if the prefix is longer, then you are pinging too much, and if it is shorter, then you are not pinging nearly enough. You need to check the network mask to decide how much to ping. The tools that try to accomplish this use multiple methods, not just ICMP (ping), and they still don't always get every host. For instance, many hosts will simply not respond to ping. – Ron Maupin Jan 24 '17 at 14:23
  • Fing provides an SDK for mobile devices: https://www.fing.io/fingkit-mobile-device-identification-engine/ (just in case this could be an option for you). – Daniele Pantaleone Apr 05 '17 at 09:05
  • @DanielePantaleone thanks, will look into it – awkward101 Apr 06 '17 at 11:24

0 Answers0