This script will enable bluetooth and connect to your preferred bluetooth device and vice versa

Forum rules
Before you post please read how to get help
Post Reply
1337haXXor123
Level 1
Level 1
Posts: 7
Joined: Tue Jun 11, 2019 5:31 pm

This script will enable bluetooth and connect to your preferred bluetooth device and vice versa

Post by 1337haXXor123 » Mon Jun 17, 2019 6:04 am

Linux Mint 19.1 Cinnamon Edition.

For security I like to keep my bluetooth turned off when I'm not using it. I also like to close windows after I'm done with them. So whenever I want to send audio to my bluetooth receiver, it takes 7 steps:
  1. Click on Blueberry applet
  2. Click Open Bluetooth device manager
  3. In the newly opened window click the switch to turn on bluetooth
  4. Click on the device I want to connect to
  5. In the newly opened window click the switch to connect to the device
  6. Click x to close the device window
  7. Click x to close the bluetooth window
Disconnecting takes a similar amount of steps. (I can't just turn off bluetooth without disconnecting from the bluetooth receiver, or I won't be able to connect my phone to it until the connection times out on the receiver's end.)

So I wrote a script to automate this and created a keyboard shortcut to execute it. Now when I press Win+A all these steps happen in an instant :)

Code: Select all

#!/bin/bash

# Variables

ADAPTER_STATUS="$(rfkill list bluetooth -no soft)"
DEVICE="xx:xx:xx:xx:xx:xx" # copy and paste your bluetooth receiver's MAC-address from the window in step 5

# Functions

error_check() {
	if [ "$?" != 0 ]; then
		echo "Something went wrong."
		exit 1
	fi
}

enable_bluetooth() {
	echo "Enabling bluetooth..."
	rfkill unblock bluetooth
	error_check
}

disable_bluetooth() {
	echo "Disabling bluetooth..."
	rfkill block bluetooth
	error_check
}

connect() {
	echo "Connecting to device..."
	bluetoothctl <<- end
		connect "$DEVICE"
		quit
	end
}

disconnect() {
	echo "Disconnecting from device..."
	bluetoothctl <<- end
		disconnect "$DEVICE"
		quit
	end
}

# Program

if [ "$ADAPTER_STATUS" = "blocked" ]; then
	enable_bluetooth
	sleep 1
	connect
elif [ "$ADAPTER_STATUS" = "unblocked" ]; then
	disconnect
	sleep 1
	disable_bluetooth
else
	echo "Bluetooth adapter status unclear, not doing anything."
fi

cedricoola
Level 1
Level 1
Posts: 26
Joined: Mon Jun 17, 2019 4:02 pm

Re: This script will enable bluetooth and connect to your preferred bluetooth device and vice versa

Post by cedricoola » Wed Jun 26, 2019 2:39 pm

I had missed your post and posted a similar concern: viewtopic.php?f=212&t=296238
I was thinking more of usability with audio devices but secutity is also involved, indead.
And for a solution I was thinking of a toogle button easily accesible through a system tray icon. I used this in Ubuntu last year but can remember how I had installed it. Also, such an applet existed in previous versions of Mint, as the screenshot in my other post shows.
It seems to be a missing feature of Linux in general.
I don't understand why it is lacking today.

Now regarding your script. I don't know how to set it up. If not too complicated could you detail the steps?

1337haXXor123
Level 1
Level 1
Posts: 7
Joined: Tue Jun 11, 2019 5:31 pm

Re: This script will enable bluetooth and connect to your preferred bluetooth device and vice versa

Post by 1337haXXor123 » Tue Jul 02, 2019 2:38 pm

  1. copy/paste the code from my first post into a plain text editor
  2. copy/paste your bluetooth receiver's MAC-address per the instruction in the code
  3. save the file as e.g. conblue.sh
  4. make it executable through your file manager or by running chmod +x conblue.sh from a terminal while current working directory is the one where the file resides
  5. run it from a terminal to test it / see it in action
  6. now bind it to a shortcut key, e.g. in Cinnamon open Keyboard > Shortcuts and under Custom Shortcuts click Add custom shortcut.
Hope that makes it clear :)

Post Reply

Return to “Scripts & Bash”