How to automate connecting a bluetooth PS4 Controller to Renegade Board

Hi All, Hoping to get help from the community. I am not sure what I am doing wrong so any advice would be helpful.

Objective: I am trying to create a script that I can double-click to automatically connect my Bluetooth Gaming Controller (off brand ps4 controller) and launch emulationstation.

Script Requirements: The script will restart the Bluetooth service, wait for 1 second, turn on Bluetooth scanning, then remove any existing Bluetooth pairing with the MAC address 69:53:51:14:1D:39. It will check if the removal was successful and send a notification accordingly. Next, it will pair with the Bluetooth controller with the MAC address 69:53:51:14:1D:39, and check if the pairing was successful and send a notification accordingly. After waiting for 2 seconds, it will check if the controller is connected. If it is connected, it will show a notification and wait for 3 seconds before launching EmulationStation. If it is not connected, it will show a different notification and stop there.

Note that in order for this to work, I need to have the controller in pairing mode before I launch the script. This is a step I make sure is done.

Issue: The script can remove the pairing just fine but cannot pair or connect with the controller.

Observations: I can manually connect the controller when using the GUI system settings or if I use the command line and manually type in the commands “bluetoothctl” “pair 69:53:51:14:1D:39” “connect 69:53:51:14:1D:39”

Environment
ROC-RK3328-CC
ubuntu-22.04.1-preinstalled-desktop-arm64+roc-rk3328-cc.img.xz
llano Bluetooth Adapter for Computer (https://www.amazon.com/llano-Bluetooth-Driver-Free-Receiver-Controller/dp/B0B2W2LXZN)
onn. Wireless Keyboard and Mouse with Dual-Connectivity USB Receiver
PS4 Controller

Steps to create the script
Open a text editor and create a new file named bluetooth_controller.sh. I use nano
nano bluetooth_controller.sh

Add the below code:

#!/bin/bash

# Function to check if a Bluetooth device is paired
function is_paired {
    info=$(bluetoothctl info "$1" 2>&1)
    if [[ $info =~ "Paired: yes" ]]; then
        return 0
    else
        return 1
    fi
}

# Restart Bluetooth service
sudo service bluetooth restart

# Wait for 1 second
sleep 1

# Turn on Bluetooth scanning
echo -e "scan on\nquit" | bluetoothctl

# Wait for 5 seconds for devices to be discovered (you can adjust this value as needed)
sleep 5

# Remove existing Bluetooth pairing with MAC address 69:53:51:14:1D:39
echo -e "remove 69:53:51:14:1D:39\nquit" | bluetoothctl

# Wait for 1 second and check if the device was removed successfully
sleep 1
if is_paired "69:53:51:14:1D:39"; then
    # Show notification for unsuccessful removal
    notify-send "Bluetooth Controller" "Failed to remove pairing!"
    exit 1
else
    # Show notification for successful removal
    notify-send "Bluetooth Controller" "Pairing with 69:53:51:14:1D:39 removed successfully!"
fi

# Pair to the Bluetooth controller with MAC address 69:53:51:14:1D:39
echo -e "pair 69:53:51:14:1D:39\nquit" | bluetoothctl

# Wait for 1 second and check if the device was paired successfully
sleep 1
if is_paired "69:53:51:14:1D:39"; then
    # Show notification for successful pairing
    notify-send "Bluetooth Controller" "Paired with 69:53:51:14:1D:39 successfully!"
else
    # Show notification for unsuccessful pairing
    notify-send "Bluetooth Controller" "Failed to pair with 69:53:51:14:1D:39!"
    exit 1
fi

# Connect to the Bluetooth controller with MAC address 69:53:51:14:1D:39
echo -e "connect 69:53:51:14:1D:39\nquit" | bluetoothctl

# Wait for 2 seconds
sleep 2

# Check if the controller is connected
if [[ $(bluetoothctl info 69:53:51:14:1D:39 | grep "Connected: yes") ]]; then
    # Show connected notification
    notify-send "Bluetooth Controller" "Connected successfully!"
    # Wait for 3 seconds
    sleep 3
    # Launch EmulationStation
    emulationstation
else
    # Show not connected notification
    notify-send "Bluetooth Controller" "Not connected!"
fi

Save and exit the text editor. Then make the script executable.
chmod +x bluetooth_controller.sh

Driver and device enumeration is controlled by udev in Linux. You can create custom rules on device attach/detrach. Learn how it works and you don’t have to write a script for this.