Autoconnect to a bluetooth speaker in Ubuntu 16.04

BT is enabled at startup. This is nice. But it will not autopair/connect to a device, even if it is once paired and get status "trusted". I can connect to a Speaker with the tool by selecting device, and "connect to audio output". How can this done automatically on boot? I can do this also with "bluetoothctl" and than "connect ", but this is interactive, seems not scriptable.

And second step is, that BT becomes default audio sink.
Isn't that all an ordinary use case?

(I found some pages here, most are outdated.)Autoconnecting Bluetooth Devices: load-module module-switch-on-connect doesn't work for me.

I also installed bt-autoconnect. But several issues: - it didn't foun the BT-Adapter - Button Audio-Setting does just nothing - Save and quit doesn't just nothing

2

6 Answers

Try bluetoothctl command.

If you then enter help, you'll see the commands to be used.

  • 'list' (devices)
  • 'trust 78:44:aa:bb:cc:dd' (MAC address of device)
  • 'info 78:44:aa:bb:cc:dd' (MAC address of device)
  • 'paired-devices'

Try, it worked for me.

3

For automatic connect to Audio Sink you can add the following line in /etc/rc.local:

(sleep 6; echo "connect AA:BB:CC:DD:EE:FF\nquit" | bluetoothctl) &

Update the address. You can verify from command line by:

echo -e "devices\nquit" | bluetoothctl

In my case sleep 6 is enough - but maybe on your computer it has to be increased to allow other bluetooth connection steps to be fully completed.

Generally with & sign at the end you will start process which will be executed in 6sec not blocking next processes starting normal bluetooth connection functions. If the rc.local is not existing (it was in my case) you can create it or to initiate at startup with other file.

For the second part (auto switch to BT speaker as a sink) I found a solution. Has been already posted here:

# /etc/pulse/default.pa
.ifexists module-bluetooth-discover.so
load-module module-bluetooth-discover
load-module module-switch-on-connect # this is new!
.endif

BUT: How can the BT server automatically pair with the speaker, as soon as it is available? HELP!


Edit: found a (not the best imho) solution for the first question:

echo "connect AA:BB:CC:DD:EE:FF" | bluetoothctl

BUT: Even better would be that this works without this MAC for all ever devices that has been connected and trusted.

I wrote a simple script that will keep reconnecting the device when you reboot your PC or when the device toggles bluetooth status or lose a signal for a while.

#!/bin/bash
MAC="E0:CC:F8:E8:87:5D"
powered() { echo "show" | bluetoothctl | grep "Powered" | cut -d " " -f 2
}
connected() { echo "info ${MAC}" | bluetoothctl | grep "Connected" | cut -d " " -f 2
}
while true
do sleep 1 if [ $(powered) = yes ] && [ $(connected) = no ]; then echo "connect ${MAC}" | bluetoothctl sleep 5 fi
done
  1. Update MAC variable with your device's MAC address. (speaker, smartphone...)
    echo "devices" | bluetoothctl | grep Device
  2. Save the script for example as a ~/.bt-autoconnect.sh
  3. Make it executable chmod +x ~/.bt-autoconnect.sh
  4. Run it at login echo "~/.bt-autoconnect.sh &" >> "~/.xprofile"

In Ubuntu sound-problems are due to installed drivers. If this speaker is the newest and most trendiest hardware, you have no good chances and would have to wait 2 to 8 months until driver is present at Linux Community. But you could try to install following packages in terminal :

sudo apt-get install amarok rhythmbox

reboot

Then switch on bluetooth and see if your speaker is supported :

  • Open the Activities overview and start typing Bluetooth.
  • Click on Bluetooth to open the panel.
  • Set the switch at the top to ON.

Good luck or be patient for at maximum 8 months.

1

In my case this Script works (I tried all setting but not work for me,Step 5 from Here)

Python script on GitHub called bluetooth-autoconnect. It’s a python script that automatically connects to all paired and trusted Bluetooth devices. However, the script is not available to install directly on Flatpak or Apt repository. Hence, we need to manually download and configure the service.

download the zip file from GitHub or use the following command to clone the repository to your home directory.

git clone 

Now that we have the repository downloaded we need to move the service and scripts to their respective location before we start the service. In systemd architecture:-

sudo cp bluetooth-autoconnect/bluetooth-autoconnect.service /etc/systemd/system/
sudo cp bluetooth-autoconnect/bluetooth-autoconnect /usr/bin/

Once, we have successfully copied the service to the respective directories, let’s enable and start the service. To do that, use the following command.

sudo systemctl enable bluetooth-autoconnect.service
sudo systemctl start bluetooth-autoconnect.service

Now remove and reconnect and Done.

The only caveat with this method is that you won’t be able to pair your Bluetooth device with other systems without switching off your Linux machine’s Bluetooth.

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like