Wednesday, March 19, 2008

Get Bluetooth RSSI values using Python

The Received Signal Strength Indicator (RSSI ) is a measurement of the power present in a received bluetooth signal. The end-user can observe the RSSI value when measuring the signal strength of received bluetooth signal.

Some interesting Projects like BlueProximity (http://www.gnomefiles.org/app.php/BlueProximity) uses RSSI value at it’s core. 
This software helps you add a little more security to your desktop by detecting your mobile phone(bluetooth enabled) , and keeping track of its distance. If you move away from your computer and the distance is above a certain level (no measurement in meters is possible) for a given time, it automatically locks your desktop.

But even in that project, they doen’t measure RSSI using Python. Instead, that project depends on the hcitool to measure RSSI . The developer of Blueproximity (Lars) posted the same issue in Ubuntu Forum (http://ubuntuforums.org/showthread.php?t=528701). That’s how I decided to spend some time for it Embarassed

My first investigation was what the Perl guys do to measure RSSI. I got the answer from http://perl.jonallen.info/pub/Main/BluetoothProximityDetection/xscreensaver.pl . 
  Yes, They depend on C to measure the RSSI using inline C code in Perl. So I decided to move in the same
direction.

As far as I know, Swig is the best method to integrate C with Python (http://linuxgazette.net/issue49/pramode.html).  So , I decided to move in that direction. I have modified the C code in hcitool by Maxim Krasnyansky <maxk@qualcomm.com> and Integrated it with Python.

enerating the swig wrapper for bluessid.c

swig -python -module bluessid bluessid.c

gcc -I /usr/include/python2.4 -c bluessid.c bluessid_wrap.c

ld -shared -o _bluessid.so bluessid.o bluessid_wrap.o -lbluetooth

This will create the _bluessid.so in the present working directory.
Invoke python

import bluessid
help(bluessid)

The completed source code of Pyrssi is available at  

http://pysportslive.googlecode.com/svn/trunk/pyrssi/

Warning: Due to the non availabity of Bluetooth module, I haven’t tested this code. It is in Beta stage . So please dont complain if it cause an explosion in your PC or fried your bluetooth adapter Innocent 

Posted by maxinbjohn at 05:57:41 | Permalink | No Comments »