View Single Post
Posts: 40 | Thanked: 43 times | Joined on Oct 2008
#5
I find there are a few things that don't work so well in this script. I am using it within ubuntu on my desktop, so perhaps that is part of the issue? Anyhow, here is a revised script with a few key changes:

1. It doesn't respond immediately to "too far" - it waits until the third consecutive reading. I found that in my setup, it was far too disruptive, since it occasionally detected "too far" without any real movement of the bluetooth device. I figure that if walking away from something, probably having three cycles is fine. On the flip side, on the first "near enough", it starts playing.

2. It uses RSSI instead of TPL. TPL seemed less responsive to the small distances over which I use the system.

3. Miscellaneous bash fixes, including "le" instead of "lte", since bash doesn't take "lte".

Code:
#/bin/bash
# Script to execute a command when going too far and one when being near enough from the other adapter
# When going too far
cmd1="rhythmbox-client --pause"
# When going too near                                                            
cmd2="rhythmbox-client --play" 
# The other endpoint
badr="00:19:1D:AB:98:23"
# Threshold level
lvl="-4"
# On startup, if the device is close enough, don't execute anything
on=1                                                                            
smooth=0

while /bin/true                                                                 
do
                l=`hcitool rssi ${badr} | tail -1 | cut -f 2 -d':'`
#                echo "Power=$l"                                                 
                if [ "$l" -le $lvl ] && [ $on == 1 ]
                then                                                            
			if [ $smooth == 2 ]
	                then
        	                echo "Too far"
                	        ${cmd1}
                        	on=0
				smooth=0
			else
				echo "Are we too far?"
				let smooth=smooth+1
			fi
                elif [ "$l" -gt $lvl ]
                then
			if [ $on == 0 ]
			then
	                        echo "Near enough"
        	                ${cmd2}
                	        on=1
			else
				smooth=0
			fi
                fi                                                              
                sleep 3                                                         
done
 

The Following User Says Thank You to tarek For This Useful Post: