Introduction
- Raspberry Pi 3 has come up with a great on-board feature i.e. the on-board Bluetooth. So, there is no need to for external Bluetooth dongle. This frees up a USB port for other uses, which would otherwise be used up by the Bluetooth dongle.
- Raspberry Pi 3 has BCM43438 highly integrated single chip which includes 2.4GHz WLAN, Bluetooth and FM receiver.
Before start communicating using Raspberry Pi’s On-board Bluetooth, we need to set up the On-board Bluetooth of Raspberry Pi.
How to set up the On-board Bluetooth of Raspberry Pi
Raspberry Pi has on-board Bluetooth which can be used for communication or sending/receiving files.
Before establishing communication between Raspberry Pi and a Bluetooth enabled device, we need to pair them.
Pairing a Bluetooth device on Raspberry Pi is same as that on a mobile or Laptop.
Turn-ON Bluetooth as follows,
Then make it discoverable.
Now, select option Add Device…
After selecting Add device… pop-up window appear which will be as follows,
In above window, we can see mobile Bluetooth device named “ZUK Z1”. Select device and then click on pair. This will prompts for pairing, and displays the pin (the pin that was randomly generated by raspberry pi) from Raspberry Pi to Bluetooth device, which should cause the device to ask you to confirm the connection.
After the device accepts the connection by using the pair option, our raspberry pi and the Bluetooth device will be paired and connection can be established between them. Now our Raspberry Pi is ready to communicate with the paired device.
How to transfer files between Raspberry Pi and a device via Bluetooth?
There are two ways for communicating between Raspberry Pi and a device via Bluetooth.
- Bluetooth GUI (Graphical User Interface)
- Command Line Interface(CLI)
Before using GUI or CLI for accessing Bluetooth based transfer, we need to install the following packages,
Blueman–It is a full featured Bluetooth manager. It provides GUI based setting panel Bluetooth manager.
Bluez–This package provides the Bluetooth protocol stack and the bluetoothctl utility.
Bluetooth–This package provides all the plugins supported by BluezBluetooth stack.
Now to use Bluetooth service install above packages using following command,
sudo apt-get install bluemanbluez Bluetooth
After successful installation of above packages, reboot Raspberry Pi.
Using Graphical User Interface(GUI) for Bluetooth Services
Raspberry Pi don’t have GUI for accessing Bluetooth services like in laptop/mobile. To get GUI for Bluetooth, we installed packages mentioned above.
Now, we can access Bluetooth services (send/receive) using GUI. For this, go to the menu and select preferences. In preferences, select Bluetooth Manager
. Now, we can see Bluetooth manager window with all the visible Bluetooth devices listed as shown below.
We need to make Bluetooth visible so that it is discoverable to other devices. To do this, select Adapter à preferences as shown below,
We can transfer files from Raspberry Pi to selected Bluetooth device.For sending file, right click on selected device and select send a file as shown below,
Browse a file and send it. We can receive a file too on Raspberry Pi.
Command Line Interface (CLI) for Bluetooth Services
The packages which we have installed earlier are also necessary for command line interface.
Pairing a Bluetooth device from shell is the simplest and most reliable option.
- Bluetooth Connection using bluetoothctl
Follow the steps given below to pair and connect Raspberry Pi’s Bluetooth with another Bluetooth device. We are using bluetoothctl
utility for pairing and establishing connection between Raspberry Pi and Bluetooth devices.
Enter following command to start bluetoothctl
bluetoothctl
turn the power to the controller on using following command
power on
enter devices to get the mac address of device with which we want to pair. This command gives a list of available devices.
devices
if no device is in the list then search for devices as follows,
scan on
turn the agent on
agent on
pair the Bluetooth device
pair <mac address>
e.g. pair 01:02:03:04:05:06
If using a device without PIN then we need to manually mark the device as a trust device as follows,
trust <mac address>
Finally establish a connection between two Bluetooth enabled devices.
Connect <mac address>
To know more about commands, you can enter help command.
- Send/Receive Data over Bluetooth using CLI
We can send/receive data from/on Raspberry Pi over Bluetooth using CLI. This communication is helpful when we need to control any device connected to Raspberry Pi using smartphone/laptop over Bluetooth. For such applications CLI is the most efficient way of communication. Here, we are using RFCOMM (Radio Frequency Communication) Bluetooth protocol.
Example
Let’s exchange data between Smartphone (Mobile) and Raspberry Pi using CLI over Bluetooth.
To use CLI for communicating between two Bluetooth devices follow the steps given below,
We already paired a device using bluetoothctl.
Here, we are using hciconfig for configuring Bluetooth devices to establish data transfer between them.
Enter following command for Raspberry Pi Bluetooth device information.
hciconfig
This will provide mac address of Raspberry Pi’s Bluetooth. Also, it gives information about whether the port is UP/DOWN.
If it is DOWN, then enter following command to make it UP.
hciconfig hci0 up
Note: sometimes it gives an error like “operation not possible due to RF-Kill” then it can be resolved with
rfkill unblock all
create Bluetooth profile as follows
sdptool add sp
To receive data from smartphone, we should connect Smartphone to Raspberry Pi. For connecting Smartphone and communicating (send/receive) data, we need to use a Bluetooth application on Smartphone. Here, we are using Blueterm
application.
For establishing connection between Raspberry Pi and Smartphone via Bluetooth, use following command
sudo rfcomm listen hci0&
This command make the Raspberry Pi wait for device(smartphone) request to connect.
We have to open Blueterm
application and select Bluetooth device (Raspberry Pi) with which we want to connect.
When device is connected successfully, we can receive messages from Smartphone.
Now, we can send character/messages from Smartphone to Raspberry Pi but we can’t see them on Raspberry Pi terminal. To see received character/messages, enter following command
cat /dev/rfcomm0
Now we can see the messages in terminal which is receiving.
If we want to send messages from Raspberry Pi to Smartphone over Bluetooth then use following command,
echo “type character/message here” >/dev/rfcomm0
Now, we can successfully communicate messages between Raspberry Pi and Mobile over Bluetooth. This message transferring can be used to build Raspberry Pi based applications like controlling home appliances (heater, Light, door, fan) using mobile phone over Bluetooth. Just by sending message from mobile we can control the devices connected to Raspberry Pi.
Components Used |
||
---|---|---|
RASPBERRY PI 3 A Plus RASPBERRY PI 3 A+RASPBERRY PI 3 A+ |
X 1 | |
Raspberry Pi 4B Raspberry Pi 4BRaspberry Pi 4B |
X 1 |