Thanks again... this was a really good start. What I found though, is that when your device goes out of range and drops the "rfcomm" connection, when it comes BACK in range again... the connection does not auto start again.
So what I needed to do is the following:
1) When the script first runs, attempt to connect to the bluetooth device:
Code: Select all
rfcomm connect 0 12:34:56:78:90:00 10 >/dev/null &
2) After a connection attempt, check the rssi level with the following:
If you get a valid response, then the device is connected and you mark a variable to keep track of your connection state so you don't attempt to connect over and over again:
3) When you finally get an "error" response from checking rssi levels, mark the device "NOT connected" and attempt to connect again.
Here is the code I've got written so far... it's sloppy and I'd invite anyone out there to clean it up and repost!
(this script is built to send me notification via Pushover.net when the device is in range vs. out of range, as well as update my home automation system)
Code: Select all
while [ 1 ]; do
cmdout=$(hcitool rssi $device)
btcurrent=$(echo $cmdout | grep -c "RSSI return value") 2> /dev/null
rssi=$(echo $cmdout | sed -e 's/RSSI return value: //g')
if [ $btcurrent = $notconnected ]; then
echo "Attempting connection..."
rfcomm connect 0 $device 1 2> /dev/null >/dev/null &
if [ $btcurrent = $connected ]; then
echo "Device connected. RSSI: "$rssi
if [ $btconnected -ne $btcurrent ]; then
if [ $btcurrent -eq 0 ]; then
if [ $btcurrent -eq 1 ]; then
It works like a gem so far... it connects and STAYS connected, connects in anywhere from 1 to 3 seconds when the device is finally back in range and reports the RSSI once a second. It's been running for a few days now and I've only had it "disconnect" and miss an RSSI while I was still home twice and the disconnect lasted for under 5 seconds.
My next plan will be to take the RSSI numbers, keep a running average of the last 30 seconds or so and use that to report the "approximate proximity" to the device. From there I will hopefully find a way to run updates from multiple bluetooth connections to the same device (at least 3) from either different bluetooth dongles, or most likely, multiple raspberry pi's around the house so I can "triangulate" where each device is in the house.
My goal is to give the ability to use "indoor location aware" type commands to my home automation system. Like, when using my XBMC remote app on our iPhones... rather than have to hit the settings button and select which XBMC (also running on Raspberry Pi) to connect to, I'll have a single DNS name called "xbmccontrol.myhouse.com" and when the proximity system thinks you've entered a particular room it will remap that DNS entry over to the IP of the XBMC you're near.
The same thing goes for lighting control using Siri-proxy... when you pick up your phone and say "turn the lights on" it will already know where you are, possibly even ASK you when it thinks you're between rooms and help update the control IP.