Using the On-Board Bluetooth on the Raspberry Pi 4 for Communication - Raspberry Pi Projects, Tutorials, Learning DIY Electronics - Makergenix

Breaking

 


Using the On-Board Bluetooth on the Raspberry Pi 4 for Communication

Introduction - Raspberry Pi 4 Model B

 The Raspberry Pi 4 Model B is the most recent addition to the popular Raspberry Pi computer line. When compared to the previous-generation Raspberry Pi 3 Model B+, it provides ground-breaking improvements in processing speed, multimedia performance, memory, and connection while maintaining backwards compatibility and similar power consumption. In terms of desktop performance, the Raspberry Pi 4 Model B is equivalent to entry-level x86 PC computers.

Using the On-Board Bluetooth on the Raspberry Pi 4 for Communication

A high-performance 64-bit quad-core processor, dual-display support at resolutions up to 4K via a pair of micro-HDMI ports, hardware video decode at up to 4Kp60, up to 4GB of RAM, dual-band 2.4/5.0 GHz wireless LAN, Bluetooth 5.0, Gigabit Ethernet, USB 3.0, and PoE capability are some of the key features of this product (via a separate PoE HAT add-on).

How to Configure the Raspberry Pi's On-Board Bluetooth

Bluetooth is built into the Raspberry Pi and may be used for communication as well as sending and receiving data.

We must first couple the Raspberry Pi with a Bluetooth-enabled device before we can communicate with it.

The process of pairing a Bluetooth device on a Raspberry Pi is the same as it is on a smartphone or laptop.

Turn on Bluetooth by doing the following:

  • Make it discoverable after that.
Configure the Raspberry Pi's On-Board Bluetooth
  • Choose an option now. Add a device...

  • After you've selected Add device... A pop-up window will emerge, Add device

We can see a mobile Bluetooth gadget named "ZUK Z1" in the window above. Select the device and then pair it. This will prompt for pairing and display the pin from Raspberry Pi to Bluetooth device (the pin that was randomly created by Raspberry Pi), which should trigger the device to ask you to confirm the connection.

Our raspberry pi and the Bluetooth device will be connected and a connection may be formed when the device accepts the connection using the pair option. Our Raspberry Pi is now ready to talk to the attached device.

How can I transfer files from a Raspberry Pi to a Bluetooth-enabled device?

Bluetooth communication between a Raspberry Pi and a device may be accomplished in two ways.

  1. GUI for Bluetooth (Graphical User Interface)
  2. The CLI (Command Line Interface) (CLI)

The following packages must be installed before we can use the GUI or CLI to access Bluetooth-based transmission.

  • Blueman is a Bluetooth manager with a lot of features. It comes with a graphical user interface (GUI)-based Bluetooth manager.
  • Bluez–The Bluetooth protocol stack and the bluetoothctl software are included in this package.
  • Bluetooth–This package contains all of the BluezBluetooth stack's plugins.

Install the required packages using the commands below to utilise the Bluetooth service.

sudo apt-get install bluemanbluez Bluetooth

Reboot Raspberry Pi once the aforementioned packages have been installed successfully.

For Bluetooth Services, a Graphical User Interface (GUI) is used.

The Raspberry Pi does not have a graphical user interface (GUI) for accessing Bluetooth services like a laptop or mobile phone. We installed the programmes mentioned above to get a Bluetooth GUI.

We can now use the GUI to access Bluetooth services (send/receive). To do so, go to the menu and pick preferences from the drop-down menu. Select Bluetooth Manager from the options menu. As illustrated below, the Bluetooth management window now displays all of the visible Bluetooth devices.

Bluetooth Manager

We need to make Bluetooth visible so that other devices may find it. To do so, go to Adapter Γ  Preferences and choose Adapter Γ  Preferences, as seen below.

Bluetooth visible

We can send files from the Raspberry Pi to a Bluetooth device of our choice.

To send a file, right-click on the device you want to send it to and select Send a File, as shown below.

Send a file by browsing through it. On the Raspberry Pi, we may also receive a file.

Bluetooth Services Command Line Interface (CLI)

The items we installed before are also required for the command line interface.

The easiest and most reliable method is to pair a Bluetooth device with shell.

  • Using bluetoothctl to connect to a Bluetooth device

To pair and connect the Raspberry Pi's Bluetooth with another Bluetooth device, follow the instructions below. For pairing and creating connections between Raspberry Pi and Bluetooth devices, we use the bluetoothctl software.

To start bluetoothctl, use the following command.

bluetoothctl

Using the following instruction, switch on the controller's power.

power on

To acquire the mac address of the device with which we wish to pair, type devices. This command displays a list of devices that are currently accessible.

devices

If no devices appear in the list, use the following search:

scan on

activate the agent

agent on

connect your Bluetooth device

pair <mac address>

e.g. pair 01:02:03:04:05:06

If you're using a device that doesn't have a PIN, you'll need to manually mark it as a trusted device, as shown below.

trust <mac address>

Finally, make a Bluetooth connection between two devices that support it.

Connect <mac address>

You may use the help command to learn more about commands.

  • CLI is used to send and receive data via Bluetooth.

Using the CLI, we can send/receive data from/on the Raspberry Pi through Bluetooth. This connection is useful when we need to control a Raspberry Pi-connected item from a smartphone or laptop through Bluetooth. CLI is the most efficient mode of communication for such apps. The Bluetooth protocol RFCOMM (Radio Frequency Communication) is used here.

LoRa HAT for raspberrry Pi

Example

Let's use CLI to send data between a Smartphone (Mobile) and a Raspberry Pi over Bluetooth.

Follow the instructions below to utilise CLI to communicate between two Bluetooth devices.

We've already used bluetoothctl to link a device.

We're going to use hciconfig to configure Bluetooth devices so that data may be sent between them.

For Raspberry Pi Bluetooth device details, use the following command.

hciconfig

This will give you the Raspberry Pi's Bluetooth's mac address. It also indicates if the port is open or closed.

If it's down, use the commands below to get it back up.

hciconfig hci0 up

Note: If you get an error like "operation not feasible due to RF-Kill," you may fix it using

rfkill unblock all

as follows: build a Bluetooth profile

sdptool add sp

We must connect the smartphone to the Raspberry Pi in order to get data from it. We need to utilise a Bluetooth application on the Smartphone to connect it and communicate (send/receive) data. We're utilising the Blueterm application here.

Use the following command to connect your Raspberry Pi to your smartphone through Bluetooth.

sudo rfcomm listen hci0&

This command instructs the Raspberry Pi to wait for a device (smartphone) connection request.

We must first launch the Blueterm programme and choose the Bluetooth device (Raspberry Pi) with which we wish to communicate.

We may get messages from our Smartphone once the device has been correctly linked.

We can now transfer characters/messages from our smartphones to the Raspberry Pi, but we can't see them on the terminal on the Raspberry Pi. Enter the following command to display the received characters/messages.

cat /dev/rfcomm0

We can now see the messages in the terminal that is receiving them.

Use the following command to transmit messages from the Raspberry Pi to a smartphone through Bluetooth.

echotype character/message here” >/dev/rfcomm0

We can now send and receive messages through Bluetooth between the Raspberry Pi and the mobile device. This message transfer may be used to create Raspberry Pi-based apps such as operating household appliances (heating, light, door, fan) over Bluetooth using a mobile phone. We can control the gadgets linked to the Raspberry Pi simply by sending text messages from our phones.

Posts You May like

 


Most Viewed Posts

Write For Us

Name

Email *

Message *

All Blogs