Raspberry Pi

How to setup and connect to other devices using Bluetooth on Raspberry Pi

You want to enjoy your free time by listening to some good music on Bluetooth handsfree, but you don’t know how to connect these Bluetooth headsets to your Raspberry Pi, then no worries this write-up will solve your problem by explaining the procedure of setting up the Bluetooth devices with Raspberry Pi.

What is a Bluetooth device

Bluetooth is a wireless technology that connects the different devices on a particular wavelength with each other and the data is transferred between these devices. The Bluetooth devices can be connected with each other if they are in the range of 30 meters but when the obstacles come between the devices like walls then this range may be decreased. This is the secure way to transfer data between two devices and can be used to connect mobiles, laptops, or peripheral devices like headsets and keyboards.

How to setup Bluetooth on Raspberry Pi using the terminal

It is considered a good practice that whenever you start the Raspberry Pi OS, first update as well as upgrade all the packages of the repository of Raspberry Pi using the command:

$ sudo apt update && sudo apt full-upgrade -y

After making sure that all the packages are up to date, we will find the status of our Bluetooth service using the command:

$ sudo systemctl status bluetooth

In the output, it is cleared that the Bluetooth service is in active status, we can stop the service of Bluetooth using the command:

$ sudo systemctl stop bluetooth

Again check the status of Bluetooth service using the systemctl command:

$ sudo systemctl status bluetooth

To start the Bluetooth service, we will execute the command in the Raspberry Pi terminal:

$ sudo systemctl start bluetooth

In order to find out the name of our Bluetooth through the terminal, we will use the command:

$ hcitool dev

In the above output, we can see that the name of our Bluetooth device “hci0”, using this name, we will scan the devices that are in the range of our Bluetooth:

$ hcitool -i hci0 scan

The “DESKTOP-BSDBLIB” is the device to which we want to connect and its MAC address is E4:A4:71:79:78:D5, we will initialize the Bluetooth by using the command:

$ bluetoothctl

Once we are in the Bluetooth environment, we will turn on the Bluetooth module using the command:

power on

Once again scan nearby Bluetooth devices which are in the range of our Bluetooth:

scan on

Pair the device with MAC address E4:A4:71:79:78:D5 by using the trust command:

trust E4:A4:71:79:78:D5

Connect the device using its MAC address:

connect E4:A4:71:79:78:D5

If you want to disconnect the Bluetooth device, execute the command:

disconnect E4:A4:71:79:78:D5

How to setup Bluetooth on Raspberry Pi using the GUI

Another way to connect the Bluetooth device is much more convenient as it is GUI (graphical user interface) based, click on the Bluetooth icon on the right top of the screen and a drop-down menu will appear, select the “Add Device”:

Click on the device to which you want to connect through Bluetooth and click on the “Pair” button, in our case the device is “DESKTOP-BSDBLIB”:

The device will be added, a red cross is showing the device is not yet connected, click on the added device, another menu will appear to choose the “Connect”:

The red color icon will turn into the green color which indicates the device is connected successfully:

For disconnection, again click on the device and choose “Disconnect” to disconnect the device with Raspberry Pi:

Conclusion

Bluetooth technology is very popular nowadays, to connect various devices such as wireless headsets, and speakers. One of the reasons behind the popularity of these Bluetooth devices is that they are easy to handle as they do not contain a mess of wires with them. In this write-up, setting up the Bluetooth devices on Raspberry Pi has been explained through the command-line interface as well as the graphical user interface.

About the author

Hammad Zahid

I'm an Engineering graduate and my passion for IT has brought me to Linux. Now here I'm learning and sharing my knowledge with the world.