Background: I have a Pi Zero that I want to send music to my bluetooth speakers. This works nicely when I have a monitor and can use bluetoothctl to pair and trust my speakers and the volume-adjuster's right-click menu to select my speakers. Due to the particular constraints of my project, however, I would like to connect to (and stream music over) a set of bluetooth speakers with a known MAC address at the push of a button.

Using GPIO and Python, I can read when that button is pushed and have set up Python code to run my program as desired, but I have to fill in the code that says "#CONNECT TO SPEAKERS HERE" and I've been stumped for a while.

Question: Knowing that I cannot provide input (ie a PIN or confirmation that I'd like to connect etc), is there a way for me to connect to speakers with a known MAC address at the push of a button?


There are a couple way you might want to approach this. If you are using an android device you can download an app called Script Kitty, and run the following code:

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

This is given that you have already paired and trusted the device.

Another option could be:

sudo rfcomm connect 0 AA:BB:CC:DD:EE:FF 1 &

If you want to use a python script and a button press you can include the following in your python script:

import subprocess
process = subprocess.Popen('sudo rfcomm connect 0 AA:BB:CC:DD:EE:FF 1 &',stdout = subprocess.PIPE,shell=True)
temp,err = process.communicate()

temp will hold the output of the file, which you don't necessarily need for this, but it can allow you to add some logic depending on what the output is. However, based on how bluetoothctl and rfcomm work you may not get any usable information back from running the command. If that is the case you can use the following:

import subprocess
process = subprocess.call('sudo rfcomm connect 0 AA:BB:CC:DD:EE:FF 1&',shell=True)


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

in place of rfcomm.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.