From 6fa8e6af2a8e1fdf0f27aee9e545180ec2c28e10 Mon Sep 17 00:00:00 2001 From: 0scar Date: Sat, 29 Oct 2022 01:24:52 +0200 Subject: Update bluetooth script to handle non-dwm dwm's --- .local/bin/bt | 37 ++++++++++++++++++++++++++----------- 1 file changed, 26 insertions(+), 11 deletions(-) (limited to '.local/bin/bt') 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.*\" >/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}') - - 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 $* + 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 +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 -- cgit v1.3