#!/bin/sh # btConnect.sh # # Selects an available device to connect to or disconnect from. title='btConnect.sh' if [ "$1" = '-d' ]; then if ! bluetoothctl info | grep -Fq 'Connected: yes'; then notify-send "$title" "Not connected to any device." exit 4 fi bluetoothctl disconnect exit 0 fi if ! systemctl show bluetooth | grep -Fq 'StatusText=Running'; then sudo systemctl start bluetooth || exit 1 notify-send "$title" "Started bluetooth.service" fi if bluetoothctl show | grep -Fq 'Powered: no'; then bluetoothctl power on || exit 1 notify-send "$title" "Bluetooth controller powered on." fi lines=$(bluetoothctl paired-devices | wc -l) selected=$(bluetoothctl paired-devices | dmenu -l $lines -p "💓") || exit 1 mac=$(echo $selected | cut -d' ' -f2) name=$(echo $selected | cut -d' ' -f3-) if bluetoothctl info $mac | grep -F 'Connected' | grep -Fq 'yes'; then notify-send "$title" "Already connected to ${name}\n(${mac})" exit 2 fi if ! bluetoothctl connect $mac; then notify-send "$title" "Unable to connect to ${name}\n(${mac})" exit 3 fi notify-send "$title" "Connected to ${name}\n(${mac})"