[SOLVED] Pi3 Bluetooth Auto-Reconnect option / script?

Max2Play Home 2016 (en) Forums Max2Play on Raspberry PI [SOLVED] Pi3 Bluetooth Auto-Reconnect option / script?

This topic contains 2 replies, has 2 voices, and was last updated by  Heiner Moderator 2 weeks, 2 days ago.

Viewing 3 posts - 1 through 3 (of 3 total)
  • 4. July 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).
    EOF

    6. July 2017 at 13:36 #29838

    Hi.
    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
    EOF

    else
    # echo $(date) ” $power_state Power switch off ” >> /opt/squeezelite/log/btrecon.log
    bluetoothctl <<EOF
    disconnect CC:B1:1A:8C:45:E6
    EOF
    fi

    Works a treat!

    6. July 2017 at 14:35 #29842

    Hi 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.

Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic.

Register here