Use Your Bluetooth Cell Phone as a Proximity Card for Linux Based Laptops (SUSE, Ubuntu, Red Hat)
Posted on 25 February 2007 by admin
Have you ever wished your computer would keep track of your comings and goings and secure itself accordingly? Odds are good that if you have a newer laptop and cell phone you can do just that. Many laptops now come with Bluetooth chips and most phones now support Bluetooth as well. This can allow your computer and cell phone to "talk" and create a simple proximity badge for you to use. There are many other fun things you can do with a link between your laptop and your phone but for now we’ll just cover how to setup the connection between the two and then have your laptop lock itself when you’re away.
The beauty of this design is that it is fairly secure (I’m assuming you don’t work for NASA, CIA, FBI or NSA). For the system to work you need to have the same MAC Address as your cell phone. Without that unique identifier the system won’t work. Someone can try sniffing the wireless transmission but they have to be within the range of the Bluetooth receiver on your laptop, and their transmitter needs to have the same MAC Address as your cell phone. Not an easy feat to duplicate. Just remember that if anyone asks for your cell phone and they have a bunch of aluminum foil and coat hangers…you best point them to the nearest pay phone. You can still manuall lock and unlock your system when you use this method.
The directions here are for SLED 10 but should work equally well on any recent Linux distribution. The only difference is where to enable the Bluetooth Service. To view how to configure Bluetooth services on UBUNTU here.
To start with, we’ll need to pair your phone to your laptop. This can be more difficult than it sounds depending on your phone manufacturer but the Linux side is straight forward. Open up YaST and go to Hardware->Bluetooth:

This will open up the Bluetooth YaST module for you. We’ll need to make a couple of changes to the default configuration:

You’ll want to make sure that Bluetooth Services are enabled and you’ll probably want to change the default Device Name as well. You’ll need to select Use Local PIN for Incoming Connections and set it to some numeric PIN because neither of the other options will work. Depending on your phone you may be able to tweak the Security Options to create a more secure communications link as well.
If everything is working properly you should be able to pair your Bluetooth enabled cell phone to your Bluetooth enabled laptop or desktop. From your cell phone go to your Bluetooth options and scan for devices. You should see your machine in the list and after selecting it you should be prompted for a PIN. Type in the same PIN you used in the YaST configuration and if everything goes well you’ve now established a link between your phone and your computer.
Next we’ll need to get the Bluetooth MAC address of our phone. This can be found if you look under /var/lib/bluetooth. There will be a directory (or several if you’ve paired multiple devices) that will be in hex in the form of a MAC address like 00:14:41:0D:2E:6D. In that directory will be a file called names that has the name of your cell phone as detected and the MAC address of your cell phone. Take note of the MAC address as we’ll need it later.
Once you’ve finished setting up your Bluetooth service we’ll need to make a few other changes to the system as root. Open up a terminal and su to root and then run the following commands:
chmod +s /usr/bin/hcitool
chmod +s /usr/bin/l2ping
What this does is allow any user on the system to run those programs as root. Since this opens up security a bit you can also just allow certain users to run those commands with sudo and then call sudo before those programs in the script we’ll be using.
The final step is to create a script that will do something with all this. For our purposes we’re going to assume you’re using SLED 10 with a default GNOME desktop but you can change the commands to do anything you want. Bluetooth has the ability to determine how close two devices are so we’ll tell our computer to perform one action if our phone is too far from the computer and another action when it becomes close again. We’ll start with this script:
#!/bin/bash
#set -o verbose sh -v
# Copied from Steven on http://gentoo-wiki.com/Talk:TIP_Bluetooth_Proximity_Monitor
# These are the sections you’ll need to edit
# You’ll need to use the MAC address of your phone here
DEVICE="00:0F:46:FF:05:07"
# How often to check the distance between phone and computer in seconds
CHECK_INTERVAL=2
# The RSSI threshold at which a phone is considered far or near
THRESHOLD=-13
# The command to run when your phone gets too far away
FAR_CMD=’/opt/gnome/bin/gnome-screensaver-command –activate’
# The command to run when your phone is close again
NEAR_CMD=’/opt/gnome/bin/gnome-screensaver-command –poke’
HCITOOL="/usr/bin/hcitool"
STARTX_PID=0
DEBUG="/tmp/btproximity.log"
connected=0
function msg {
echo "$1" #>> "$DEBUG"
}
function check_connection {
connected=0;
found=0
for s in `$HCITOOL con`; do
if [[ "$s" == "$DEVICE" ]]; then
found=1;
fi
done
if [[ $found == 1 ]]; then
connected=1;
else
msg ‘Attempting connection…’
if [ -z "`$HCITOOL cc $DEVICE 2>&1`" ]; then
msg ‘Connected.’
connected=1;
else
if [ -z "`l2ping -c 2 $DEVICE 2>&1`" ]; then
if [ -z "`$HCITOOL cc $DEVICE 2>&1`" ]; then
msg ‘Connected.’
connected=1;
else
msg "ERROR: Could not connect to device $DEVICE."
connected=0;
fi
fi
fi
fi
}
check_connection
while [[ $connected -eq 0 ]]; do
check_connection
sleep 3
done
name=`$HCITOOL name $DEVICE`
msg "Monitoring proximity of "$name" [$DEVICE]";
state="near"
while /bin/true; do
check_connection
if [[ $connected -eq 1 ]]; then
rssi=$($HCITOOL rssi $DEVICE | sed -e ’s/RSSI return value: //g’)
if [[ $rssi -le $THRESHOLD ]]; then
if [[ "$state" == "near" ]]; then
msg "*** Device "$name" [$DEVICE] has left proximity"
state="far"
$FAR_CMD > /dev/null 2>&1
fi
else
if [[ "$state" == "far" && $rssi -ge $[$THRESHOLD+2] ]]; then
msg "*** Device "$name" [$DEVICE] is within proximity"
state="near"
$NEAR_CMD > /dev/null 2>&1
STARTX_PID=$(pgrep startx)
fi
fi
msg "state = $state, RSSI = $rssi"
fi
sleep $CHECK_INTERVAL
done
You’ll need to change the DEVICE value to use the MAC address we found for the phone previously. You might also want to try different near and far commands if you use a different screen saver or want it to unlock for you altogether. For example, if you want the GNOME screensaver to deactivate when you get back instead of just opening the password prompt you could use:
NEAR_CMD=’/opt/gnome/bin/gnome-screensaver-command ?deactivate’
If you use xscreensaver you can use something like:
FAR_CMD=’/usr/bin/xscreensaver-command -lock’
NEAR_CMD=’/usr/bin/xscreensaver-command -deactivate’
There are many different options you could use to control how your computer behaves with Bluetooth proximity detection.
Once you’ve saved your script to a file you’ll need to make it executable. We’ll save it under ~/bin/proximity.sh for our purposes but you can save it anywhere on the system. After you’ve saved it you’ll need to make it executable and then you can run it. If everything goes well you should see something like:
username@host:~> chmod +x ~/bin/proximity.sh
username@host:~> ~/bin/proximity.sh
Monitoring proximity of "BlackBerry 8100" [00:0F:46:FF:05:07]
state = near, RSSI = -5
state = near, RSSI = -4
state = near, RSSI = -4
state = near, RSSI = -4
You’ll probably want to adjust the THRESHOLD depending on how sensitive your phone is and tweak other values but if it works you’ve got you’re very own proximity badge that lets your computer know when you’re around and when you’re away.
Updated [August 27, 2007]
Thanks to Lars for submitting the comment about the BlueProximity Project. You are all invited to check it out.

March 5th, 2007 at 5:03 pm
Wow!!! That is awesome!!! Can’t wait to try this out.
Cheers.
May 22nd, 2007 at 2:20 pm
RSSI is always 0, no matter how far I go
August 14th, 2007 at 9:03 am
Just wanted to point you to my project
http://blueproximity.sourceforge.net
which is similar to your script but it is all python and has a nice gui
(probably it wouldn’t help with kriko’s problem)
August 16th, 2007 at 10:29 am
Make this a simple download for XP and I will buy it.
October 18th, 2007 at 2:27 am
Guys, I trying out the script given above (but not getting result), need to know whether it works on bluetooth 1.1. Running on Suse 10.1
November 12th, 2007 at 9:16 am
Windows alternative.. Written in python using the pybluez library. Lock/Unlock Windows desktop with bluetooth proximity. http://www.blingcrazy.com/index.php?entry=entry070913-050958