Create this script (I created it as /usr/local/bin/proximity.sh):

#!/usr/local/bin/bash

DEVICE="epa-palm"
USER="anderson"
CHECK_INTERVAL=2
THRESHOLD=200
PID=0
START_CMD='echo test'
FAR_CMD='daemon -f -u ${USER} /usr/local/bin/xflock4 -display localhost:0'
NEAR_CMD='/usr/bin/killall xlock'
#You really shouldn't kill -9 xscreensaver.
#From help: (Note that one must *never* kill xscreensaver with -9!)
HCITOOL="/usr/sbin/hccontrol"
DEBUG="/var/log/btproximity.log"
 
connected=0
 
function msg {
    echo "$1" >> $DEBUG
}
 
function check_connection {
    connected=0;
    found=0
    CONNECTION=`$HCITOOL -n ubt0hci read_connection_list|grep ${DEVICE}|awk '{ print $2 }'`
    if [ "$CONNECTION"  == "" ]; then
            found=0;
    else
            found=1;
    fi
    if [[ $found -eq 1 ]]; then
        connected=1;
    else
       msg 'Attempting connection...'
       echo "$HCITOOL -n ubt0hci create_connection ${DEVICE} 2>&1"
        if [ -z "`$HCITOOL -n ubt0hci create_connection ${DEVICE}`" ]; then
            msg 'Connected.'
            connected=1;
        else
            msg "ERROR: Could not connect to device $DEVICE."
        fi
    fi
}
 
function check_xscreensaver {
    PID=`ps -au|grep [x]lock|awk '{print $1 }'`
}
 
while [[ $connected -eq 0 ]]; do
    check_connection
done
 
name=$DEVICE
msg "Monitoring proximity of \"$name\" [$DEVICE]";
 
state="near"
while /usr/bin/true; do
 
    check_xscreensaver
    check_connection
 
    if [[ $connected -eq 1 ]]; then
        rssi=`$HCITOOL -n ubt0hci get_link_quality $CONNECTION | grep quality | awk '{print \$3}'`
        if (( $rssi <= $THRESHOLD )); then
            if [[ "$state" == "near" ]]; then
                msg "*** Device \"$name\" [$DEVICE] has left proximity"
                state="far"
                $FAR_CMD > /dev/null 2>&1
            fi
        else
            if [[ "$state" == "far" ]]; then
                msg "*** Device \"$name\" [$DEVICE] is within proximity"
                state="near"
                $NEAR_CMD > /dev/null 2>&1
            fi
        fi
        msg "state = $state, RSSI = $rssi, PID = $PID"
    fi
 
    sleep $CHECK_INTERVAL
done

proximity.sh

 
bt_proximity.txt · Last modified: 2007/05/15 23:54 by anderson
 
Recent changes RSS feed Creative Commons License Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki