-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbluetooth_search.sh
executable file
·72 lines (70 loc) · 2.63 KB
/
bluetooth_search.sh
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
#!/bin/bash
#
#
# Find OBDLink Device on startup, add and connect
# Created by Christopher Phipps [email protected]
# Apr 28th 2014
#
process=$(/bin/ps -ef | /bin/grep -c bluetooth_search.sh)
if [[ $process -gt 4 ]]; then
logger Instance already running.. exiting... $process running
exit 0
fi
connectDevices=$(/usr/bin/bt-device --list | grep -c OBDLink)
if [ $connectDevices != 1 ]; then
logger No OBDLink device connected... trying to find it and add it...
while [ $connectDevices < 1 ]; do
findDevice=$(hcitool scan | grep OBDLink | grep -o -E '([[:xdigit:]]{1,2}:){5}[[:xdigit:]]{1,2}')
if [ "$findDevice" == "" ]; then
logger Nothing found.. sleeping and trying again
sleep 1
else
logger Found OBDLink Device with Mac: "$findDevice".. Attempting to add and connect with rfcomm
# Connect to the device without interaction, via bt-device
#yes 'yes' | /usr/bin/bt-device trusted $findDevice yes
#yes 'yes' | /usr/bin/bluez-test-device create $findDevice
connectDevices=$(/usr/bin/bt-device --list | grep -c OBDLink)
# Check if we successfully paired to the device without interaction
if [ $connectDevices != 0 ]; then
rfcomm connect /dev/rfcomm0 $findDevice 1 &
sleep 10
# Checking if we successfully connected via rfcomm
deviceMac=$(/usr/bin/bt-device --list | grep OBDLink | grep -o -E '([[:xdigit:]]{1,2}:){5}[[:xdigit:]]{1,2}')
rfcommConfirm=$(rfcomm -a | grep -c $deviceMac)
if [ "$rfcommConfirm" != "" ]; then
logger Successfully connected via RFCOMM
else
logger Failed to connect via RFCOMM. Please restart and try again
fi
else
logger Failed to add the device. Please restart and try again.
fi
fi
connectDevices=$(/usr/bin/bluez-test-device list | grep -c OBDLink)
done
else
logger OBDLink device is already added... Checking if we\'re connected...
deviceMac=$(/usr/bin/bt-device --list | grep OBDLink | grep -o -E '([[:xdigit:]]{1,2}:){5}[[:xdigit:]]{1,2}')
if [ "$deviceMac" != "" ]; then
rfcommConfirm=$(rfcomm -a | grep -c $deviceMac)
else
rfcommConfirm=0
fi
# Checking to see if we connected... if not, then we keep trying because we might be out of range
if [ "$rfcommConfirm" != 1 ]; then
echo "Not connected... trying to connect"
while [ "$rfcommConfirm" != 1 ]; do
rfcomm connect /dev/rfcomm0 $deviceMac 1 &
sleep 5
rfcommConfirm=$(rfcomm -a | grep -c $deviceMac)
if [ "$rfcommConfirm" != 1 ]; then
logger Failed to connnect to device.. Going to sleep and try again
sleep 5
else
logger Successfully connected $deviceMac to /dev/rfcomm0
fi
done
else
logger Device already connected on /dev/rfcomm0
fi
fi