-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProximityDetector
82 lines (67 loc) · 1.71 KB
/
ProximityDetector
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#!/bin/sh
DEVICE="xx:xx:xx:xx"
CHECK_INTERVAL=5
HCITOOL="/usr/bin/hcitool"
DEBUG="/home/raspberry/.log/btproximity.log"
function msg {
echo "$1" >> $DEBUG
}
function msgn {
echo -n "$1" >> $DEBUG
}
function check_connection {
connected=0;
found=0
for s in `$HCITOOL con`; do
if [[ "$s" == "$DEVICE" ]]; then
found=1;
fi
done
if [[ $found == 1 ]]; then
connected=1;
else
msgn 'Attempting connection...'
if [ -z "`sudo $HCITOOL cc $DEVICE 2>&1`" ]; then
msg 'Connected.'
connected=1;
else
msg "ERROR: Could not connect to device $DEVICE."
fi
fi
}
function start_mocp {
if ! ps aux | grep -w "mocp" | grep -v grep > /dev/null ; then
mocp -S
mocp -o shuffle
mocp -p
else
mocp -G
fi
}
check_connection
while [[ $connected -eq 0 ]]; do
check_connection
sleep 1
done
name=`$HCITOOL name $DEVICE`
msg "Monitoring proximity of \"$name\" [$DEVICE]";
state="NotDetectable"
while /bin/true; do
check_connection
if [[ $connected -eq 1 ]]; then
if [[ "$state" == "NotDetectable" ]]; then
rssi=`$HCITOOL rssi $DEVICE | sed -e 's/RSSI return value: //g'`
msg "*** Device \"$name\" [$DEVICE] is now detectable (signal: $rssi)"
state="Detectable"
start_mocp > /dev/null 2>&1
CHECK_INTERVAL=5
fi
elif [[ "$state" == "Detectable" ]]; then
msg "*** Device \"$name\" [$DEVICE] is no longer detectable"
state="NotDetectable"
start_mocp > /dev/null 2>&1
CHECK_INTERVAL=2
fi
msg "state = $state, PID = $PID, sleep = $CHECK_INTERVAL"
sleep $CHECK_INTERVAL
done