diff --git a/README.md b/README.md index d0ca894..b82e7a5 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@ A simple interactive tool to: - pair new ones - unpair already paired devices - turn bluetooth on/off -- blacklist paired devices to not be listed in connection selection (useful for auto-connecting devices like some mice) +- blacklist paired devices to not be listed in connection selection all via dmenu. Should be easily extendable by editing the `actions` string. @@ -17,11 +17,15 @@ Edit the Makefile to reflect your preffered installation destination. Then, simp make install ``` +Afterwards, invoking `bt` will start the menu. + # Clear blacklist/pairlist Since `bluetoothctl paired-devices` seems to be hugely unreliable in listing paired devices, bthandler has a seperate list for all devices paired through bthandler. There is also a blacklist available to not display certain devices, which might be useful for autoconnection Bluetooth mice for example. +*Note:* If for some reason `bluetoothctl paired-devices` works for you dont worry, bthandler lists all devices returned from `bluetoothctl` and it's own paired devices list. (Duplicates are not displayed.) + To clear these files, simply run ```bash make clear @@ -39,6 +43,6 @@ make uninstall Here's a growing list of features that are not yet actively worked on but might be nice in the future. Currently I'm more concerned with reliability than fulfilling feature requests, but I'm still open to put some on this list. -- start scanning for new devices immediately at execution to save time when pairing new devices +- start scanning for new devices immediately at execution to save time when pairing new devices -> Implemented (I think, seems to be working at least) - auto-trust newly paired devices/trust device via dmenu - blacklist devices via dmenu diff --git a/bt b/bt index 633fee5..3d63d5f 100755 --- a/bt +++ b/bt @@ -17,6 +17,10 @@ for prog in dmenu bluetoothctl awk cat; do fi done +#start scanning as early as possible to speed up pairing process: +bluetoothctl power on & bluetoothctl scan on &>/dev/null & +start_scan="$( date +'%s' )" + #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}' )" @@ -30,10 +34,45 @@ disp_devices="$( echo "$paired_devices" | grep -vf "$BLACKLIST" )" [ "$disp_devices" != "" ] && choice=$( echo "$disp_devices" "$actions" | dmenu -i -p 'What BT action would you like to perform:' ) cleanup(){ - bluetoothctl scan off > /dev/null + bluetoothctl scan off exit 0 } +pair(){ + #since this function can get called indefinitely, make sure to always be scanning and controller has power in the case that it got deactived by some other process. + bluetoothctl power on + bluetoothctl scan on &>/dev/null & +#check whether $SCAN_PERIOD seconds has already passed since starting scanning, if not, wait for the rest of that time. + if [ $((( "$(date +'%s')" - "$start_scan" ))) -lt $SCAN_PERIOD ]; then + sleep_period="$((( "$SCAN_PERIOD" - "$( date +'%s')" + "$start_scan" )))" + notify-send "Bluetooth" "Searching for devices, please wait $sleep_period seconds" + sleep "$sleep_period" + fi + echo "$paired_devices" > /tmp/paired_devices + new_devices="$( bluetoothctl devices | awk '{for (i=3; i> "$PAIRLIST" + fi + fi + cleanup +} + case $choice in "turn on") bluetoothctl power on cleanup;; @@ -42,29 +81,7 @@ case $choice in "scan on") bluetoothctl power on && echo power on && sleep 2 ([ -n "$TERMINAL" ] && $TERMINAL -e bluetoothctl scan on ) || st bluetoothctl scan on cleanup;; - "pair") bluetoothctl power on - bluetoothctl scan on & disown - notify-send "Bluetooth" "Searching for devices, please wait a bit" - sleep $SCAN_PERIOD - echo "$paired_devices" > /tmp/paired_devices - new_devices="$( bluetoothctl devices | awk '{for (i=3; i> "$PAIRLIST" - fi - fi - cleanup;; + "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}')