Setting Up Bluetooth Serial Port Profile on Raspberry Pi using sdptool

This post shows steps to set up Bluetooth Serial Port Profile (or SPP)[1] on Raspberry Pi.

The goal is to establish SPP connection between Raspberry Pi 3 and Android phone, then send/receive text using serial terminal applications on both ends.

SPP is not available by default. There are two options to enable it in BlueZ (which is the default Bluetooth stack in Linux). This post shows steps for the 1st option (sdptool).

  1. sdptool
  2. D-bus API (please see this post for this option)

 

Prerequisites (parentheses indicate my environment)

 

Steps
1. Installation
1-1. Install a serial terminal application on Raspberry Pi. In this post, I’ll use minicom[2].

sudo apt-get install minicom -y

 

2. Enable SPP on Raspberry Pi
In order to use SPP, Bluetooth service needs to be restarted with ‘compatibility’ flag[3].

2-1. Open Bluetooth service configuration file.

sudo nano /etc/systemd/system/dbus-org.bluez.service

2-2. Look for a line starts with “ExecStart” and add compatibility flag ‘-C’ at the end of the line.

ExecStart=/usr/lib/bluetooth/bluetoothd -C

2-3. Add a line below immediately after “ExecStart” line, then save and close the file.

ExecStartPost=/usr/bin/sdptool add SP

2-4. Reload the configuration file.

sudo systemctl daemon-reload

2-5. Restart the service.

sudo systemctl restart bluetooth.service

 

3. Pairing
To establish a connection, Raspberry Pi and the phone need to be paired.

3-1. Launch bluetoothctl.

bluetoothctl

3-2. Enter below in order to be discovered from the phone.

discoverable on

3-3. On the phone, scan for Raspberry Pi and pair. You should be able to see something like below.

[CHG] Device XX:XX:XX:XX:XX:XX Paired: yes

3-4. Press Ctrl+D to quit.

 

4. Establishing Connection from Phone

4-1. Listen for incoming connection on Raspberry Pi.

sudo rfcomm watch hci0

4-2. Install and launch “Serial Bluetooth Terminal” app[4] on the phone.

4-3. In the app, go to “Device” menu and select Raspberry Pi. If everything goes well and the connection is established, you should be able to see like this:

$ sudo rfcomm watch hci0
Waiting for connection on channel 1
Connection from XX:XX:XX:XX:XX:XX to /dev/rfcomm0
Press CTRL-C for hangup

 

5. Connecting Serial Terminal on Raspberry Pi
5-1. Open another terminal and launch the serial terminal.

minicom -b 9600 -o -D /dev/rfcomm0

 

6. Test
6-1. Input some text on the phone.

You should be able to see the text on Raspberry Pi’s serial terminal.

6-2. Input some text back to the phone on Raspberry Pi.

You should be able to see the text on the phone.

 

References
[1] Serial_Port_Profile_(SPP) – Wikipedia
[2] minicom(1) – Linux man page
[3] sdptool is broken in Bluez 5 – Arch Linux
[4] Serial Bluetooth Terminal – Google Play
[5] Bluetooth issues – Raspberry Pi Forum

 

 

Sponsor Link

20 Comments

  1. Could you make a tutorial for setting up HM-10 as virtual serial port? I am unable to get it working using sdptool (says host is down, hcltool lescan or lecc says it’s ok). Firmware v540…

    1. Can you elaborate what you are trying to do? Are you trying to establish virtual serial port over BLE using HM-10 on Raspberry Pi? I think sdptool is for classic Bluetooth and not for BLE. You can communicate with HM-10 via UART and control it by using AT commands provided by HM-10. However, I don’t have HM-10 and not sure if it supports custom GATT profiles.

  2. Hi, great tutorial.
    How can I do the same using PC. I have a bluetooth dongle connected to my PC. I want to be to get the devices to talk to each other and exchange data.

    Thanks

    1. On Windows 10, run Settings, search for “Bluetooth & Other Devices”, Select Add Device button, then Bluetooth, then select the raspberrypi. The SPP device will then be enumerated at two COM ports – I have no idea why two appear (seems to be a Windows issue); in my experience only one of them will work.

      I use TeraTerm Pro as the terminal emulator in Windows. PuTTY is another option, but is truly ugly!

      1. … The two COM ports are one each for incomming and outgoing connections – the active one depends on which end initiates the connection I believe.

        Open the Windows Control Panel. In search box, type “Bluetooth”, and then click “Change Bluetooth settings”, select the COM Ports tab, and it will show which is which.

  3. Hello,

    I have a GPS running on /dev/serial0
    when I sudo bluetoothctl
    discoverable on
    I get No default controller available

    What do I need to do ?

  4. Can you clarify what parts of this are one-time only persistent operations and which I have to do for each connection session?

    How do I make it persistent, so the port can be connected at any time?

    I exited minicom (no mean feat in itself), and hung-up the rfcomm session and thereafter could not re-establish a connection – the /dev/rfcomm0 device not present it seems. rfcomm stops on “Waiting for connection on channel 1”.

  5. Hi,

    Great tutorial! It works on raspberry pi well. However, when I applied it into Jetson Nano (because there is no bluetooth module for jetson nano, I used bluetooth adapter), it could not work. If I edit /etc/systemd/system/dbus-org.bluez.service as you wrote in the tutorial, the bluetooth service fails to start. When I give the command “sudo rfcomm bind 0 00:04:3E:4B:32:40”, the error message araise which said “RFCOMM TTY support not available”. For my issue, do you have any suggestions?

  6. Hello, I’ve already enabled the SP profile and I’ve done a Python script using sockets to connect several sensors via Bluetooth. When I connect one sensor I receive the data correctly, but when I connect a second one they both work badly. I think it’s because I’m using the same port for both of them. How many serial ports does the Raspberry Pi have? Is it possible to enable more than one to connect several sensors via Bluetooth without that their data interfering with the other’s data?

  7. Hello, I’ve already enabled the SP profile and I’ve done a Python script using sockets to connect several sensors via Bluetooth. When I connect one sensor I receive the data correctly, but when I connect a second one they both work badly. I think it’s because I’m using the same port for both of them.
    How many serial ports does the Raspberry Pi have?
    Is it possible to enable more than one to connect several sensors via Bluetooth without that their data interfering with the other’s data?

  8. Hi, used your instruction for Bluetooth serial port profile on Raspberry 2B+ and it work great with some very old Bluetooth usb dongle, its amazing. Made a small android app, and now control led on my Raspbian and remote Arduino using LoRa radio. Thank You 🙂

  9. Hello,
    First of all, thanks for the tutorial. I have a follow up question:
    Is it possible to implement this project without pairing the device? The USB modul I use isn’t able to be paired. But it can be connected without pairing. I can allready use the serial bluetooth terminal on my phone to connect and talkto my modul. Now i would love to implement an aplication which can talk from a raspberry to my modul without pairing.
    The module i use is HC-08. (I know the pairing with HC-05 is possible, but sadly not with HC-08)

Comments are closed.