aboutsummaryrefslogtreecommitdiff
path: root/bluetooth
diff options
context:
space:
mode:
authorInigoGutierrez <inigogf.95@gmail.com>2022-06-11 13:03:34 +0200
committerInigoGutierrez <inigogf.95@gmail.com>2022-06-11 13:03:34 +0200
commitbe61070d0cd1cbe1b25ef3cedd94dab40efd47c0 (patch)
treea0199a747666cdf942d25ba04359bfab5fc329f6 /bluetooth
parentc8d88c06c81f2e6102a22ab98aa28610da9ed86b (diff)
downloadscripts-be61070d0cd1cbe1b25ef3cedd94dab40efd47c0.tar.gz
scripts-be61070d0cd1cbe1b25ef3cedd94dab40efd47c0.zip
Created bluetooth/btConnect.sh to manage bluetooth connections with dmenu.
Diffstat (limited to 'bluetooth')
-rwxr-xr-xbluetooth/btConnect.sh43
1 files changed, 43 insertions, 0 deletions
diff --git a/bluetooth/btConnect.sh b/bluetooth/btConnect.sh
new file mode 100755
index 0000000..801fbf3
--- /dev/null
+++ b/bluetooth/btConnect.sh
@@ -0,0 +1,43 @@
+#!/bin/sh
+
+# btConnect.sh
+#
+# Selects an available device to connect to or disconnect from.
+
+title='btConnect.sh'
+
+if [ "$1" = '-d' ]; then
+ if ! bluetoothctl info | grep -Fq 'Connected: yes'; then
+ notify-send "$title" "Not connected to any device."
+ exit 4
+ fi
+ bluetoothctl disconnect
+ exit 0
+fi
+
+if ! systemctl show bluetooth | grep -Fq 'StatusText=Running'; then
+ sudo systemctl start bluetooth || exit 1
+ notify-send "$title" "Started bluetooth.service"
+fi
+
+if bluetoothctl show | grep -Fq 'Powered: no'; then
+ bluetoothctl power on || exit 1
+ notify-send "$title" "Bluetooth controller powered on."
+fi
+
+lines=$(bluetoothctl paired-devices | wc -l)
+selected=$(bluetoothctl paired-devices | dmenu -c -bw 3 -i -l $lines -p "💓") || exit 1
+mac=$(echo $selected | cut -d' ' -f2)
+name=$(echo $selected | cut -d' ' -f3-)
+
+if bluetoothctl info $mac | grep -F 'Connected' | grep -Fq 'yes'; then
+ notify-send "$title" "Already connected to ${name}\n(${mac})"
+ exit 2
+fi
+
+if ! bluetoothctl connect $mac; then
+ notify-send "$title" "Unable to connect to ${name}\n(${mac})"
+ exit 3
+fi
+
+notify-send "$title" "Connected to ${name}\n(${mac})"