1 Attachment(s)
Bluetooth proximity monitor
Hello,
I haven't found anything similar on this site, so I hope it might be useful.
I wrote a few scripts to automatically monitor bluetooth devices in range of BT USB stick attached to Asus router (for example mobile phones of people coming and leaving home or office). Devices can be in hidden mode.
!!! Some modifications according your own environment will be needed, mainly paths and usernames !!!
Requirements (see other related topics in this forum how to get it working):
- kernel modules for USB bluetooth stick loaded
- bluez-libs, bluez-utils (or bluez2-libs, bluez2-utils) packages installed
- sqlite2 (or sqlite) package
- procps (for pgrep used in startup script)
Main script blueproximity.sh can be run directly from command line or using simple script S99local:
Code:
#!/bin/sh
#
# Startup script for local custom services
case $1 in
start)
echo "Starting blueproximity.sh"
su pavel -c "exec nice sh ~pavel/btproximity/blueproximity.sh" >/dev/null 2>&1 &
;;
stop)
pid=`pgrep -f blueproximity.sh`
if [ -n "$pid" ] ; then
echo "Stopping blueproximity.sh"
kill $pid
fi
;;
restart)
$0 stop
sleep 1
$0 start
;;
*)
echo "usage: $0 (start|stop|restart)"
exit 1
;;
esac
Script doesn't require to be run under root privileges - I used my own account 'pavel'. It uses sqlite (version 2) for data storage - database has to be created in advance:
Code:
sqlite btlog.db < db.sql
Then it is neccessary to add one or more MAC addresses and names into 'devices' table. Value of 'enabled' allows to selectively disable or enable scanning of devices.
There are some variables that can be changed in the beginning of the script (blueproximity.sh):
Code:
treshold=2 # no of successive unsuccessful tries to treat device as out of range
cycletime=120 # duration of one cycle
treshold means that device will be logged as 'away' after two succesive unsuccessful attempts to find it.
cycletime=120 means that every enabled device will be scanned every 2 minutes.
Every device also has it's own directory (automatically created) which contain temporary state file. If you create a script run-in and/or run-out inside, it will be run on corresponding event with device name as argument. Additionally, 'run-in' gets second argument with minutes since last 'OUT' event.
Finally, script btlog.php is a script to display records from the database (tables status and log). It is very simple - if you develop more sophisticated version, possibly including management of devices in database, you're welcome to post it here ;-).
Script was tested under lighttpd and php-fcgi with extensions pdo.so and sqlite.so enabled in php.ini.
I hope it will be useful for somebody.
Paul