blob: 33256085fc05619c7d76dbee57204d938f4acc8d (
plain)
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
|
#!/usr/bin/env bash
set -e
die() {
echo "${1}"
exit 1
}
sigdwm() {
if [ "$XDG_SESSION_TYPE" = "wayland" ]; then
sigblock -b bt
else
which sigdwmblocks>/dev/null || return
pgrep dwmblocks>/dev/null || return
sigdwmblocks 4
fi
}
# Check if bluetoothctl is in $PATH
which bluetoothctl>/dev/null || die 'No bluetoothctl in $PATH'
# Check if bluetooth is blocked
(rfkill | grep -E "bluetooth.*\<blocked\>" >/dev/null) && echo "Please check rfkill and unblock bluetooth module." && exit || true
# If "stop" is the first argument, stop bluetooth service
[ "$1" = "stop" ] && systemctl stop bluetooth.service && sigdwm && exit
# Start bluetooth service if it's not started
systemctl is-active bluetooth.service >/dev/null || systemctl start bluetooth.service
# If the bluetooth module is powered off, power it on
[ "$(bluetoothctl show | awk '/Powered/ {print $2}')" = "no" ] && bluetoothctl power on && bluetoothctl agent on
# BTID regex (just a MAC address)
BTID='^([A-F0-9]{2}:){5}[A-F0-9]{2}$'
if [ $# -gt 0 ]; then
PAIRED=$(bluetoothctl devices Paired | grep "$1" | awk '/Device/ {print $2}')
if [ $(echo "$1" | grep -E $BTID) ]; then bluetoothctl connect $1
elif [ $(echo "$PAIRED" | grep -E $BTID) ]; then bluetoothctl connect $PAIRED
else bluetoothctl $*
fi
else # If no arguments are provided, print available devices
CONNECTED="$(bluetoothctl info | awk '/Name/ {print $2}')" || true
bluetoothctl devices Paired | awk "/Device/ {if(\"$CONNECTED\"==\$3) print \"\033[1;32m\" \$2 \"\t\" \$3 \"\033[0m\"; else print \$2 \"\t\" \$3;}"
fi
sigdwm
|