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
- 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

65
bt
View File

@ -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<NF; i++) printf $i " "; print $NF}' | grep -vf /tmp/paired_devices )"
rm -f /tmp/paired_devices
choice=$( (echo "$new_devices" && echo 'rescan' ) | dmenu -l 10 -i -p 'pair with which device?' )
if [ -n "$choice" ]; then
if [ "$choice" = "rescan" ]; then
start_scan="$( date +'%s')"
pair
fi
bt_IDS="$( bluetoothctl devices | awk '{print $2}' )"
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}' )
bluetoothctl pair "$dev_id" && sleep 2
bluetoothctl connect "$dev_id"
#if device is not already hard coded as paired, add to paired devices list
#echo grep: "$( grep "$dev_id" "$PAIRLIST" )"
if grep -q "$dev_id" "$PAIRLIST"
then
echo Device "$dev_id" "$choice" >> "$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<NF; i++) printf $i " "; print $NF}' | grep -vf /tmp/paired_devices )"
rm -f /tmp/paired_devices
choice=$( echo "$new_devices" | dmenu -l 10 -i -p 'pair with which device?' )
if [ -n "$choice" ]; then
bt_IDS="$( bluetoothctl devices | awk '{print $2}' )"
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}' )
bluetoothctl pair "$dev_id" && sleep 2
bluetoothctl connect "$dev_id"
#if device is not already hard coded as paired, add to paired devices list
#echo grep: "$( grep "$dev_id" "$PAIRLIST" )"
if grep -q "$dev_id" "$PAIRLIST"
then
echo Device "$dev_id" "$choice" >> "$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}')