Proximity switch with RP3

Hello!
My idea is to use RP3 to detect presence (or absence) of the wifi device (android smart phone) and to toggle one GPIO pin on each connection/disconnection event. As first step I’ve created “hot spot” wifi and phone can connect to it, but then what? Obviously some kind of software has to “listen” for connection/disconnection event, or even - to check is desired phone is connected. Can someone advise me about what to look for.
Thanks!

Figured out - endless pinging and checking returncode.