Here's the instruction for everyone who wants to make Bluetooth Serial communication to work without dealing with Python or other piggybacked programs running inside Raspberry Pi Model 3 B+ or 4 and Pi Zero easily.
- This configuration (below) works well with Android (old Android with BT 4.0 or later) and iPhone (8 or newer) with Bluetooth 4.0 or higher, using the Bluetooth Serial app for Android/IOS. Windows 10 notebook with Bluetooth and Bluetooth app will work well too.
- Treat RPi bluetooth serial device the same as other RPi serial ports, which would be useful when it comes to development (Python, C, etc.).
- With your Python/C/whatever program with serial routines, you can add username/password or token key or whatever for authentication for accessing RPi serial port (for others who are concerned about security).
- Make sure that your Raspberry Pi is freshly installed with latest O/S (as of March 2021) first before applying the configuration settings.
- This configuration took a long time to develop with hundreds of trials and errors to make it working correctly.
Known RPi serial ports:
Name Device Path name
Bluetooth Serial /dev/rfcomm0
TTL TX/RX /dev/serial0
Note: You will need to disable TTL TX/RX serial console and enable it via raspi-config
Note: USB0-USB3 - using USB-serial adapter.
Bluetooth System Settings for Bluetooth Serial Communication
Important Note: The depreciated functions as listed here:
hciattach hciconfig hcitool hcidump rfcomm sdptool ciptool gatttool
as described from bluez.git may not happen. Reason: That link was posted 4 years ago, which I do not believe it will happen at all.
1. Install Minicomm
sudo apt-get install minicom -y
Minicom serial terminal program is useful for testing the serial communication between Bluetooth serial app installed the phone and RPi serial.
2. Enable SPP on Raspberry Pi
In order to use SPP, Bluetooth service needs to be added/restarted with ‘compatibility’ flag.
Open Bluetooth service configuration file.
sudo nano /etc/systemd/system/dbus-org.bluez.service
Look for a line starts with “ExecStart” and add compatibility flag ‘-C’ at the end of the existing line as shown here:
Add a new line below immediately after the “ExecStart” line (see above):
ExecStartPost=/usr/bin/sdptool add SP
Save / close the dbus-org.bluez.service file.
Then reload the configuration file.
sudo systemctl daemon-reload
Then restart the Bluetooth service.
sudo systemctl restart bluetooth.service
3. Auto-Pairing Setup
Open rc.local file to add the services that will start at boot:
sudo nano /etc/rc.local
The new commands in rf.local will make bluetooth discoverable and listen for the incoming connections automatically. (no need to deal with MAC address!)
Copy the below commands and paste it in the file (at end of file, just before exit 0 line)
sudo bluetoothctl <<EOF
sudo rfcomm watch hci0 &
then save rc.local.
Then, edit Bluetooth main.conf file:
sudo nano /etc/bluetooth/main.conf
Un-comment both lines:
then save main.conf.
Then reboot the Raspberry Pi system to update the system settings for Bluetooth services.
After reboot, open your Bluetooth Serial Terminal app on your phone, then pair it, then connect it.
Then, in Raspberry Pi online, launch minicom
sudo minicom -b 19200 -o -D /dev/rfcomm0
You should be able to send/receive the messages from both ends (BT Serial Terminal app and Minicom terminal screen).