added immediate scan start on menu launch, rescan now available in paring menu

This commit is contained in:
Alexander Bocken 2020-06-09 18:20:51 +02:00
parent c667d9d602
commit 5729ed7c51
2 changed files with 47 additions and 26 deletions

View File

@ -4,7 +4,7 @@ A simple interactive tool to:
- pair new ones - pair new ones
- unpair already paired devices - unpair already paired devices
- turn bluetooth on/off - 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. 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 make install
``` ```
Afterwards, invoking `bt` will start the menu.
# Clear blacklist/pairlist # 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. 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. 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 To clear these files, simply run
```bash ```bash
make clear 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. 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 - auto-trust newly paired devices/trust device via dmenu
- blacklist devices via dmenu - blacklist devices via dmenu

45
bt
View File

@ -17,6 +17,10 @@ for prog in dmenu bluetoothctl awk cat; do
fi fi
done 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) #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}' )" bt_IDS="$( ( bluetoothctl paired-devices && cat "$PAIRLIST" ) | sort -u | awk '{print $2}' )"
@ -30,27 +34,29 @@ 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:' ) [ "$disp_devices" != "" ] && choice=$( echo "$disp_devices" "$actions" | dmenu -i -p 'What BT action would you like to perform:' )
cleanup(){ cleanup(){
bluetoothctl scan off > /dev/null bluetoothctl scan off
exit 0 exit 0
} }
case $choice in pair(){
"turn on") bluetoothctl power on #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.
cleanup;; bluetoothctl power on
"turn off") bluetoothctl power off bluetoothctl scan on &>/dev/null &
cleanup;; #check whether $SCAN_PERIOD seconds has already passed since starting scanning, if not, wait for the rest of that time.
"scan on") bluetoothctl power on && echo power on && sleep 2 if [ $((( "$(date +'%s')" - "$start_scan" ))) -lt $SCAN_PERIOD ]; then
([ -n "$TERMINAL" ] && $TERMINAL -e bluetoothctl scan on ) || st bluetoothctl scan on sleep_period="$((( "$SCAN_PERIOD" - "$( date +'%s')" + "$start_scan" )))"
cleanup;; notify-send "Bluetooth" "Searching for devices, please wait $sleep_period seconds"
"pair") bluetoothctl power on sleep "$sleep_period"
bluetoothctl scan on & disown fi
notify-send "Bluetooth" "Searching for devices, please wait a bit"
sleep $SCAN_PERIOD
echo "$paired_devices" > /tmp/paired_devices echo "$paired_devices" > /tmp/paired_devices
new_devices="$( bluetoothctl devices | awk '{for (i=3; i<NF; i++) printf $i " "; print $NF}' | grep -vf /tmp/paired_devices )" new_devices="$( bluetoothctl devices | awk '{for (i=3; i<NF; i++) printf $i " "; print $NF}' | grep -vf /tmp/paired_devices )"
rm -f /tmp/paired_devices rm -f /tmp/paired_devices
choice=$( echo "$new_devices" | dmenu -l 10 -i -p 'pair with which device?' ) choice=$( (echo "$new_devices" && echo 'rescan' ) | dmenu -l 10 -i -p 'pair with which device?' )
if [ -n "$choice" ]; then if [ -n "$choice" ]; then
if [ "$choice" = "rescan" ]; then
start_scan="$( date +'%s')"
pair
fi
bt_IDS="$( bluetoothctl devices | awk '{print $2}' )" bt_IDS="$( bluetoothctl devices | awk '{print $2}' )"
dev_no=$( echo "$new_devices" | nl | grep "$choice" | awk '{print $1}') dev_no=$( echo "$new_devices" | nl | grep "$choice" | awk '{print $1}')
dev_id=$( echo "$bt_IDS" | nl | grep -P "^.*$dev_no\t" | awk '{print $2}' ) dev_id=$( echo "$bt_IDS" | nl | grep -P "^.*$dev_no\t" | awk '{print $2}' )
@ -64,7 +70,18 @@ case $choice in
echo Device "$dev_id" "$choice" >> "$PAIRLIST" echo Device "$dev_id" "$choice" >> "$PAIRLIST"
fi fi
fi fi
cleanup
}
case $choice in
"turn on") bluetoothctl power on
cleanup;; cleanup;;
"turn off") bluetoothctl power off
cleanup;;
"scan on") bluetoothctl power on && echo power on && sleep 2
([ -n "$TERMINAL" ] && $TERMINAL -e bluetoothctl scan on ) || st bluetoothctl scan on
cleanup;;
"pair") pair;;
"unpair") choice=$( echo "$paired_devices" | dmenu -l 10 -i -p 'remove which paired device?') "unpair") choice=$( echo "$paired_devices" | dmenu -l 10 -i -p 'remove which paired device?')
if [ -n "$choice" ]; then if [ -n "$choice" ]; then
dev_no=$( echo "$paired_devices" | nl | grep -P "[0-9]+\t$choice$" | awk '{print $1}') dev_no=$( echo "$paired_devices" | nl | grep -P "[0-9]+\t$choice$" | awk '{print $1}')