It’s been a while since I added something on this blog.
One I’ve been very busy, secondly I’ve also been breaking my head getting Bluetooth communication to work on my Raspberry.
But I’ve managed to get it to work :
The first step is to setup Bluetooth on the Raspberry.
On the Raspberry Pi 3 we do not need to do anything special since Bluetooth is by default activated.
With the following command you can search for any Bluetooth devices around your raspberry :
“hcitool” is a command to configure and send some special commands to Bluetooth devices.
We now need to add a new service by creating a new rfcomm.service :
sudo nano /etc/systemd/system/rfcomm.service
and then add following code inside
[Unit] Description=RFCOMM service After=bluetooth.service Requires=bluetooth.service [Service] ExecStart=/usr/bin/rfcomm watch hci0 [Install] WantedBy=multi-user.target
With this newly created service we watch for any incoming connections from outside.
After the client disconnects, the “watch” command will restart listening for new incoming connections.
Now we only need to enable the service with the following command :
sudo systemctl enable rfcomm
The next time we boot-up the raspberry he will listen for any connection being made from our PC. Or if you do not want to reboot you can start the service manually :
sudo systemctl start rfcomm
We also need to change the following lines in /etc/systemd/system/dbus-org.bluez.service
and add the following line directly beneath it :
ExecStartPost=/usr/bin/sdptool add SP
With these lines we setup serial communication (SP).
Now all what is left to do is try to pair our Raspberry with our PC.
We do this in the Bluetooth setup of our PC.
But first we must make sure that our Raspberry can be found. We do this by entering the following command :
Here we enter the following line :
Now I went back to my PC’s bluetooth settings and found “raspberrypi” as a new bluetooth device.
I clicked on “raspberrypi” and then “pair”.
After a couple of seconds it now shows me that the “raspberrypi” is paired.
Now when I look into “more bluetooth settings” in the “com-ports” tab I see the following ports :
COM3 – incoming connection – raspberrypi
COM4 – outgoing connection – raspberrypi ‘serial port’
Windows automatically chooses which COM-port it assigns to the Raspberry, so this could be any COM-port (except COM1).
Now we can test our communication :
With Putty open up a new serial connection on COM4.
On the raspberry enter the following command to read out the Bluetooth channel :
sudo cat /dev/rfcomm0
Now everything you type in Putty should appear on the Raspberry…
Een gedachte over “Serial Bluetooth communication”
Thanks for the useful info on setting Rasbian BT serial, it really saved me.
I use it to stream GPS info from Android via the ‘Share GPS’ app by jillbunch.
While the connection works fine once, I have to reboot the pi to get it to work a second time.
The problem may be the Share GPS app. After choosing the paired BT connectionthere is a button labelled ‘idle’. At this point the PI does not show the /dev/rfcomm0 port.
On Android, clicking the ‘Connect’ button and the label changes from ‘idle’ to ‘listening’.
Clicking a second time and the label changes to ‘Connecting’ then ‘Connected’.
At this point /dev/rfcomm0 exists and I can read data from Android.
do cat < /dev/rfcomm0' and you see the stream of NMEA data from android.
Finally I click 'Disconnect' on Android and the label changes to 'idle'.
At this point I would expect /dev/rfcomm0 to disapper, but it does not.
I ran all the above with bluetoothctl active. I saw that the first connection showed me a popup message 'Device xxxxx Connected: yes'.
I was expecting a similar message 'Device xxxxx Connected: no' after disconnecting the Android, but it did not appear.
I tried using the bluetoothctl disconnect command but it did not save the situation.
Please let me know how I can help debug this, what logs to configure and save.