SUSE Conversations


How to Set Up a Bluetooth Proximity System



By: danman21

February 22, 2007 9:25 am

Reads:244

Comments:0

Rating:0

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. We’ll take a page from the Gentoo Wiki and use a script from Steven to get everything tied together. The directions here are for SLED 10 but should work equally well on any recent SUSE distribution.

To start with, we’ll need to pair our phone to our laptop. This can be more difficult than it sounds depending on your phone manufacture 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.

VN:F [1.9.22_1171]
Rating: 0.0/5 (0 votes cast)

Categories: Uncategorized

Disclaimer: As with everything else at SUSE Conversations, this content is definitely not supported by SUSE (so don't even think of calling Support if you try something and it blows up).  It was contributed by a community member and is published "as is." It seems to have worked for at least one person, and might work for you. But please be sure to test, test, test before you do anything drastic with it.

Comment

RSS