summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
author0scar <qgt268@alumni.ku.dk>2022-10-28 23:24:52 +0000
committer0scar <qgt268@alumni.ku.dk>2022-10-28 23:25:14 +0000
commit6fa8e6af2a8e1fdf0f27aee9e545180ec2c28e10 (patch)
treee7e0ea15ad759af492f557f70c8f9e17e26b11ab
parentc2e162a4ac179c12c1d4a8a2bfd236a615ff596c (diff)
Update bluetooth script to handle non-dwm dwm's
-rwxr-xr-x.local/bin/bt35
1 files changed, 25 insertions, 10 deletions
diff --git a/.local/bin/bt b/.local/bin/bt
index 9d40a67..bda82c9 100755
--- a/.local/bin/bt
+++ b/.local/bin/bt
@@ -2,30 +2,45 @@
set -e
+die() {
+ echo "${1}"
+ exit 1
+}
+
+sigdwm() {
+ which sigdwmblocks>/dev/null || return
+ sigdwmblocks 4
+}
+
+# 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
-[ "$1" = "stop" ] && systemctl stop bluetooth.service && sigdwmblocks 4 && exit
+# 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
- if [ "$#" -eq 1 ]; then
- PAIRED=$(bluetoothctl devices Paired | grep "$1" | awk '/Device/ {print $2}')
+ 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 bluetoothctl $*
+ if [ $(echo "$1" | grep -E $BTID) ]; then bluetoothctl connect $1
+ elif [ $(echo "$PAIRED" | grep -E $BTID) ]; then bluetoothctl connect $PAIRED
+ else bluetoothctl $*
fi
-else
+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
-sigdwmblocks 4
+sigdwm