#!/bin/bash #wait time to discover new devices in seconds SCAN_PERIOD=5 AUTOTRUST=false #locations of blacklist and hard coded list of paired devices (watch out, need to modify Makefile as well if you want to change these values and still use make install) BLACKLIST=${XDG_DATA_HOME:-$HOME/.local/share}/bt/blacklist PAIRLIST=${XDG_DATA_HOME:-$HOME/.local/share}/bt/paired actions="pair disconnect unpair" [ "$AUTOTRUST" = false ] && actions+=" trust" #Checks for necessary programs to be present. Very unlikely not to be present but let's just err on the safer side. for prog in dmenu bluetoothctl awk cat date nl; do if ! hash "$prog" 2>/dev/null; then printf 'bthandler: %s: command not found\n' "$prog" >&2 exit 127 fi done power(){ powerstatus="$( bluetoothctl show | grep Powered | awk '{print $2}' )" [ "$powerstatus" = "no" ] && [ "$1" = on ] && bluetoothctl power on [ "$powerstatus" = "yes" ] && [ "$1" = off ] && bluetoothctl power off } scan(){ scanstatus="$( bluetoothctl show | grep Discovering | awk '{print $2}' )" if [ "$1" = on ]; then if [ "$scanstatus" = "no" ]; then bluetoothctl scan on & start_scan="$( date +'%s' )" fi elif [ "$1" = off ]; then if [ "$scanstatus" = "yes" ]; then bluetoothctl scan off start_scan="$( date +'%s' )" fi fi } #start scanning as early as possible to speed up pairing process #=> maybe use an option to do this? Otherwise ever invocation of bt powers on the controller. power on scan on #Compile list of all Bluetooth IDS of paired devices (from bluetoothctl and from hardcoded list) bt_IDS="$( ( bluetoothctl paired-devices && cat "$PAIRLIST" ) | sort -u | awk '{print $2}' )" #Compile list of all device Names of paired devices (from bluetoothctl and from hardcoded list) paired_devices="$( ( bluetoothctl paired-devices && cat "$PAIRLIST" ) | sort -u | awk '{for (i=3; i /tmp/paired_devices new_devices="$( bluetoothctl devices | awk '{for (i=3; i> "$PAIRLIST" fi fi } case $choice in "turn on") bluetoothctl power on;; "turn off") bluetoothctl power off;; "scan on") bluetoothctl power on && echo power on && sleep 2 ([ -n "$TERMINAL" ] && $TERMINAL -e bluetoothctl scan on ) || st bluetoothctl scan on;; "pair") pair;; "unpair") choice=$( echo "$paired_devices" | dmenu -l 10 -i -p 'remove which paired device?') if [ -n "$choice" ]; then dev_no=$( echo "$paired_devices" | nl | grep -P "[0-9]+\t$choice$" | awk '{print $1}') dev_id=$( echo "$bt_IDS" | nl | grep -P "^.*$dev_no\t" | awk '{print $2}' ) bluetoothctl remove "$dev_id" #remove device to unpair from hard coded paired devices list new_paired_list="$( grep -v "$dev_id" "$PAIRLIST" )" echo "$new_paired_list" > "$PAIRLIST" fi;; "trust") choice=$( echo "$paired_devices" | dmenu -l 10 -i -p 'remove which paired device?') if [ -n "$choice" ]; then dev_no=$( echo "$paired_devices" | nl | grep -P "[0-9]+\t$choice$" | awk '{print $1}') dev_id=$( echo "$bt_IDS" | nl | grep -P "^.*$dev_no\t" | awk '{print $2}' ) bluetoothctl trust "$dev_id" fi;; "disconnect") choice=$( echo "$paired_devices" | dmenu -l 10 -i -p 'remove which paired device?') if [ -n "$choice" ]; then dev_no=$( echo "$paired_devices" | nl | grep -P "[0-9]+\t$choice$" | awk '{print $1}') dev_id=$( echo "$bt_IDS" | nl | grep -P "^.*$dev_no\t" | awk '{print $2}' ) bluetoothctl disconnect "$dev_id" fi;; *) echo "$choice" dev_no=$( echo "$paired_devices" | nl | grep -P "[0-9]+\t$choice$" | awk '{print $1}') dev_id=$( echo "$bt_IDS" | nl | grep -P "^.*$dev_no\t" | awk '{print $2}') bluetoothctl power on bluetoothctl pair "$dev_id" bluetoothctl connect "$dev_id";; esac cleanup