From cf876823fb228617a9689fb20f691b42de3f80b6 Mon Sep 17 00:00:00 2001 From: Julian P Samaroo Date: Sat, 27 Mar 2021 11:10:39 -0500 Subject: [PATCH] Add scan/pair to BT menu Signed-off-by: Maarten van Gompel --- scripts/core/sxmo_bluetoothmenu.sh | 32 ++++++++++++++++++++++++++---- 1 file changed, 28 insertions(+), 4 deletions(-) diff --git a/scripts/core/sxmo_bluetoothmenu.sh b/scripts/core/sxmo_bluetoothmenu.sh index 5591684..a3e5f9f 100755 --- a/scripts/core/sxmo_bluetoothmenu.sh +++ b/scripts/core/sxmo_bluetoothmenu.sh @@ -1,9 +1,25 @@ #!/usr/bin/env sh +connect() { + if bluetoothctl info "$1" | grep Connected | grep -q no; then + bluetoothctl discoverable on + if bluetoothctl info "$1" | grep Paired | grep -q no; then + bluetoothctl pairable on + notify-send "Pairing with $1..." + bluetoothctl --timeout=10 pair "$1" || notify-send "Pairing failed" && return + bluetoothctl trust "$1" || notify-send "Trusting failed" && return + fi + bluetoothctl --timeout=5 connect "$1" || notify-send "Connecting failed" && return + fi +} + devicemenu() { + if bluetoothctl show | grep Powered | grep -q no; then + bluetoothctl power on + fi while true; do DEVICES="$(bluetoothctl devices | awk '{ $1=""; printf $2; $2=""; printf " ^" $0 "\n" }')" - ENTRIES="$(echo "$DEVICES" | sed 's|.* ^ ||' | xargs -0 printf "Close Menu\nDisconnect\n%s")" + ENTRIES="$(echo "$DEVICES" | sed 's|.* ^ ||' | xargs -0 printf "Close Menu\nDisconnect\nScan\n%s")" PICKED="$( echo "$ENTRIES" | @@ -14,12 +30,20 @@ devicemenu() { exit elif echo "$PICKED" | grep -q "Disconnect"; then st -e sh -c "bluetoothctl disconnect; sleep 1" - continue + elif echo "$PICKED" | grep -q "Scan"; then + notify-send "Scanning BT devices for 5 seconds..." + bluetoothctl --timeout=5 scan on else devicemac="$(echo "$DEVICES" | grep " \^ $PICKED$" | sed 's| ^ .*||' )" - st -e sh -c "bluetoothctl connect $devicemac; sleep 1" + st -e sh -c "$0 connect $devicemac" + bluetoothctl pairable off + bluetoothctl discoverable off fi done } -devicemenu +if [ -n "$1" ]; then + "$@" +else + devicemenu +fi