Raspberry Pi Bluetooth Gamepad Setup

Setting up a Bluetooth gamepad on a Raspberry Pi may not be as straightforward as some were expected. This post documents how I set up a Bluetooth gamepad on a Raspberry Pi, and I hope it could help you if you encountered any difficulties when doing so.

Specifically, the gamepad I'm using is an Xbox controller. If you are using a joystick from other brands, there is a high chance that this post still works for you, but as I only have an Xbox controller, your mileage may vary.

1. Change Bluetooth Settings

The first thing to look at is /etc/bluetooth/main.conf. In my setup, I changed the following option values. The first five were in the General section and the last one was in the Policy section of this .conf file.

[General]
Class = 0x000100
ControllerMode = dual
FastConnectable = true
Privacy = device
JustWorksRepairing = always

[Policy]
AutoEnable=true

Then we can restart the bluetooth service or restart the pi.

sudo systemctl restart bluetooth
# or restart the pi
# sudo reboot

2. (Optional) Test

To test if everything works, we can use bluetoothctl to manually pair and connect to the joystick.

$ bluetoothctl
[bluetooth]# scan on
Discovery started
[NEW] Device AA:BB:CC:DD:EE:FF Controller Name
[bluetooth]# pair AA:BB:CC:DD:EE:FF
[bluetooth]# trust AA:BB:CC:DD:EE:FF
[bluetooth]# connect AA:BB:CC:DD:EE:FF

If there's still some issues connecting the joystick, then you may have a try to disable the Bluetooth ERTM feature.

echo <<EOF | sudo tee /etc/modprobe.d/bluetooth.conf
options bluetooth disable_ertm=1
EOF
sudo reboot

3. (Optional) Auto-connect Script

We can have an auto-connect script that tests whether the input device exists, and try to connect to the gamepad if the specified device is not presented.

#!/bin/sh
JS="$1"
ADDRESS="$2"
usage() {
	S="$(basename "$0")"
	echo "usage:  ${S} [input_name] [address]"
	echo "        ${S} js0 AA:BB:CC:DD:EE:FF"
	exit 1
}
if [ -z "${JS}"] || [ -z "${ADDRESS}" ]; then
	usage
fi

if [ -e "/dev/input/${JS}" ]; then
	echo "${JS} is connected"
else
	echo "try to connect ${ADDRESS}"
	echo "connect ${ADDRESS}" | bluetoothctl
fi

A cron task could be set to execute this script every minute. The following code shows how to auto-connect to a joystick at AA:BB:CC:DD:EE:FF, assuming only connect one gamepad to Raspberry Pi (thus testing /dev/input/js0).

* * * * * /usr/bin/autoconnect_joystick js0 AA:BB:CC:DD:EE:FF

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.