#!/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.*\" >/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