3

I want to make an android app to find the IP address of all the connected devices connected on the same wifi. I tried this :

for (int i = lower; i <= upper; i++) {
    String host = subnet + i;

    try {
        InetAddress inetAddress = InetAddress.getByName(host);
        if (inetAddress.isReachable(timeout)){
            publishProgress(inetAddress.toString());
        }

    } catch (UnknownHostException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

But it can get only the connected mobile phone's IP, not the pc's. How to get the connected pc's IP address also?

Dr Mido
  • 2,414
  • 4
  • 32
  • 72

2 Answers2

0

Try this

public ArrayList<String> getClientList() {
    ArrayList<String> clientList = new ArrayList<>();
    BufferedReader br = null;
    try {
        br = new BufferedReader(new FileReader("/proc/net/arp"));
        String line;
        while ((line = br.readLine()) != null) {
            String[] clientInfo = line.split(" +");
            String mac = clientInfo[3];
            if (mac.matches("..:..:..:..:..:..")) { // To make sure its not the title
                clientList.add(clientInfo[0]);
            }
        }
    } catch (java.io.IOException aE) {
        aE.printStackTrace();
        return null;
    }
    return clientList;
}

and use it like this

ArrayList<String> list = getClientList();
        for (String ip:list) {
            Log.e(TAG,ip);
        }

Result :

192.168.1.9 192.168.1.1 192.168.1.5

also check this question

Dr Mido
  • 2,414
  • 4
  • 32
  • 72
0

Many networks ban that entirely, a practice known as "wireless isolation" or "client isolation", which saves battery, improves throughput, improves privacy if the network is public, at a cost of making it difficult for one client to find other hosts on the same WLAN.

For the remaining ones, pinging all of the addresses in the subnet will generally be the best way, even if a little slow. Your code looks okay, provided that the string subnet ends with a dot.

All of this is meaningless on IPv6, of course.

arnt
  • 8,949
  • 5
  • 24
  • 32