Skip to content

Instantly share code, notes, and snippets.

@m0ngr31
Last active August 30, 2024 19:18
Show Gist options
  • Save m0ngr31/fc78c327f9aa62f39dda92e8d97b9b17 to your computer and use it in GitHub Desktop.
Save m0ngr31/fc78c327f9aa62f39dda92e8d97b9b17 to your computer and use it in GitHub Desktop.
Force Stadia Controller to connect in Linux

Using Google Stadia controllers with bluetooth is notoriously a pain. Some just won't reconnect after they've been paired initially. This script is an attempt to solve that issue in Linux. You can create a .desktop file and have it autostart when you boot your computer.

Every 60 seconds it will look at the list of the controllers that you have paired previously and compare that with the list of controllers that are currently connected. It will take the diff of that and attempt to connect to each one.

#!/bin/bash

# Function to get currently connected Stadia controllers
get_connected_controllers() {
    local devices="$1"
    while read -r mac; do
        if bluetoothctl info "$mac" | grep -q "Connected: yes"; then
            echo "$mac"
        fi
    done <<< "$devices"
}

while true; do
    # Get the list of all the previously connected Stadia controllers
    all_stadia_controllers=$(bluetoothctl devices | grep Stadia | awk '{print $2}')

    if [ -z "$all_stadia_controllers" ]; then
        echo "No Stadia controllers found."
        continue
    fi

    # Get the list of currently connected Stadia controllers
    connected_controllers=$(get_connected_controllers "$all_stadia_controllers")

    # Filter out connected controllers
    unconnected_stadia_controllers=$(comm -23 <(echo "$all_stadia_controllers" | sort) <(echo "$connected_controllers" | sort))

    # Check if any Stadia controllers were found
    if [ -z "$unconnected_stadia_controllers" ]; then
        echo "No unconnected Stadia controllers found."
        continue
    fi

    # Attempt to connect to each unconnected Stadia controller
    echo "Found the following unconnected Stadia controllers:"
    echo "$unconnected_stadia_controllers"

    while IFS= read -r mac; do
        timeout 5 bluetoothctl connect "$mac" > /dev/null 2>&1 || true
    done <<< "$unconnected_stadia_controllers"

    # Wait for 1 minute before running the loop again
    sleep 60
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment