In your code, why do you skip disconnected devices (continue if flag!=0x2)?
All you need to do (as I understand) is to get list of devices that are in proximity to your router regardless of their connection status. By dropping this condition you can react to device appearance before it connects to your network (couple seconds earlier).
I added that check to mark devices that have disconnected. For example, if I leave the room, the entry still stays in the arp proc file but it's status changes to 0x0. It will be removed according to whatever caching rules exist on the router. Hence, I decided to handle the flag and only honour devices that are connected.
Also, new connections were fairly fast to detect. There was less than a seconds delay everytime for my router. OTOH, disconnections are much slower to detect. On average, it was taking 30 mins to detect a disconnection.
We're a place where coders share, stay up-to-date and grow their careers.
We strive for transparency and don't collect excess data.