Script to auto connect wireless controller on startup

I have got a Raspberry Pi4 last week and setup Lakka. I also purchased a DualShock 4 wireless controller. It is not PS4/Xbox, but I was able to pair and connect it. My trouble is that everytime I reboot the Pi, I have to SSH into it to repair and connect the controller. I was looking information on this and I know that after every reboot, the controller is not paired with the Pi, even though it is trusted. So I have to manually pair it, every time, and then connect it. I have also tried to create a simple script where it automatically pairs and connects with the controller.

Here is the script;

echo -e 'pair <Controller MAC>\nconnect <Controller MAC>' | bluetoothctl

I tested this and it works :+1: My problem is, is trying to get this to run whenever the Pi boots. Again doing my homework online, I have to create a .service in the /etc/systemd/system directory. But I am falling at the hurdle of creating the system directory as it says it is read-only. I don’t understand this as I have logged into the terminal as root user and the permissions is 644 and the owner is root.

Is there something I am missing or is there any other way that I can get my simple script to run at startup?

hi there. I also have just bought a rp4, i have a ps4 controller which works fine when connected, but i can’t figure out how to connect if via the bluetooth. Can you help? ive tried following the instructions. Your scrips seems like what i need, can you talk me through it? Where do i find my controller MAC?

Sure. I basically had my controller on and followed this article. You are going to have to get on the CLI for this. But follow the commands in the Pairing section of the article. Once your device is scanning it will pick up other device MAC address and display them on the screen as well as their bluetooth names. I was able to find my controller because it was called Wireless Controller, I assume all controllers are called that. Once you get the MAC address, pair with it, trust it and connect to it. That’s all there really is to it.

Trouble is (for me anyway), when you reboot your device, it won’t be paired to the controller so you will have to go onto the CLI every time to re-pair the controller manually and connect it. Hence my question to try and create a script that will manually run the commands for me and do it automatically on startup

thanks. So, after trying various things my ps4 controller connects to the bluetooth automagically. I now just press the playstation button, and it connects. i tried a variety of version of lakka, using the one below, i needed to only turn on the bluetooth in the settings and it worked. Perhaps that is helpful to someone

devel-20200213010100-db102f6 (RPi4.arm). Which is from the nightly builds /2020-02-13

So now that you have paired your controller once, does that automatically connect if you turn your Pi off and on?

Hi doldy101, I’ve also run into this problem after updating Lakka recently. Controllers worked fine before, but after update they would not stay paired. They are 8bitdo, but I imagine this will work for others too. Try the steps below and see if it works- good luck! It was super frustrating but now at least this first world problem is solved. :joy:

you will need to be able to ssh into your lakka box at a minimum. i also used a file browser connection to create and edit the file and look at stuff.

  1. enable check to see if autostart is enabled- systemctl status retroarch-autostart.service

if not, systemctl enable retroarch-autostart.service

  1. create/edit/touch/nano autostart here- /storage/.config/

  2. add this to autostart, edit in your mac address(es). i added 2 controllers and this works-

#!/bin/bash ( while (sleep 15) do bluetoothctl << EOF connect E4:17:D8:3F:09:69 connect E4:17:D8:D3:0B:75 exit EOF done ) &

  1. make autostart executable- chmod +x /storage/.config/

  2. reboot pi and try connecting controller(s). mine connect after reboots and after being turned on and off during the same session. may take a bit for the connection to happen, be patient.