-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpiBluetoothSerialConfig.sh
More file actions
executable file
·119 lines (100 loc) · 3.47 KB
/
piBluetoothSerialConfig.sh
File metadata and controls
executable file
·119 lines (100 loc) · 3.47 KB
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#!/bin/bash
#Edit the target device name before attempting to run the script
TARGET_DEVICE_NAME=""
BLUETOOTH_SERVICE_FILE="/lib/systemd/system/bluetooth.service"
SERVICE_KEY="ExecStart"
SERVICE_KEY_POST="ExecStartPost"
COMPATIBLE_MODE="/usr/lib/bluetooth/bluetoothd -C\nExecStartPost=/usr/bin/sdptool add SP \&\& hciconfig hci0 piscan"
if [[ -z $TARGET_DEVICE_NAME ]]; then
echo "Please set the target device name to establish a connection"
exit 1
fi
if [[ $EUID -ne 0 ]]; then
echo "Please run the script as root"
exit 1
fi
installLibs() {
echo "Installing the bluetooth dev library"
INSTALL_DEV_FAIL=0
apt install libbluetooth-dev || INSTALL_DEV_FAIL=1
if [[ $INSTALL_DEV_FAIL -eq 1 ]]; then
echo "Failed to install dev tools for bluetooth"
exit 1
else
echo "Successfully installed the bluetooth dev library"
fi
}
setCompatibleFlag() {
echo "Adding compatible mode flag (-C) to Bluetooth service"
FLAG_FAIL=0
sed -i "/^\($SERVICE_KEY_POST\)/d" $BLUETOOTH_SERVICE_FILE || FLAG_FAIL=1
sed -i "s#^\($SERVICE_KEY\s*=\s*\).*#\1$COMPATIBLE_MODE#" $BLUETOOTH_SERVICE_FILE || FLAG_FAIL=1
if [[ $FLAG_FAIL -eq 1 ]]; then
echo "Error occured while setting the compatible flag, check if you have the permissions to edit the file"
exit 1
else
echo "Succesfully added the compatible flag"
fi
}
restartBluetoothService() {
echo "Restarting the bluetooth service"
RESTART_FAIL=0
systemctl restart bluetooth.service || RESTART_FAIL=1
systemctl daemon-reload || RESTART_FAIL=1
if [[ $RESTART_FAIL -eq 1 ]]; then
echo "Error occured while restarting the bluetooth service"
exit 1
else
echo "Successfully restarted the bluetooth service"
fi
}
setupBluetooth() {
echo "Setting up the bluetooth"
BLUETOOTH_SETUP_FAIL=0
hciconfig noauth || BLUETOOTH_SETUP_FAIL=1
coproc BLUETOOTH_PROC (bluetoothctl -a) || BLUETOOTH_SETUP_FAIL=1
echo -e 'power on\n' >&${BLUETOOTH_PROC[1]}
sleep 2
echo -e 'default-agent\n' >&${BLUETOOTH_PROC[1]}
sleep 2
echo -e 'discoverable on\n' >&${BLUETOOTH_PROC[1]}
sleep 2
echo -e 'pairable on\n' >&${BLUETOOTH_PROC[1]}
sleep 2
echo -e 'scan on\n' >&${BLUETOOTH_PROC[1]}
sleep 10
echo -e 'scan off\n' >&${BLUETOOTH_PROC[1]}
sleep 2
echo -e 'devices\n' >&${BLUETOOTH_PROC[1]}
sleep 2
IFS=' '
echo 'Pair to "raspberrypi" from the target device'
while read -ra output <&${BLUETOOTH_PROC[0]}; do
if [[ ${output[2]} == $TARGET_DEVICE_NAME ]]; then
echo "Found the target device with MAC address of ${output[1]}"
echo "Pairing with the target device"
echo -e "pair ${output[1]}\n" >&${BLUETOOTH_PROC[1]}
sleep 10
echo -e 'yes\n' >&${BLUETOOTH_PROC[1]}
sleep 2
echo -e "trust ${output[1]}\n" >&${BLUETOOTH_PROC[1]}
sleep 2
echo -e 'exit\n' >&${BLUETOOTH_PROC[1]}
else
echo "${output[@]}"
fi
done
hciconfig hci0 piscan || BLUETOOTH_SETUP_FAIL=1
sdptool add SP || BLUETOOTH_SETUP_FAIL=1
if [[ $BLUETOOTH_SETUP_FAIL -eq 1 ]]; then
echo "Error occured while restarting the bluetooth service"
exit 1
else
echo "Successfully restarted the bluetooth service"
fi
}
installLibs
setCompatibleFlag
restartBluetoothService
setupBluetooth
echo "Device is ready to communicate with serial over bluetooth"