1
1
#! /bin/bash
2
2
set -eu -o pipefail
3
3
4
+ # ##############################################################################
5
+ # script for doing stuff with bluetooth
6
+ # simplifies the process of connecting and disconnected bluetooth devices
7
+ # ##############################################################################
8
+
4
9
SCRIPTNAME=$( basename $0 )
5
10
6
11
SCANFILE=$( mktemp --tmpdir bluescanXXXXX)
@@ -20,6 +25,7 @@ function printusage {
20
25
echo " con|connect pair/connect a device"
21
26
echo " dis|disconnect disconnect a connected device"
22
27
echo " disall|disconnectall disconnect all connected devices"
28
+ echo " reset reset bluetooth kernel module"
23
29
echo " "
24
30
echo " options:"
25
31
echo " --scan [n] do a scan for n seconds (deafult 4) for devices"
@@ -31,15 +37,15 @@ function check_connected {
31
37
local sleep_time=" 0.3"
32
38
local err=1
33
39
while [[ $tries -lt $max_tries ]]; do
34
- sleep
35
40
if bluetoothctl info $1 | grep ' Connected: yes' > /dev/null; then
36
41
err=0
37
42
break
38
43
fi
39
44
tries=$(( ${tries} + 1 ))
40
- sleep 0.5
45
+ echo -n " ."
46
+ sleep 1
41
47
done
42
- return err
48
+ return $ err
43
49
}
44
50
45
51
set -- ` getopt -n$0 -u -a --longoptions=" scan help" " h" " $@ " `
@@ -150,22 +156,22 @@ elif [[ "$1" == "con" ]] || [[ "$1" == "connect" ]]; then
150
156
echo -n " already paired. connecting ${DEVICE} ..."
151
157
if ! bluetoothctl connect $chosen_id > /dev/null; then
152
158
# sometimes connecting reports as failed, but actually succeeds
153
- if check_connected " $chosen_id " ; then
159
+ if ! check_connected " $chosen_id " ; then
154
160
echo " could not connect"
155
161
exit 1
156
162
fi
157
163
fi
164
+ echo " done."
158
165
else
159
166
echo -n " not paired, pairing and connecting ${DEVICE} ..."
160
167
if ! bluetoothctl pair $chosen_id > /dev/null; then
161
168
# sometimes pairing reports as failed, but actually succeeds
162
- if check_connected " $chosen_id " ; then
169
+ if ! check_connected " $chosen_id " ; then
163
170
echo " could not connect"
164
171
exit 1
165
172
fi
166
173
fi
167
- echo " done"
168
-
174
+ echo " done."
169
175
fi
170
176
171
177
elif [[ " $1 " == " dis" ]] || [[ " $1 " == " disconnect" ]]; then
@@ -180,17 +186,28 @@ elif [[ "$1" == "dis" ]] || [[ "$1" == "disconnect" ]]; then
180
186
181
187
chosen_id=$( cat $IDFILE | awk ' $3 == "yes"' | tail -n+" $REPLY " | head -n1 | cut -f1)
182
188
echo -n " disconnecting $DEVICE ..."
183
- bluetoothctl disconnect $chosen_id
189
+ bluetoothctl disconnect $chosen_id > /dev/null
184
190
echo " done."
185
191
186
192
elif [[ " $1 " == " disall" ]] || [[ " $1 " == " disconnectall" ]]; then
187
193
for x in $( cat $IDFILE | awk ' $3 == "yes"' | cut -f 1) ; do
188
194
bluetoothctl disconnect $x
189
195
done
196
+ elif [[ " $1 " == " reset" ]]; then
197
+ # sometimes bluetooth gets into a bad state
198
+ # this lets us completely reset it
199
+ # see https://askubuntu.com/questions/1340713/bluetooth-service-failed-to-set-mode
200
+ sudo hciconfig hci0 down # Close HCI device.
201
+ sudo rmmod btusb # Remove btusb module from the kernel
202
+ sudo modprobe btusb # Add it back
203
+ sudo hciconfig hci0 up # Open and initialize HCI device
204
+ # bluetooth.service should be running at this point
205
+ if ! systemctl statu bluetooth | grep Running > /dev/null; then
206
+ echo " bluetooth service not running!"
207
+ exit 1
208
+ fi
190
209
else
191
210
echo " unknown command '$1 '"
192
211
exit 1
193
212
fi
194
213
195
- rm -f $SCANFILE $RAWIDFILE $IDFILE $PAIREDFILE
196
-
0 commit comments