Max2Play Home › Forums › Max2Play on Raspberry PI › [SOLVED] Pi3 Bluetooth Auto-Reconnect option / script?
Tagged: Bluetooth Auto Connect
- This topic has 4 replies, 4 voices, and was last updated 3 years, 10 months ago by psleertouwer.
-
Posted in: Max2Play on Raspberry PI
-
4. Juli 2017 at 19:58 #29800
I have a Squeezelite instance running on m2p on a Pi3 connected to a Bluetooth speaker.
My Bluetooth speaker (a Samsung R1) has multiple inputs, plus times out after 15 mins or so of inactivity.
Currently, if I change the input on R1 away from Bluetooth, then back to Bluetooth (or if R1 times out, or I switch R1 off, then back on again) the connection is lost but does not automatically reconnect – I have to click Connect button again from Bluetooth tab on Max2Play web UI to get playing again.
Would it not be possible to have a script to run (via cron or otherwise) to reconnect automatically (to last or only paired/trusted device). I managed to create a short script as follows that does a reconnect when I run it (same as clicking Connect button), just not sure how to get it to run the whole time. Can this be added to Max2Play web interface as an option, or help to automate please?#!/bin/bash
bluetoothctl << EOF
connect XX:XX:XX:XX:XX:XX (MAC of already paired/trusted bluetooth device).
EOF6. Juli 2017 at 13:36 #29838Hi.
In case anyone is interested, I managed to solve this myself.
I had to:
1) Via Max2Play web UI, Update to Beta, then Update Squeezelite
– this was necessary to get the GPIO squeezelite build option that -S command line option depends on.
2) In Squeezelite advanced options, set Command Line Options to -B -S /opt/squeezelite/btrecon.sh
– this tells squeezelite to run my btrecon.sh script passing a 1 or 0 command line arg when player is switched on or off via iPeng / OrangeSqueeze.
3) Create the script below (and make executable) at /opt/squeezelite/btrecon.sh
– the script just runs bluetoothctl and either issues a connect or disconnect command to my BT speaker.#!/bin/bash
power_state=$1
if [ „$power_state“ = „1“ ]
then
# echo $(date) “ $power_state Power switch on “ >> /opt/squeezelite/log/btrecon.log
bluetoothctl <<EOF
connect CC:B1:1A:8C:45:E6
EOFelse
# echo $(date) “ $power_state Power switch off “ >> /opt/squeezelite/log/btrecon.log
bluetoothctl <<EOF
disconnect CC:B1:1A:8C:45:E6
EOF
fiWorks a treat!
6. Juli 2017 at 14:35 #29842Hi bfddld,
First of all, thanks for sharing your process and your solution in detail!
I was just about to answer your first post, but its great you got it working yourself. I still wanted to mention that the Plugin Builder is a great option to create solutions for something like this. If you are interested/open to checking this out, you could just share the .tar-file of the plugin with anyone who wants to have the same solution. Here are some more infos: https://www.max2play.com/en/developers-corner/
Nonetheless, great job! We will look into it in more detail as well.
8. Januar 2019 at 0:13 #43222Hi,
I think that auto connect funcionality is very useful under many conditions, for example with mobile phone which can be within range or not during the day.
Is it possible to get auto connect functionality by default in bluetooth plugin?
Thank you very much.
7. Dezember 2020 at 16:24 #50442I’ve added this to my pi zero. It only seems to work half. When I connect manually to my bluetooth headphone. I can playback music. If I Poweroff the script kicks in and disconnect so I can use my headphone with another device. But when I start playing music again it does not connect to the headphone.
I’ve enabled the logging function. It only logs when I’ve manually connected to the headphone. Once disconnected nothing seems to happen. -
You must be logged in to reply to this topic.