bluetooth-devices.sh (view raw)
1#
2# This program lists all paired devices which you can select from, the selected one
3# will be connected / disconnected
4#
5
6connect_bluetooth() {
7 local MAC=$1
8 local DEVICE=$2
9 if bluetoothctl connect $MAC | grep -q 'successful'
10 then
11 notify-send -t 5000 -r 2954 -u normal " Connected successfully from" " $DEVICE"
12 else
13 notify-send -t 5000 -r 2954 -u normal " Couldn't connect from" " $DEVICE"
14 fi
15}
16
17
18disconnect_bluetooth() {
19 local MAC=$1
20 local DEVICE=$2
21 if bluetoothctl disconnect $MAC | grep -q 'Successful'
22 then
23 notify-send -t 5000 -r 2954 -u normal " Disconnected successfully from" " $DEVICE"
24 else
25 notify-send -t 5000 -r 2954 -u normal " Couldn't disconnect from" " $DEVICE"
26 fi
27}
28
29# Opens dmenu prompt, which lets you decide which device you want to connect to / disconnect from
30DEVICE=$(bluetoothctl devices | sed 's/[^ ]* //' | sed 's/[^ ]* //' | dmenu -i)
31
32# If dmenu was cancelled, exit program
33if [ $? -ne 0 ]; then
34 exit 1
35fi
36
37# Get MAC adress of the device you selected
38MAC=$(bluetoothctl devices | grep "$DEVICE" | sed 's/[^ ]* //' | cut -d ' ' -f1)
39
40# If bluetooth device is already connected, disconnect, else connect
41CONNECTED=$(bluetoothctl devices Connected | cut -f3 -d ' ')
42if echo $CONNECTED | grep $DEVICE
43then
44 disconnect_bluetooth $MAC $DEVICE
45else
46 connect_bluetooth $MAC $DEVICE
47fi
48
49