Fixed isseues raised by bash conversion, added a blacklist feature

This commit is contained in:
Alexander Bocken 2020-04-19 10:01:39 +02:00
parent 0b9421af97
commit 7520256f12

View File

@ -2,20 +2,22 @@
#wait time to discover new devices in seconds #wait time to discover new devices in seconds
SCAN_PERIOD=5 SCAN_PERIOD=5
BLACKLIST=$HOME/.local/bin/tools/bt_blacklist BLACKLIST=$HOME/.local/bin/tools/bt_blacklist
IDS=$(bluetoothctl paired-devices | awk '{print $2}') IDS=$(bluetoothctl paired-devices | awk '{print $2}')
echo $IDS > /tmp/bt_IDS echo $IDS > /tmp/bt_IDS
Devices=$(bluetoothctl paired-devices | awk '{for (i=3; i<NF; i++) printf $i " "; print $NF}' | sed 's/ $//') bluetoothctl paired-devices | awk '{for (i=3; i<NF; i++) printf $i " "; print $NF}' > /tmp/bt_devices
echo $Devices > /tmp/bt_devices Devices=$(cat /tmp/bt_devices)
Blacklist='MX Master\n' disp_devices=$(cat /tmp/bt_devices)
Devices=$( echo $Devices | grep -vf $BLACKLIST ) grep -vf "$BLACKLIST" /tmp/bt_devices > /tmp/disp_devices
actions="\nturn off\nturn on\nscan on\npair" len=$(cat /tmp/disp_devices | wc -l)
choice=$( printf "$Devices$actions" | dmenu -i -p 'What BT action would you like to perform:' ) actions="turn off\nturn on\nscan on\npair"
[ $len -gt 0 ] && choice=$( printf "$(cat /tmp/disp_devices)\n$actions" | dmenu -i -p 'What BT action would you like to perform:' )
[ $len -eq 0 ] && choice=$( printf "$actions" | dmenu -i -p 'What BT action would you like to perform:' )
cleanup(){ cleanup(){
rm -f /tmp/bt_devices rm -f /tmp/bt_devices
rm -f /tmp/bt_IDS rm -f /tmp/bt_IDS
rm -f /tmp/paired_devices rm -f /tmp/paired_devices
rm -f /tmp/disp_devices
exit exit
} }
@ -32,13 +34,13 @@ case $choice in
bluetoothctl scan on & disown bluetoothctl scan on & disown
notify-send "Bluetooth" "Searching for devices, please wait a bit" notify-send "Bluetooth" "Searching for devices, please wait a bit"
sleep $SCAN_PERIOD sleep $SCAN_PERIOD
all_devices=$(bluetoothctl devices | awk '{for (i=3; i<NF; i++) printf $i " "; print $NF}') bluetoothctl devices | awk '{for (i=3; i<NF; i++) printf $i " "; print $NF}' > /tmp/bt_devices
bluetoothctl paired-devices | awk '{for (i=3; i<NF; i++) printf $i " "; print $NF}' > /tmp/paired_devices bluetoothctl paired-devices | awk '{for (i=3; i<NF; i++) printf $i " "; print $NF}' > /tmp/paired_devices
new_devices=$(echo $all_devices | grep -vf /tmp/paired_devices) grep -vf /tmp/paired_devices /tmp/bt_devices > /tmp/new_devices
choice=$( echo $new_devices | dmenu -l 10 -p 'pair with which device?:' ) choice=$( cat /tmp/new_devices | dmenu -l 10 -p 'pair with which device?:' )
if [ -n "$choice" ]; then if [ -n "$choice" ]; then
bluetoothctl devices | awk '{print $2}' > /tmp/bt_IDS bluetoothctl devices | awk '{print $2}' > /tmp/bt_IDS
dev_no=$(echo $all_devices | cat -n | grep "$choice" | awk '{print $1}') dev_no=$(cat -n /tmp/bt_devices | grep "$choice" | awk '{print $1}')
dev_id=$(cat -n /tmp/bt_IDS | grep -E $dev_no"[[:space:]]" | awk '{print $2}' ) dev_id=$(cat -n /tmp/bt_IDS | grep -E $dev_no"[[:space:]]" | awk '{print $2}' )
bluetoothctl pair $dev_id && sleep 2 bluetoothctl pair $dev_id && sleep 2
@ -50,7 +52,7 @@ case $choice in
esac esac
if [ -n "$choice" ]; then if [ -n "$choice" ]; then
choice=$( echo $choice | sed 's/ $//') # choice=$( echo $choice | sed 's/ $//')
dev_no=$(cat -n /tmp/bt_devices | grep "$choice" | awk '{print $1}') dev_no=$(cat -n /tmp/bt_devices | grep "$choice" | awk '{print $1}')
dev_id=$(cat -n /tmp/bt_IDS | grep $dev_no | awk '{print $2}') dev_id=$(cat -n /tmp/bt_IDS | grep $dev_no | awk '{print $2}')
bluetoothctl power on bluetoothctl power on