Bluetooth Low Energy for the Raspberry Pi


The serial interface is one of the oldest interfaces in the IT world and has managed to survive for many generations on continually evolving carrier technologies. The UART service for BLE is the Nordic UART Service (NUS). For many applications, UART plays the role of an intermediate protocol. This is not exactly what the inventors of Bluetooth had in mind, but it is simple. In particular, developers can easily port existing applications.

Listing 3 shows an example of transferring sensor data with UART. A microcontroller reads a connected BME280 sensor, bundles the results into a string, and writes the string to the serial interface. An XIAO nRF52840 microcontroller is a great choice for entry-level applications because it is inexpensive and supports all common languages.

Listing 3

Transfer Sensor Data with UART


The code turns out to be very compact thanks to the CircuitPython libraries, but even with other programming languages the whole thing is unlikely to be much more complicated. The logic is similar on the client side. Instead of advertisements, the control panel scans for UART services, connects to the sensor, and reads from the serial port. The complete code for the server and client can be found in my project repository [6].

You may encounter some disadvantages of the UART procedure if other devices on the network offer NUS. The client program gets around this by querying the advertisement name BME280_Sensor, but this is not an elegant approach. It would make more sense to have a separate service that defines the data structure.

Figure 5 shows the advantage of using UART, on the other hand. Adafruit's app can read UART data in CSV format without needing to understand the details of the data; you have a choice between text output or a plot for display purposes. A shared plot is not really suitable for the BME280 sensor data. The values for temperature, humidity (two-digit range), and pressure (four-digit range) are so far apart that the details are lost. But if you have the right kind of data, fast visualization of the measured values via a UART plot proves to be very useful.

Figure 5: The UART output as a plot with Bluefruit LE Connect.


Broadcasts are a special type of advertisement. The peripheral device sends user data along with the advertisement. This means that other devices do not even have to establish a connection. Broadcasts are the only way to send data from one device to many others.

Broadcasts are popular in indoor navigation using beacons or in museums. In a museum, the advertisement typically contains a URL to a website that explains an exhibit in more detail. If the transmitter is close to the picture and transmits with low power, only suitable apps will receive the information.

For my example, the setup with the microcontroller and the BME280 sensor again provides a sample use case; Listing 4 shows the matching implementation. This program, including the appropriate client that reads the data, can also be found in the project repository [6].

Listing 4

Advertisement with Data


The AdafruitSensorMeasurement class, a subclass of Advertisement, has a set of predefined fields that the program populates with the measurements from the BME280 (lines 11 to 14). The command in line 16 ultimately sends the advertisement.

Any other devices can receive the advertisement by scanning – and even extract the data, if they know the internal structure. If the fields of the AdafruitSensorMeasurement class don't match your projects, just copy the class and adapt it for your needs. This works even if you don't understand all the details.


BLE is ideal for networking battery-based sensors. The data sheet for the combined CYW43439 Bluetooth and WiFi chip on the new Raspberry Pi Pico W, for example, specifies energy consumption for BLE that is a factor of 1,000 lower than for WLAN (TX: 234 to 351 µA versus 270 to 320 mA).

Until BLE is enabled on the chip, you will have to resort to other microcontrollers. You won't get away with this quite as cheaply as with the Pico W. However, for around $10, the above-mentioned XIAO nRF52840 BLE is pretty affordable. Smartphones also support the wireless standard, which opens up other ideas for cool projects. For example, the Raspberry Pi could insist on a specific smartphone being nearby to release an application.


  1. "Connect Pi Devices and a Smartphone with Bluetooth" by Bernhard Bablok, Linux Magazine, issue 264, November 2022, pp. 62-27
  2. BLE Primer:
  3. Official UUIDs for standardized profiles and services:
  4. Bleak:
  5. Nordic Semiconductor Developer Academy:
  6. Git repository for this article:

The Author

Bernhard Bablok works at Allianz Technology SE as an SAP HR developer. When he is not listening to music, riding his bike, or walking, he focuses on Linux, programming, and small computers. You can reach him on

Buy this article as PDF

Express-Checkout as PDF
Price $2.95
(incl. VAT)

Buy Linux Magazine

Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content

  • Reverse Engineering Bluetooth

    What do you do when all your Bluetooth clocks show slightly different times? With some reverse engineering, you can write a Python program to synchronize your clocks.

  • Bluetooth Communication

    We use a Raspberry Pi, a Pi Pico, and a smartphone to communicate over Bluetooth.

  • Bluetooth Printing

    Even if your printer vendor doesn’t advertise Linux Bluetooth support, there are a few tools that may help you set up your Linux system for Bluetooth printing.

  • Bluetooth Security

    Is your address book open to the world? Is your mobile phone calling Russia? Many users don’t know how easy it is for an attacker to target Bluetooth.

  • Bluetooth Mobile Phones

    It is becoming increasingly common for new generation mobile phones to have an integrated Bluetooth interface. This article explores how to access your Bluetooth phone using Linux.

comments powered by Disqus
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters

Support Our Work

Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.

Learn More