title: Network discovery in android
body: <p>I want to discover all devices (host name, MAC address and vendor name) connected to my Wifi network through an android phone. There are no common services running on the devices.</p>
  
  <p>Tried the following methods:</p>
  
  <ol>
  <li>Pinging all IPs in the subnet takes too long (<code>inetAddress.isReachable()</code>).</li>
  <li>Sending an <code>nbstat</code> request to query the <code>NETBIOs</code> names works in some devices. Some of them give <code>PortNotOpenException</code> (port 137 not opened). Some other devices do not have <code>NETBIOs</code> name service enabled.</li>
  <li><p>Tried using <a href="https://jcifs.samba.org/" rel="noreferrer">jcifs</a> for android. Was able to fetch host names using </p>
  
  <pre><code>nbtAddress.getByName(ip)
  </code></pre></li>
  </ol>
  
  <p>looped over all IP addresses in the subnet. This method gives all hostnames, irrespective of whether they are active or not.</p>
  
  <p>What is the best method to find all ACTIVE devices on a network? Some apps like <a href="https://play.google.com/store/apps/details?id=com.overlook.android.fing&amp;hl=en" rel="noreferrer">FING</a> seem to do it.</p>
  
  <p>Edit: I tried to analyze how the android FING app does it.</p>
  
  <ol>
  <li>Connected a small hotspot with 2 devices. Started discovery in FING and captured packets on wireshark.
  A series of the following requests are sent(All broadcast):</li>
  </ol>
  
  <p><a href="https://i.stack.imgur.com/xQHh7.png" rel="noreferrer"><img src="https://i.stack.imgur.com/xQHh7.png" alt="Packets on wireshark"></a></p>
  
  <ol start="2">
  <li>Started discovery in FING on a large wifi network (65 k devices)
  Netbios name service query and response packets are captured. (Unicast and sometimes to broadcast address)</li>
  </ol>
  
  <p>Meanwhile, I try to print the arp entries of my android phone on the IDE.</p>
  
  <p>The number of entries keeps increasing as discovery progresses in FING app. </p>
  
  <p>5 entries before discovery starts</p>
  
  <p>Increases to 1025 entries as the discovery progresses.</p>
  
  <p>Decreases to 150 entries after the discovery is over. </p>
  
  <p>What exactly is happening? There were no ICMP requests. Can ARPRequests be sent in an android application? Please help. </p>