This reads the local ARP table on Android, which is cool. The problem is: the table only has one device in it... the router. Why doesn't it list all the other devices on the network? There are clearly more devices on the network other than the router.
One theory I've had is it only lists the devices that have been accessed by my Android device.(I don't know much about computer networking so this is all I could come up with)
Does anyone know why it only shows the router in the ARP table and nothing else?
I don't know about your question, I don't have enough experience with ARP to know what's going on. Maybe the router doesn't want to give up the other devices? I'm not sure.
I can however tell you that you're not using the correct idiom to read files. Since you're not closing the reader, you may run out of file system handles as you query the ARP table. Here's how the code should read:
Or if you're interested in individual lines, and the file is not too big:
Or if you have big files and you're on Java 8:
Steffe Wilson wrote:IIRC the ARP table is just a cache of recently discovered IP-MAC mappings. After a while the table is trimmed and older entries removed. Its purpose is merely to avoid continually having to request the same mapping info over and over by caching it here for a while.
Ah... that's a bit of a downer.
Would you know of any other ways to discover the devices on your network?
but it's very inefficient. It takes way too long to go through all 255. Any help is appreciated!
Stephan van Hulst wrote:The best way is probably to do this using native Linux commands, or by invoking a process that can do this. Maybe you can install something like nmap on Android?
Why do you want to see all hosts in the network?
I'm working on a network scanning tool similar to Fing.
I've decompiled the APK files for some similar apps but I'm terrible at understanding the code. I managed to understand they were pulling something from the /proc/net/arp file. I need to study their apps some more... but I was hoping someone could push me in the right direction.
Wouldn't I need root access for the app to access linux commands?
As Stephen says, a possibly faster way is running "/system/bin/ping -c1 <ip>", again from multiple threads. The -c1 means send just 1 packet, to reduce the time.
Run it using java.lang.ProcessBuilder. /system/bin/ping is part of stock android, and you don't need root to run it.
If you're really adventurous, I think you can possibly extract bits of relevant code from ping.c or arping.c (both are GPL open source), compile them as an NDK project into its own native library,
bundle that library into your APK, and call a ping/arping function from your app via JNI.
It's rather complex, but it may reduce the overhead of android having to launch many ping processes and reduce total time. I have not tried this, it's just a (remote and possibly high effort) possibility.
I will definitely try the more adventrous approach once I get the simpler approach working. That would be amazing if I could get something like that working.
I'm really thankful for the posts! You guys really opened my mind to some possibilities on Android.