From 2736f9bd99c0c9ae8a93ad99a2197ddade26a89d Mon Sep 17 00:00:00 2001 From: Alexander Bocken Date: Sat, 4 Jul 2020 20:27:19 +0200 Subject: [PATCH] increased reliability of pairing and unpairing the pairlist now works again as intended. furthermore more correct handling for special cases like first/last device added. --- bt | 80 +++++++++++++++++++++++++++++++++++++------------------------- 1 file changed, 48 insertions(+), 32 deletions(-) diff --git a/bt b/bt index 7e0ac8a..4de5e55 100755 --- a/bt +++ b/bt @@ -1,5 +1,5 @@ -#!/bin/bash -#wait time to discover new devices in seconds +#!/bin/sh + 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) @@ -10,7 +10,7 @@ actions="pair disconnect unpair" -[ "$AUTOTRUST" = false ] && actions+=" +[ "$AUTOTRUST" = false ] && actions="${actions} trust" #Checks for necessary programs to be present. Very unlikely not to be present but let's just err on the safer side. @@ -28,7 +28,7 @@ power(){ } scan(){ scanstatus="$( bluetoothctl show | grep Discovering | awk '{print $2}' )" - if [ "$1" = "on" ]; then + if [ "$1" = on ]; then #sets variable in case scanning was already on before the start of bt [ "$start_scan" = "" ] && start_scan="$( date +'%s' )" if [ "$scanstatus" = "no" ]; then @@ -81,35 +81,44 @@ pair(){ sleep "$sleep_period" fi - if [ $paired_devices = "" ]; then - echo "$paired_devices" > /tmp/paired_devices - new_devices="$( bluetoothctl devices | awk '{for (i=3; i /tmp/paired_devices$$ + filtered_devices="$( echo "$all_devices" | grep -vf "/tmp/paired_devices$$")" + bt_IDS="$( echo "$filtered_devices" | awk '{print $2}' )" + new_devices="$( echo "$filtered_devices" | awk '{for (i=3; i> "$PAIRLIST" + else + echo all:"$all_devices" + echo choice:"$choice" + echo new:"$new_devices" + dev_no=$( echo "$new_devices" | nl | grep "$choice" | awk '{print $1}') + echo dev_no:$dev_no + dev_id=$( echo "$bt_IDS" | nl | grep -P "^.*$dev_no\t" | awk '{print $2}' ) + echo bt_IDS:"$bt_IDS" + echo devid:$dev_id + #only attempt to connect if pairing succeeds + bluetoothctl pair "$dev_id" && ( pair_succesful=true && bluetoothctl connect "$dev_id" ) || pair_succesful=false + if $AUTOTRUST; then + bluetoothctl trust "$dev_id" + fi + #if device is not already hard coded as paired, add to paired devices list + if [ "$( grep "$dev_id" "$PAIRLIST")" = "" ] || [ "$(wc -l "$PAIRLIST")" -eq 0 ] && $pair_succesful + then + echo to be added to "$PAIRLIST": + echo Device "$dev_id" "$choice" + echo Device "$dev_id" "$choice" >> "$PAIRLIST" + fi fi fi } @@ -127,7 +136,13 @@ case $choice in 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" + if [ "$new_paired_list" != "" ] + then + echo "$new_paired_list" > "$PAIRLIST" + else + rm -rf "$PAIRLIST" + touch "$PAIRLIST" + fi fi;; "trust") choice=$( echo "$paired_devices" | dmenu -l 10 -i -p 'remove which paired device?') if [ -n "$choice" ]; then @@ -144,9 +159,10 @@ case $choice in *) 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";; + [ "$dev_no" != "" ] && dev_id=$( echo "$bt_IDS" | nl | grep -P "^.*$dev_no\t" | awk '{print $2}') + if [ -n "$dev_id" ]; then + power on + bluetoothctl pair "$dev_id" + bluetoothctl connect "$dev_id" + fi;; esac -cleanup