any way to discover Android devices on your network?

There is an very simple approach that gave me positive results in few different devices.

When a device connects to your router it receives an IP (i.e. DHCP) and registers a name in DNS. The name that is registered seems to be always in the form android_nnnnnnnn.

Of course, you can name any computer with the same approach and trick the check, resulting in false positives …

Also, I can’t ensure that all device suppliers are following the same approach, but I’ve found it to work correctly in a few devices from different brands (including different SDK levels) that I’ve tested.

–EDITED–

How to do it

It depends on where you would be running the code to discover the Android devices. Assuming that you would be running the code in an Android device:

  1. First discover devices responding to ping in your network. You can use the code in my answer to this post: execComd() to run a ping command.

  2. Get the name of responding devices using the code:

    InetAddress inetAddress = InetAddress.getByName(string_with_ip_addr);

    String name = inetAddress.getCanonicalHostName();

–EDIT 2–

Proof of concept

The method below is just a proof of concept for what I’ve wrote above.

I’m using isReachable() method to generate the ICMP request, which is said to only work with rooted devices in many posts, which is the case for the device used for testing it. However, I didn’t give root permissions for the application running this code, so I believe it couldn’t set the SIUD bit, which is the reason why some claim that this method fails. I would like to do it here from the perspective of someone testing it on a non-rooted device.

To call use:

ArrayList<String> hosts = scanSubNet("192.168.1.");

It returns in hosts, a list of names for devices responding to ping request.

private ArrayList<String> scanSubNet(String subnet){
    ArrayList<String> hosts = new ArrayList<String>();
    
    InetAddress inetAddress = null;
    for(int i=1; i<10; i++){
        Log.d(TAG, "Trying: " + subnet + String.valueOf(i));
        try {
            inetAddress = InetAddress.getByName(subnet + String.valueOf(i));
            if(inetAddress.isReachable(1000)){
                hosts.add(inetAddress.getHostName());
                Log.d(TAG, inetAddress.getHostName());
            }
        } catch (UnknownHostException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    
    return hosts;
}

Regards.

Leave a Comment