Bluetooth presence detection

Posted by – 27/12/2008

This is the follow-up on my Asus EEE PC configuration. Next on my TODO list was to make the webcam work, and as Ben Armstrong had pointed it worked fairly well, proving to be a non-issue.

After that, I decided it would be a good thing if the presence of my bluetooth-enabled cellphone were tested, so that if I walk away from the PC, it called xscreensaver -lock. After some googling, I found a tool that did just that: BlueProximity. It really seemed a good idea, except that my cellphone (a Palm Treo 650) kept warning me about a connection going on, which was quite unpleasant. This happens because BlueProximity tests the RSSI of a bluetooth connection… Beautiful, but a little overkill for what I wanted: I just wanted to know if it is there or not.

First I tested the bluetooth discovery with hcitool scan, but for that I would have to keep my cellphone Discovery On, which is not a smart thing to do… So I tested other things, and found out that hcitool name XX:XX:XX:XX:XX:XX only returned the name of my cellphone if it were around. So that was what I used. This is treo-presence.sh script:

#!/bin/bash
 
TREO='XX:XX:XX:XX:XX:XX'
NAME='name_of_my_cellphone'
CMD="/usr/bin/hcitool name $TREO"
 
if [ "`$CMD`" = "$NAME" ]; then
  exit 1
fi
 
exit 0

It returns 0 or 1 if the TREO device is absent or present, respectively. I use it from the following cron script:

#!/bin/bash
 
/usr/bin/w | /bin/grep $LOGNAME > /dev/null 2>&1
 
if [ $? -ne 0 ]; then
  # Running user is not logged in
  exit 1
fi
 
CMD="/usr/local/bin/treo-presence.sh"
LOCK_CMD="/usr/bin/xscreensaver-command -lock"
FILE="/tmp/no-treo-lock.txt"
TMPFILE=`/bin/tempfile`
 
/bin/touch $FILE
/usr/bin/tail -2 $FILE > $TMPFILE
$CMD; echo $? >> $TMPFILE
/bin/mv $TMPFILE $FILE
/bin/rm -f $TMPFILE
 
for line in `cat $FILE`; do
  if [ "$line" = "1" ]; then
    exit 1
  fi
done
 
# Got here: all 3 lines are not 1
$LOCK_CMD > /dev/null 2>&1
exit 0

This is run from the user crontab file every minute, recording the last 3 runs in a file in /tmp. If all 3 runs indicates the absence of my cellphone, xscreensaver -lock is called. Simple enough and doesn’t give me connection warnings in my Treo.

Other approaches are surely possible. Also, I am not sure treo-presence.sh would work for other devices… This is just what works for me…

5 Comments on Bluetooth presence detection

  1. spectra says:

    @aeth,

    Surely could… But I’d rather not having some endless while loop. That’s surely just a matter of preference, but whenever possible I prefer cronjobs over endless loops.

    BTW, it’s running as a user cronjob… I just test the LOGNAME varibale to check if the user is logged (user cronjobs run even if the owner is not logged, as they should 🙂 )

  2. spectra says:

    @Luiz Guaraldo,

    Sure, you can use in other ways… What is important is that hcitool name doesn’t generate a connection, and works in my Treo (and now in Sony Ericsson W300i). Thanks for the report.

  3. aeth says:

    very nice, but you could just as well do:

    while true; do if [ `hcitool name 00:00:00:00:00:00` != "my_bt_name" ]; then xlock -mode matrix -bg black -fg gray ;fi; sleep 60; done

    (it could even be smaller, less intrusive and simpler, but lets not give the newbies a headache)

  4. Otubo says:

    Very handy and simple solution.
    Congrats 🙂

  5. It’s very nice and works for my W300i too.

    Changing some lines is possible to make instantaneously lock (up to 1 minute) and unlock with gnome-screensaver-command instead.

    And best of all, different than BlueProximity (that discharged my phone in lass than 2 hours), this script don’t discharge my phone at all…

    Great job!

Leave a Reply

Your email address will not be published. Required fields are marked *