Bluetoothed penguin
Linux’s implementation of Bluetooth isn’t half bad, especially when used from the desktop along with kdebluetooth (or gnome’s equivalent if you’re that way inclined). The KDE version comes with the ability to run scripts on detection of Bluetooth enabled devices within range of your computer. I personally run my computer as a server, and like to run as little as possible from X (It’s started at runlevel 3). For this reason, I’ve decided to mimic what KDE does, and write my own scripts for detecting Bluetooth devices, running various programs on detection. I’ve set up scripts to do a number of things, including, downloading of photos from phone to computer when phone comes in range and setting of IM away messages. In order to do this, a few simple steps have to be performed.
Firstly ensure that Bluetooth is installed on your computer, and that it is working. I’m using Fedora Core 5 (FC5), so to do this is as simple as (assuming you have yum set up properly:
# yum install bluez-*
We don’t really need all of the packages here to do this, but you will no doubt find those that are installed useful.
Insert your dongle here (assuming you’re using a USB Bluetooth device, if Bluetooth is built in, ignore this next bit). You may, depending on how your computer is set up, have to reboot here – I have no idea how to detect added hardware! I’m sure there is a better solution to this, but unfortunately I don’t know what it is. Now test the Bluetooth, which should give you output similar to below.
$ hcitool dev
Devices:
hci0 11:22:33:44:55:66
Assuming you’ve got this far and everything is working, you can now try the more interesting bits. I’m going to assume that you’re wanting to connect a phone to your computer, and monitor the presence of the phone. If you’re intending on using something else (TomTom, GPS box, PDA, whatever), then just use that whenever I mention phone – simple. Turn on Bluetooth on the phone, and scan for the device from the computer
$ hcitool scan
Scanning ...
00:AA:BB:CC:DD:EE phone
This will give you the name, and the id of your phone (and also any other devices in range). Make a note of the ID/Mac address (the letters/numbers/colons bit), we’ll need this later. Now, what we’re going to use to detect the presence of your phone is this Mac address. If you scan specifically for the Mac address when the phone is in range, then the hcitool program returns the name of the phone, if the phone is out of range, then NULL is returned:
$ hcitool name 00:AA:BB:CC:DD:EE
phone
The results from this are best piped into a file, which can then be used to monitor the presence of your phone. A simple bash
script to do this might look like this:
#!/bin/bash
## usage:
## detectphone time mac output_file_name
## Where:
## time - Is the time in minutes between detection attempts
## mac - Is the mac address of the device to attempt to detect
## output_file_name is the filename to write the output to!
if [ ! -n "$3" ]
then
echo "usage: detectphone time mac outputfile &"
exit 2
fi
## Simply put the program into a loop, sleeping for the first argument each loop
## and looking for the device with the id equal to the second argument each loop.
## The output from this is saved to the third argument
while [ "$var1" != "end" ]
do
`hcitool name $2 > $3`
`sleep $1`
done
Next we need a script which will check for the content of the file produced above and for it to then produce an action. Note, we don’t put the above check in to the script, as it would mean that for each action you want to do, you would have to check for the presence of the phone, whereas doing the above, the phone is only checked for once, no matter how many scripts you have. The following scripts can be executed periodically, or could have a loop in like the above script.
#!/bin/bash
## This script should be reproduced as the top of all the scripts that you create.
## Usage:
## fileexists pathtodetectionfile
if [ ! -n "$1" ] # Ensure that the path to the file was passed
then
echo "usage: fileexists pathtodetectionfile"
exit 2
fi
if [ ! -e "$1" ] # Check if file exists.
then
echo "$1 does not exist.";
exit 2
fi
output=`ls -l "$1" | awk '{print $5}'`
if [ $output == 0 ]
then
# File length is 0, exit quietly
exit 0;
fi
## If the script reaches this point, then everything has worked, and the phone
## has been detected.
## Insert your application logic HERE ##
## Insert your application logic HERE ##
exit 0;
If you’d prefer a script that executes a particular function when the phone comes into range, or moves out of range, then the following will probably be useful.
I know this is a little open ended, and that I’m not really finished with this, but I felt the need to post this as it has been sitting on my computer for far too long.
No comments
Jump to comment form | comments rss [?] | trackback uri [?]