Setting Up Bluetooth Serial Port Profile on Raspberry Pi using D-Bus API

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

The goal is to establish an SPP connection between Raspberry Pi 3 and Android phone, then send and receive texts 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 2nd option (D-Bus API).

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

 

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
2-1. Create a python script (e.g. “spp.py”)[3][4].

import dbus, time

service_record = """
<?xml version="1.0" encoding="UTF-8" ?>
<record>
  <attribute id="0x0001">
    <sequence>
      <uuid value="0x1101"/>
    </sequence>
  </attribute>
  <attribute id="0x0004">
    <sequence>
      <sequence>
        <uuid value="0x0100"/>
      </sequence>
      <sequence>
        <uuid value="0x0003"/>
        <uint8 value="1" name="channel"/>
      </sequence>
    </sequence>
  </attribute>
  <attribute id="0x0100">
    <text value="Serial Port" name="name"/>
  </attribute>
</record>
"""

bus = dbus.SystemBus()
manager = dbus.Interface(bus.get_object("org.bluez", "/org/bluez"),
                        "org.bluez.ProfileManager1")
manager.RegisterProfile("/bluez",
                        "00001101-0000-1000-8000-00805f9b34fb",
                        {"AutoConnect":True, "ServiceRecord":service_record})
while True:
    time.sleep(1)

2-2. Run the script.

python spp.py

2-3. Suspend the script by pressing Ctrl+Z.

 

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[5] 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] Re: Bluetooth Python script. – Raspberry Pi Forum
[4] Tutorial: Creating a Bluetooth service
[5] Serial Bluetooth Terminal – Google Play

 

 

Sponsor Link

7 Comments

  1. I use Raspberry Pi 3, my work is sending a serial from the android phone to the Raspberry Pi through the bluetooth module HC-06. But I can connect the HC-06 with my Raspberry. Can you help me??

    1. Hi Duy,
      In this post, I’m using Raspberry Pi’s on-board Bluetooth module. I don’t have HC-06, so I don’t know what’s exactly required to connect SPP using HC-06, but I think at least you need to communicate with HC-06 module via UART. This post may be useful to establish UART connection between Raspberry Pi and the module.

  2. I am trying it on archlinux on orange pi plus 2e.
    when i do on bluetoothctl show. It does not show the registered service with the python script. Is that ok

Comments are closed.