SQLite Based Bluetooth Device Logger

Btsql is a simple Bluetooth device scanner and logger, originally written in Perl, but later I rewrote it in C to support multiple Bluetooth (HCI) devices.
The C source is very rough, but it basically works. :-)

All application logic resides within the SQLite3 database, that way the logger doesn’t need to do more than blindly inserting values in the database. A couple of triggers do the rest.


Download the source from here: btsql-0.3.1.tgz. Unpack the source, build and install:

    sh:~$ tar zxf btsql-0.3.1.tgz
    sh:~$ cd btsql; make
    sh:~/btsql$ sudo make install

After installing, you can run the logger using the btsqllogger command. Run it without arguments to get some help on the options it understands:

    sh:~$ btsqllogger -h

    btsql logger, version 0.3 - Hessel Schut, hessel@isquared.nl
    Usage: btsqllogger [options] <db path>

        -b <bdaddr>   use local device with bdaddr for scanning
        -h            display this message
        -i <interval> run repeatedly every <interval> seconds
        -q            be vewwy, vewwy quiet

To initiate a scan, just run the logger with the database path as argument, by default the database is in /var/lib/btsql/btsql.db. If any devices are found (and no -q argument is given), then it will output any found devices to STDOUT as well as logging them to the database:

    sh:~$ btsqllogger /var/lib/btsql/btsql.db
        00:01:02:03:04:05   foo bar

Whitout further options btsqllogger just uses the first available HCI device, usually hci0. To bind the scanner to a specific local HCI device, get the bdaddr of the local device using hcitool and use the -b flag as follows:<

    sh:~$ hcitool dev
        hci0    00:0B:5D:A5:C1:6B
        hci1    00:10:60:AB:67:9E

    sh:~$ btsqllogger /var/lib/btsql/btsql.db -b 00:10:60:AB:67:9E
         00:01:02:03:04:05       foo bar

The -i option is still a bit buggy because of some issues with SQLite locking that I have been too lazy to solve, so far. It would probably help if I actually could program at all in C, or any other language, for that matter. ;-) To run the logger repeatedly, create a crontab for btsqllogger as follows:

    sh:~$ vi /etc/cron.d/btsql

And insert the following:

*/1 * * * *   btsql     /usr/local/bin/btsqllogger -q /var/lib/btsql/btsql.db

If you like, you can create individual entries for each hci device on your system, using the -b flag.

You can then query the database to your heart’s content. In the sql directory in the source tarball you find some example SQL. For instance to see the devicess logged in the last run of the logger, you can use the btsql-last-seen.sql file like so:

    sh:~$ sqlite3 /var/lib/btsql/btsql.db &lt; btsql-last-seen.sql 

    name                 manufacturer        last seen          
    -------------------  ------------------  -------------------
    Nintendo RVL-CNT-01  Nintendo Co., Ltd.  2008-10-12 14:42:11

I usually use this script wrapped inside watch, tucked away in an xterm in a corner of my screen, to see my latest discoveries when commuting to work. :-)