0

I managed to connect a sensor via Bluetooth to a serial port on the Raspberry Pi 4 by doing the following:

First, I added the SP profile to the Raspberry by opening and editing the following file:

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

I added the compatibility flag -C and a new line to add the SP profile lines:

ExecStart=/usr/lib/bluetooth/bluetoothd -C
ExecStartPost=/usr/bin/sdptool add SP

Then I paired and connected entering the following on the terminal:

bluetoothctl
pair <deviceAddress>
trust <deviceAddress>
exit

On a different terminal window, I entered:

   sudo rfcomm connect hci0 <deviceAddress> 

This connected the device, and then I opened a minicom window to see the data the sensor was sending by entering:

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

This worked fine, but now I want to connect another sensor to a different serial port (an eventually a third sensor), so that I can simultanously see the data the two sensors are sending on two different minicom windows. I managed to pair and trust the new sensor but when I execute the folling line it gives me an error:

   sudo rfcomm connect hci0 <deviceAddress> 

The error is: "Can't create RFCOMM TTY: Address already in use"

I guess the problem might be with the hci0 but I'm not sure, as I don't completely understand what that is. I tried to use channel 2 by adding a 2 to the prior line (I don't know if this makes sense) and it didn't work either ("Can't create RFCOMM TTY: Address already in use".)

I also tried to use another device, hci1 instead of hci0, and it says: "Can't connect RFCOMM socket: Host is down". When I run the folowing:

hcitootl dev

only hci0 appears, so I don't know if I should create another device to be able to connect another sensor or how it is done.

So, anyone knows a way to make several Bluetooth serial connections, so that I can see the data several sensors are sending at the same time on different minicom windows?

Thanks.

1 Answer 1

1

hci0 refers to the physical Bluetooth adapter/radio in your system. If you connect a Bluetooth USB dongle to your RPi, then you would see hci1. e.g.

pi@raspberrypi:~ $ bluetoothctl list
Controller 00:02:5B:33:44:07 RPi_UART [default]
Controller B8:27:EB:33:57:E0 SeeMe 

pi@raspberrypi:~ $ busctl tree 'org.bluez'
└─/org
  └─/org/bluez
    ├─/org/bluez/hci0
    └─/org/bluez/hci1

It would be best to avoid using bluetoothd -C, rfcomm and hcitool as they have been deprecated

As outlined in http://blog.kevindoran.co/bluetooth-programming-with-python-3/, a Serial Port Profile (SPP) Bluetooth client connection can be done with Python3 (after doing the pairing in bluetoothctl):

import socket

serverMACAddress = '00:1f:e1:dd:08:3d'
port = 3
s = socket.socket(socket.AF_BLUETOOTH, socket.SOCK_STREAM, socket.BTPROTO_RFCOMM)
s.connect((serverMACAddress,port))
while 1:
    text = input()
    if text == "quit":
        break
    s.send(bytes(text, 'UTF-8'))
s.close()

There is also a library like bluedot: https://bluedot.readthedocs.io/en/latest/btcommapi.html#bluetoothclient

There are a lot of pieces that come together to support a Bluetooth profile. There is the hardware, BlueZ stack etc and getting them all to line up can require effort. However, the specification for SPP is clear that while it is a point-to-point protocol, there is the expectation that there can be multiple SPP's running: enter image description here

SPP is a serial cable replacement. So I think of it as that you have to create multiple cables connections. I don't have the hardware here to test this but does the following work:

s1.connect((serverMACAddress_1,port))
s2.connect((serverMACAddress_2,port))
7
  • Thanks for your answer. I have a couple questions: 1. So if I want to connect a second sensor simultanously I would need a Bluetooth USB dongle? Is it not possible to use several serial ports simultanously on the Raspberry Pi? 2. Is it possible to use that Python program with the Thonny Python IDE that comes by default with the Raspberry Pi or do I need to download Python 3?
    – blt
    Nov 26, 2020 at 12:04
  • You should be able to have two connections at the same time without an additional dongle. But, how often is your sensor updating and do you need live updates? Often sensors log the data and then the client poles them every so often to get an update of the results it doesn't have yet. I've not used Thonny IDE but sounds like it might be possible. BTW, Python3 is on the RPi by default. Type python3 -c "import this" it should display the Zen of Python
    – ukBaz
    Nov 26, 2020 at 12:52
  • This is a good answer.
    – Seamus
    Nov 27, 2020 at 8:09
  • @ukBaz It has to update very often, a few times per second. It's an inertial sensor that you can place on a part of the body an it measures the angles of rotation on different axes. I need to place 3 on different parts of the arm and receive the data simultaneously and continuously to correctly capture its motion, so I would need at least 3 Bluetooth serial connections at the same time. How do you know the Raspberry Pi 4 can have a maximum of 2 connections? It appears somewhere in the specifications?
    – blt
    Nov 27, 2020 at 11:15
  • Updated answer with reference to specification for Bluetooth profile. Also added a suggestion for how client can connect to multiple servers.
    – ukBaz
    Nov 27, 2020 at 11:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.