Bleak: A Cross-Platform Asynchronous BLE GATT Client Library

Bleak, a striking name, hides powerful features behind it. It is a cross-platform asynchronous Bluetooth Low Energy (BLE) GATT client library written in Python, providing developers with a simple and easy-to-use interface to connect and control BLE devices effortlessly. This article will delve into the features, usage, and potential of Bleak in IoT applications.

Bleak: Platform-Agnostic BLE Client

Bleak stands for Bluetooth Low Energy platform Agnostic Klient, which precisely summarizes its core advantage: it abstracts the differences of underlying operating systems, providing developers with a unified API to interact with BLE devices using the same code across Windows, Linux, macOS, and Android platforms. This platform independence greatly simplifies the development process, reduces development costs, and enhances code portability.

Core Features and Advantages

Bleak’s main functionalities include:

  • Cross-Platform Support: Bleak is compatible with multiple operating systems, including Windows 10 (version 16299 and above), Linux (BlueZ >= 5.43), macOS (10.11 and above), and Android (via python-for-android). This allows developers to easily deploy their applications across various platforms.

  • Asynchronous Operations: Bleak is based on an asynchronous programming model, allowing developers to handle multiple BLE connections and operations simultaneously, improving the efficiency and responsiveness of applications. The asynchronous feature is particularly important when dealing with multiple BLE devices or scenarios requiring simultaneous read/write operations.

  • Full-Featured GATT Client: Bleak provides complete GATT client functionalities, including device discovery, connection, reading, writing, subscribing to notifications, and unsubscribing. Developers can easily interact with BLE devices using these functionalities.

  • Simple and Easy-to-Use API: Bleak offers a straightforward and intuitive API that is easy to learn and use, even for developers lacking BLE development experience. Its clear documentation and sample code further lower the learning curve.

Installation and Usage Example

Installing Bleak is very simple; just use the pip command:

$ pip install bleak

The following code example demonstrates how to scan for BLE devices and connect to a specified device to read its model information:

Device Scanning

import asyncio
from bleak import BleakScanner

async def main():
    devices = await BleakScanner.discover()
    for d in devices:
        print(d)

asyncio.run(main())

This code will scan for available BLE devices in the vicinity and print detailed information about each device, including address, name, services, etc.

Connecting and Reading Data

import asyncio
from bleak import BleakClient

address = "24:71:89:cc:09:05"  # Replace with your BLE device address
MODEL_NBR_UUID = "2A24"  # UUID for model number service

async def main(address):
    async with BleakClient(address) as client:
        model_number = await client.read_gatt_char(MODEL_NBR_UUID)
        print("Model Number: {0}".format("".join(map(chr, model_number))))

asyncio.run(main(address))

This code connects to the BLE device at the specified address and reads its model information (UUID is 2A24). The async with statement ensures that the client connection is properly closed, even if an error occurs.

Notes

It is important to note that you should not name your script bleak.py, as it would cause a circular import error and prevent the program from running.

Conclusion

Bleak is a powerful, easy-to-use, and cross-platform Python BLE library that provides developers with efficient tools for interacting with BLE devices. Its asynchronous features, simple API, and support for multiple operating systems make it an ideal choice for IoT application development. Whether for simple device monitoring or complex sensor data collection, Bleak can provide strong support for developers, accelerating development speed and improving efficiency. Its active community and comprehensive documentation also offer ample resources for developers seeking assistance.

Project URL: https://github.com/hbldh/bleak

Leave a Comment