about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorckrinitsin <101062646+ckrinitsin@users.noreply.github.com>2024-04-22 11:19:42 +0200
committerGitHub <noreply@github.com>2024-04-22 11:19:42 +0200
commit0671d8c22465a3541fdac791a53944759e23e1c2 (patch)
tree721f4352615c91d68b3bd476d6ccd1988e94d2b2
parent43b7f8d910851970bc7eb5fcb43289d7943da0ae (diff)
parent6fc80fa87f5fab1f204c4d7bb18c2e02e97c288f (diff)
downloadbluetoothctl-dmenu-0671d8c22465a3541fdac791a53944759e23e1c2.tar.gz
bluetoothctl-dmenu-0671d8c22465a3541fdac791a53944759e23e1c2.zip
Merge pull request #1 from noahpy/main
Add disconnection from connected devices
-rwxr-xr-x[-rw-r--r--]bluetooth-devices.sh38
1 files changed, 32 insertions, 6 deletions
diff --git a/bluetooth-devices.sh b/bluetooth-devices.sh
index ead8cae..25819b3 100644..100755
--- a/bluetooth-devices.sh
+++ b/bluetooth-devices.sh
@@ -1,9 +1,32 @@
 #
 # 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
+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
@@ -14,10 +37,13 @@ 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
+
+