Help with script to maintain bluetooth connection
-
I've got a Raspberry Pi 3 loaded with RetroPie 3.8. I connected a cheap bluetooth gamepad that I was able to connect via bluetoothctl.
My issue is that if I walk away from the RPi for a few minutes, the gamepad goes to sleep, and will not reconnect when powered back on, forcing me to drop back to the command line and use bluetoothctl to reconnect.
I'd really like help writing a script that will periodically check to see if the gamepad is connected, and if not, attempt to reconnect.
Any help would be greatly appreciated! -
Might want to try these instructions, use the 8bitdo instructions
https://github.com/RetroPie/RetroPie-Setup/wiki/Setting-up-a-Bluetooth-controller
-
I followed those instructions exactly, and have had 100% success connecting the gamepad at startup. My issue is when the gamepad goes to sleep (say I get up to help my wife with something) when I come back and my gamepad has powered down due to inactivity, when I power it back up, it doesn't reconnect to the Pi.
At this point I have to either reconnect via the command line, or reboot the Pi. -
I think I may have found a solution. If someone with a little more knowledge than me can take a look at this and let me know what they think. Adding this to my already existing startup script:
#!/bin/bash
address="XX:XX:XX:XX:XX:XX"
while (sleep 1)
do
connected=$(hidd --show) > /dev/null
if [[ ! $connected =~ .${address}. ]] ; then
hidd --connect ${address} > /dev/null 2>&1
fi
done -
I suffer with this problem as well.
-
@JasperSlade Did you create those files listed in the guide?
-
So far I've done the following:
Created: sudo nano /bin/connect-bluetooth.sh
Added the following text:
#!/bin/bash
sudo bluetoothctl << EOF
power on
connect [MAC Address]
exit
EOFMade the file executable: sudo chmod +x /bin/connect-bluetooth.sh
Created a service file: sudo nano /etc/systemd/system/connect-bluetooth.service
Added the following text:
[Unit]
Description=Connect Bluetooth
[Service]
Type=oneshot
ExecStart=/bin/connect-bluetooth.sh
[Install]
WantedBy=multi-user.targetEnabled the service file: sudo systemctl enable /etc/systemd/system/connect-bluetooth.service
At this point, my gamepad connects at startup flawlessly. When the gamepad powers down, it fails to reconnect.
I'm hoping that if I add the following text to my connect-bluetooth.sh, it will continually check for my gamepad, and reconnect when needed. Hopefully that is, it looks like the below code is for an older version of RetroPie, one that utilizes bluez, where as my build has bluetoothctl instead(#!/bin/bash
address="XX:XX:XX:XX:XX:XX"
while (sleep 1)
do
connected=$(hidd --show) > /dev/null
if [[ ! $connected =~ .${address}. ]] ; then
hidd --connect ${address} > /dev/null 2>&1
fi
done) -
In /bin/connect-bluetooth.sh, you did put the Mac Address where it says right?
-
Yes. 85:55:08:78:17:D1
-
I found my solution. I made the following changes (in bold) to my /bin/connect-bluetooth.sh file.
#!/bin/bash
while (sleep 30)
do
sudo bluetoothctl << EOF
power on
connect [MAC Address]
exit
EOF
doneYou can change the sleep value to whatever you'd prefer. 30 seconds seemed to work out fine for me. If i start to get impatient, I'll change it down to 15.
-
@JasperSlade said in Help with script to maintain bluetooth connection:
I found my solution. I made the following changes (in bold) to my /bin/connect-bluetooth.sh file.
#!/bin/bash
while (sleep 30)
do
sudo bluetoothctl << EOF
power on
connect [MAC Address]
exit
EOF
doneYou can change the sleep value to whatever you'd prefer. 30 seconds seemed to work out fine for me. If i start to get impatient, I'll change it down to 15.
Nice solution! Could you add this to the wiki page? Setting up a Bluetooth controller
-
@JasperSlade said in Help with script to maintain bluetooth connection:
You can change the sleep value to whatever you'd prefer. 30 seconds seemed to work out fine for me. If i start to get impatient, I'll change it down to 15.
It'll sleep for 30 seconds during the boot?
-
@Rion it's a wiki. Anyone can add to it provided the content is valid.
Contributions to the project are always appreciated, so if you would like to support us with a donation you can do so here.
Hosting provided by Mythic-Beasts. See the Hosting Information page for more information.