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…