From d85c7caca73dbd7da5d5c35811ab5755fefd9cd1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kier=C3=A1n=20Meinhardt?= Date: Tue, 15 Sep 2020 20:33:08 +0200 Subject: [PATCH] feat(packages): improve dmenubluetooth --- packages/scripts/dmenubluetooth.sh | 55 ++++++++++++++++++++++++------ 1 file changed, 44 insertions(+), 11 deletions(-) diff --git a/packages/scripts/dmenubluetooth.sh b/packages/scripts/dmenubluetooth.sh index e646a7e..a8535db 100755 --- a/packages/scripts/dmenubluetooth.sh +++ b/packages/scripts/dmenubluetooth.sh @@ -1,22 +1,55 @@ #!/bin/sh # UI for connecting to bluetooth devices +set -efu bluetooth_notify() { notify-send --app-name=" Bluetooth" "$@" } -bluetoothctl --timeout 1 -- scan on +chose_device() { + # the output from `bluetoothctl {paired-,}devices` has a first column which always contains `Device` followed by a MAC address and the device name + cut -d ' ' -f2- | dmenu -i -l 5 -p "Bluetooth device" +} -bluetooth_devices="$(bluetoothctl devices | cut -d ' ' -f2-)" +bluetoothctl scan on & -chosen="$(echo "$bluetooth_devices" | dmenu -i -l 5 -p "Bluetooth device")" -chosen_name="$(echo "$chosen" | cut -d ' ' -f2-)" +case "$(printf "pair\nconnect\ndisconnect" | dmenu -i)" in + pair) + chosen="$(bluetoothctl devices | chose_device)" + chosen_name="$(echo "$chosen" | cut -d ' ' -f2-)" -bluetooth_notify "$chosen_name" "Connecting ..." + bluetooth_notify "$chosen_name" "Pairing ..." -if bluetoothctl connect "$(echo "$chosen" | cut -d ' ' -f1)" -then - bluetooth_notify "✔ $chosen_name" "Connected to device." -else - test "$chosen" && bluetooth_notify "❌ $chosen_name" "Failed to connect to device." -fi + if bluetoothctl pair "$(echo "$chosen" | cut -d ' ' -f1)"; then + bluetooth_notify "✔ $chosen_name" "Paired with device." + else + test "$chosen" && bluetooth_notify "❌ $chosen_name" "Failed to pair with device." + fi + ;; + + connect) + chosen="$(bluetoothctl paired-devices | chose_device)" + chosen_name="$(echo "$chosen" | cut -d ' ' -f2-)" + + bluetooth_notify "$chosen_name" "Trying to connect ..." + + if bluetoothctl connect "$(echo "$chosen" | cut -d ' ' -f1)"; then + bluetooth_notify "✔ $chosen_name" "Connected to device." + else # something was selected but it didn't work + test "$chosen" && bluetooth_notify "❌ $chosen_name" "Failed to connect to device." + fi + ;; + + disconnect) + chosen="$(bluetoothctl paired-devices | chose_device)" + chosen_name="$(echo "$chosen" | cut -d ' ' -f2-)" + + bluetooth_notify "$chosen_name" "Disconnecting ..." + + if bluetoothctl disconnect "$(echo "$chosen" | cut -d ' ' -f1)"; then + bluetooth_notify "✔ $chosen_name" "Disconnected from device." + else # something was selected but it didn't work + test "$chosen" && bluetooth_notify "❌ $chosen_name" "Failed to disconnect from device." + fi + ;; +esac