about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authornoahpy <noah.schlenker2002@gmail.com>2024-04-21 14:33:56 +0200
committernoahpy <noah.schlenker2002@gmail.com>2024-04-21 14:33:56 +0200
commit08be832de68821c51fcd7456095b15c51a311d9a (patch)
treedfa02342cc9f3c7f4f6509e96141d3e32726c12d
parent8aaf45eb682eb56b8836d368536bccb95661fa9f (diff)
downloadbluetoothctl-dmenu-08be832de68821c51fcd7456095b15c51a311d9a.tar.gz
bluetoothctl-dmenu-08be832de68821c51fcd7456095b15c51a311d9a.zip
review correction
-rwxr-xr-xbluetooth-devices.sh46
1 files changed, 30 insertions, 16 deletions
diff --git a/bluetooth-devices.sh b/bluetooth-devices.sh
index e8a7517..91c3ae9 100755
--- a/bluetooth-devices.sh
+++ b/bluetooth-devices.sh
@@ -1,8 +1,31 @@
 #
 # This program lists all paired devices which you can select from, the selected one
-# will be connected
+# will be connected / disconnected 
 #   
 
+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
 DEVICE=$(bluetoothctl devices | sed 's/[^ ]* //' | sed 's/[^ ]* //' | dmenu -i)
 
@@ -14,22 +37,13 @@ fi
 # Get MAC adress of the device you selected
 MAC=$(bluetoothctl devices | grep "$DEVICE" | sed 's/[^ ]* //' | cut -d ' ' -f1)
 
-# If bluetooth device is already connected, disconnect
+# If bluetooth device is already connected, disconnect, else connect
 CONNECTED=$(bluetoothctl devices Connected | cut -f3 -d ' ')
-if echo $CONNECTED | grep $DEVICE; then
-    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 
-    exit 0
-fi
-
-# Send a notify whether the connection was successful 
-if bluetoothctl connect $MAC | grep -q 'successful' 
+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
+
+