summary refs log tree commit diff stats
path: root/bin/bluetooth-devices
diff options
context:
space:
mode:
authorChristian Krinitsin <code@krinitsin.xyz>2024-06-03 22:05:39 +0200
committerChristian Krinitsin <code@krinitsin.xyz>2024-06-03 22:05:39 +0200
commit4a719347c682dc51e8886292b1a19cb0c6a57741 (patch)
treecd285bf70e5cf39da7fdcdb9ca6120cdb75bfc08 /bin/bluetooth-devices
parent72ecba9ca9488ab0b95a2973077326ed9770a0cf (diff)
downloaddotfiles-4a719347c682dc51e8886292b1a19cb0c6a57741.tar.gz
dotfiles-4a719347c682dc51e8886292b1a19cb0c6a57741.zip
enhance latex support in nvim, rewrite bluetooth, add new gaps script, change waybar pacman segment
Diffstat (limited to 'bin/bluetooth-devices')
-rwxr-xr-xbin/bluetooth-devices39
1 files changed, 32 insertions, 7 deletions
diff --git a/bin/bluetooth-devices b/bin/bluetooth-devices
index 5cc4876..f5fe284 100755
--- a/bin/bluetooth-devices
+++ b/bin/bluetooth-devices
@@ -2,11 +2,34 @@
 
 #
 # This program lists all paired devices which you can select from, the selected one
-# will be connected
+# will be connected / disconnected 
 #   
 
-# Opens dmenu prompt, which lets you decide which device you want to connect to
-DEVICE=$(bluetoothctl devices | sed 's/[^ ]* //' | sed 's/[^ ]* //' | dmenu)
+connect_bluetooth() {
+    local MAC=$1
+    local DEVICE=$2
+    if bluetoothctl connect $MAC | grep -q 'successful' 
+    then
+        notify-send -t 5000 -r 2954 -u normal "  Connected successfully from" "     $DEVICE"
+    else 
+        notify-send -t 5000 -r 2954 -u normal "  Couldn't connect from" "     $DEVICE"
+    fi
+}
+
+
+disconnect_bluetooth() {
+    local MAC=$1
+    local DEVICE=$2
+    if bluetoothctl disconnect $MAC | grep -q 'Successful' 
+    then
+        notify-send -t 5000 -r 2954 -u normal "  Disconnected successfully from" "     $DEVICE"
+    else 
+        notify-send -t 5000 -r 2954 -u normal "  Couldn't disconnect from" "     $DEVICE"
+    fi
+}
+
+# Opens dmenu prompt, which lets you decide which device you want to connect to / disconnect from
+DEVICE=$(bluetoothctl devices | sed 's/[^ ]* //' | sed 's/[^ ]* //' | dmenu -i)
 
 # If dmenu was cancelled, exit program
 if [ $? -ne 0 ]; then
@@ -16,10 +39,12 @@ fi
 # Get MAC adress of the device you selected
 MAC=$(bluetoothctl devices | grep "$DEVICE" | sed 's/[^ ]* //' | cut -d ' ' -f1)
 
-# Send a notify whether the connection was successful 
-if bluetoothctl connect $MAC | grep -q 'successful' 
+# If bluetooth device is already connected, disconnect, else connect
+CONNECTED=$(bluetoothctl devices Connected | cut -f3 -d ' ')
+if echo $CONNECTED | grep $DEVICE
 then
-    notify-send -t 5000 -r 2954 -u normal "  Connected successfully to" "     $DEVICE"
+    disconnect_bluetooth $MAC $DEVICE
 else 
-    notify-send -t 5000 -r 2954 -u normal "  Couldn't connect to" "     $DEVICE"
+    connect_bluetooth $MAC $DEVICE
 fi
+