This guide provides steps to set up automatic Bluetooth pairing on a Raspberry Pi 5 without using pin codes for authentication. After these settings, your RPI becomes a bluetooth device like headset/speaker, where you can connect automatically without using any UI.
sudo apt-get install bluez && sudo apt-get install bluez-tools
NOTE: After installation, you can test no-pin connection manually via this command:
sudo bt-agent -c NoInputNoOutput
- Open dbus.org.bluez.service file.
sudo nano /etc/systemd/system/dbus.org.bluez.service
- Edit ExecStart line and add ExecStartPost line to the file.
...
ExecStart=/usr/libexec/bluetooth/bluetoothd -C
ExecStartPost = /usr/bin/sdptool add SP
...
Create an AutoStart folder in the Home directory.
mkdir ~/AutoStart
Next, create an executable script named bluetooth-pair.sh inside /Home/AutoStart.
touch /Home/AutoStart/bluetooth-pair.sh && chmod +777 /Home/AutoStart/bluetooth-pair.sh
Add your Bluetooth pairing script content to bluetooth-pair.sh file.
#!/bin/bash
sudo bluetoothctl << EOF
power on
discoverable on
pairable on
agent NoInputNoOutput
default-agent
scan on
EOF
sudo bt-agent -c NoInputNoOutput & sudo rfcomm watch hci0
To ensure this script runs automatically at every power cycle, add the following command to the /etc/rc.local file just before the Exit 0 command:
To edit the rc.local file, use:
sudo nano /etc/rc.local
While inside rc.local file, add sudo sh /home/pi/AutoStart/bluetooth-pair.sh line before Exit 0:
...
sudo sh /home/pi/AutoStart/bluetooth-pair.sh
Exit 0
NOTE: You can also add all commands above directly to the rc.local file, but this is more loosely-coupled way. This may also work in RPI 4.
NOTE: Added bt_writer.py and bt_reader.py files for online testing via Serial Bluetooth Terminal application in Android phones.