Python is an increasingly popular programming language due to its simplicity and readability. The gatt-python library for Python that facilitates the implementation and communication with Bluetooth Low Energy devices using the Generic Attribute Profile (GATT). GATT is the specification for the transmission and reception of short data over a Bluetooth Low Energy link.
The SDK supports a range of functionalities, including device discovery, connection and disconnection, custom GATT profile implementation and access to all Bluetooth GATT services and characteristics. It also allows for reading and writing characteristic values and subscribing to notifications for changes in these values. The library is only compatible with Linux because it uses the D-Bus API of BlueZ for Bluetooth device interaction.
Barry Byford has a new blog post on D-Bus and Bluez. It documents his experiences creating a BLE central client on Linux. Barry deliberates over dbus-python and pydbus. He then provides an example how to create a Bluetooth GATT connection, converting D-Bus typed data to python types and interacting with a BBC micro:bit.
While you are on Barry’s site, read his notes from workshops and further notes on using Bluetooth.
The Bluetooth SIG has a new Bluetooth Technology for Linux Developers Study Guide. It explains how Bluetooth is implemented in hardware as part of the main board or added using a USB dongle. The Bluetooth stack runs as a system service using BlueZ. BlueZ is accessed via inter-process communication (IPC) via D-Bus, a message-based system service. Applications use D-Bus and hence BlueZ.
Barry Byford has a useful and continually growing introduction to using Bluetooth on Linux. He describes how to use BlueZ, the official Bluetooth stack on Linux, via D-Bus and Python. D-Bus allows communication between multiple processes running concurrently on the same machine. There’s an example how to set up an asynchronous event loop and how to build a Bluetooth Low Energy (BLE) device implementing the Bluetooth Central role.
While you are there, also take a look at Barry’s notes and examples on Creating a BlueZ Pairing Agent and Bluez beacons on the command line.
The open source code advertises Eddystone-URL, Eddystone-UID and Eddystone-TLM. Examples are provided.
Our previous post mentioned node-beacon-scanner that provides for node.js Bluetooth scanning.
Most use of Bluetooth LE and beacons only looks at the transmitted advertising containing identification and sensor information. More advanced use requires connection to the device using GATT to write, read and be notified of changes in values (Bluetooth Service Characteristics). The most common use for connecting is to set configurable settings as in the case of device manufacturer smartphone apps.
Some solutions need to manipulate Bluetooth Service Characteristics programmatically. Barry Byford has a new Pyton library BLE-GATT for Linux based devices. It’s based on the BlueZ D-Bus API, features a small number of dependencies and can be easily installed without sudo privileges.
Cybele bridges Bluetooth Low Energy devices to MQTT on Linux devices. MQ Telemetry Transport Protocol (MQTT), invented by IBM, is an ISO standard that uses lightweight publish/subscribe messaging to send data to a server.
TinyGo is a Go compiler for small devices such as microcontrollers. For example, it runs on the BBC micro:bit, Nordic (used in beacons), ESP32, Arduino Uno and Adafruit boards.
Bluetooth LE support has recently been added to TinyGo:
There’s an article on hackster and the open source repository is on GitHub.
If you are developing using Node, there’s a Node.js module, node-beacon-scanner, that allows you to scan Bluetooth beacon packets and parse the packet data.
While Node tends to be associated with Linux, servers and hence web sites, Node can also be used on small single board computers such as the Raspberry Pi. Hence, this module provides an easy way to scan for Beacons and other Bluetooth devices.
The module supports iBeacon and Eddystone and outputs the MAC address, local name, transmit power level, RSSI and the iBeacon/Eddystone specific attributes.
We recently came across a Python library, BeaconTools, for working with various types of Bluetooth LE Beacon. It supports iBeacon, Eddystone and Estimote beacons, extracting information from raw binary beacon advertisements. It can also work as a beacon scanner.
The source code is on GitHub.