123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243 |
- #!/bin/bash
- CONFIG_FILE="$HOME/.blueproximity/blueproximity.conf"
- HCITOOL="/usr/bin/hcitool"
- RFCOMM="/usr/bin/rfcomm"
- L2PING="/usr/bin/l2ping"
- if [ ! -f "$CONFIG_FILE" ]; then
- [ -d `dirname $CONFIG_FILE` ] || mkdir `dirname $CONFIG_FILE`
- echo "Creating Default Profile..."
- hcitool scan
- read -p "Please Enter your Device's Bluetooth MAC Address: " device_mac_addr
-
- if [[ -z `echo $device_mac_addr | egrep "^([0-9a-fA-F]{2}:){5}[0-9a-fA-F]{2}$"` ]]; then
- echo "Invalid Bluetooth MAC Address"
- fi
-
-
- cat > $CONFIG_FILE << CEOF
- DEBUG_LEVEL=1
- DEBUG="/tmp/btproximity.log"
- DEVICE="$device_mac_addr"
- CHECK_INTERVAL_FAR=5
- CHECK_INTERVAL_NEAR=60
- THRESHOLD=-15
- FAILURES_THRESHOLD=2
- CONNECTION_METHOD=rfcomm
- CEOF
- echo "your config is stored to $CONFIG_FILE"
- echo "to check out configuration, type:"
- echo " cat $CONFIG_FILE"
- echo ""
- echo "Configuration Finished"
- echo "now you can edit or run this script again in background."
- exit 0
- fi
- source $CONFIG_FILE
- CHECK_INTERVAL=$CHECK_INTERVAL_FAR
- connected=0
- state="near"
- failures=0
- function far_execution {
-
- sflock
-
-
- }
- function near_execution {
-
-
-
- killall sflock
-
-
-
-
-
-
-
-
-
- }
-
-
-
-
-
-
-
- function msg {
- case $DEBUG_LEVEL in
- 1)
-
- echo "$@" >> "$DEBUG"
- ;;
- 2)
-
- echo "$@" | tee "$DEBUG"
- ;;
- 0|*)
-
- echo -n ""
- ;;
- esac
- }
- function check_connection {
- connected=0;
- found=0
- for s in `$HCITOOL con`; do
- if [[ `echo $s |grep $DEVICE` ]]; then
- found=1;
- fi
- done
- if [[ $found == 1 ]]; then
- connected=1;
- else
- msg 'Attempting connection...'
- case $CONNECTION_METHOD in
- hcitool)
- $HCITOOL cc $DEVICE
- CONNECTION_RESULT="`$HCITOOL con | grep $DEVICE`"
- ;;
- rfcomm|*)
- setsid $RFCOMM connect 0 $DEVICE 1&
- sleep 6
- CONNECTION_RESULT="`$RFCOMM show * | grep $DEVICE | grep connect`"
- ;;
- esac
- if [ -n "$CONNECTION_RESULT" ]; then
- msg 'Connected'
- connected=1;
- else
- if [ -z "`$L2PING -c 1 -t 2 -i $DEVICE 2>&1`" ]; then
- if [ -z "`$HCITOOL cc $DEVICE 2>&1`" ]; then
- msg 'Ping is good!'
- connected=1;
- else
- msg "ERROR: Could not connect to device $DEVICE."
- connected=0;
- fi
- fi
- fi
- fi
- }
- function is_far {
- if [[ "$state" == "near" ]]; then
- let "failures += 1"
- msg "*** Device \"$dev_name\" [$DEVICE] has left proximity, failures = $failures"
- if [[ $failures -ge FAILURES_THRESHOLD ]]; then
-
- state="far"
- far_execution
- fi
- CHECK_INTERVAL=$CHECK_INTERVAL_FAR
- msg "Current Interval=$CHECK_INTERVAL"
- fi
- }
- function is_near {
-
- failures=0
- if [[ "$state" == "far" ]]; then
- msg "*** Device \"$dev_name\" [$DEVICE] is within proximity"
- state="near"
- near_execution
- fi
- CHECK_INTERVAL=$CHECK_INTERVAL_NEAR
- msg "Current Interval=$CHECK_INTERVAL"
- proximity_execution
- }
- while [[ $connected -eq 0 ]]; do
- msg "init loop"
- check_connection
- sleep $CHECK_INTERVAL
- done
- dev_name=`$HCITOOL name $DEVICE`
- msg "Monitoring proximity of \"$dev_name\" [$DEVICE]";
- while /bin/true; do
- msg "execution loop"
- check_connection
- if [[ $connected -eq 1 ]]; then
-
- rssi=$($HCITOOL rssi $DEVICE | sed -e 's/RSSI return value: //g')
- if [[ $rssi -le $THRESHOLD ]]; then
-
- is_far
- else
-
- is_near
- fi
- msg "state = $state, RSSI = $rssi"
- else
-
- is_far
- msg "not connected"
- fi
- sleep $CHECK_INTERVAL
- done
|