Saturday, March 08, 2008

Controlling your Linux PC with your presence

Well the title may be a "bit" misleading. Its literally true as long as you have a bluetooth enabled mobile phone with you. I got the idea of controlling the pc depending on the presence of a Bluetooth enabled phone from KBlueMon, a KDE app that locks the screen if a specified device disappears. It didnt work as well as I would have liked and i wanted the app to do a little bit more. So i decided to try my hand in writing a small no-GUI tool using the BlueZ GNU/Linux Bluetooth stack, that would pause amarok when I(my phone) left the area and would start playback again when I'm around.

  • A Bluetooth enabled mobile phone. Bluetooth needs to be turned on but it need'nt be discoverable once you know your phones unique bluetooth id.
  • Standard issue bluetooth dongle hooked up to your pc
  • Any recent GNU/Linux distro(2.6* kernel). I use Kubuntu 7.10 Gutsy Gibbon
  • bluez-utils and libbluetooth-dev installed
I tried many methods for checking whether a device with known bluetooth id is present. The standard hcitool query method was too slow for my purpose. Hence i decided to use a small function provided by the Bluez C development headers. The hci_read_remote_name function is a reverse DNS of sorts. It queries and returns the human readable name of a bluetooth device given its bluetooth address. The bluetooth address is set in the code(too lazy:D). To find out your phones bluetooth id, make it discoverable and run "hcitool scan" from terminal. Rest of the explanation is there in the code. Compile it using the option -lbluetooth

Eg: $g++ -o sense.bin sense.cpp -lbluetooth

To Do: Integrate into system tray and a GUI with many more functions maybe.

Have been running the program for some hours in my pc now. Every time i leave the area with my phone playback stops and when I'm back, playback resumes(WWE style >:) ). Do try this one out if you can and lemme know how it goes :)

Update: Bluetooth id of device to be tracked is now specified as a command line argument.

./bluesense.bin 00:17:E3:A3:59:CB

Also now it performs an extra check if device not found just to ensure the absence of the device before taking any action. This was needed as sometimes the phone takes sometime to respond to successive queries and hence may end up not responding to some queries.

Download code from
Post a Comment