Top Related Projects
Quick Overview
Bleak (Bluetooth Low Energy platform Agnostic Klient) is a cross-platform Bluetooth Low Energy (BLE) client library for Python. It provides a high-level, asynchronous API for discovering, connecting to, and interacting with BLE devices on Windows, macOS, and Linux.
Pros
- Cross-platform compatibility (Windows, macOS, Linux)
- Asynchronous API for efficient handling of multiple BLE operations
- Simple and intuitive interface for common BLE tasks
- Active development and community support
Cons
- Limited support for some advanced BLE features
- Potential performance overhead due to its cross-platform nature
- Dependency on platform-specific backends (e.g., BlueZ on Linux)
- May require additional setup steps on some platforms
Code Examples
- Scanning for BLE devices:
import asyncio
from bleak import BleakScanner
async def scan_devices():
devices = await BleakScanner.discover()
for d in devices:
print(f"Device: {d.name}, Address: {d.address}")
asyncio.run(scan_devices())
- Connecting to a device and reading a characteristic:
from bleak import BleakClient
async def read_characteristic(address, char_uuid):
async with BleakClient(address) as client:
value = await client.read_gatt_char(char_uuid)
print(f"Characteristic value: {value}")
asyncio.run(read_characteristic("XX:XX:XX:XX:XX:XX", "00002a00-0000-1000-8000-00805f9b34fb"))
- Writing to a characteristic and receiving notifications:
async def write_and_notify(address, write_char_uuid, notify_char_uuid):
def notification_handler(sender, data):
print(f"Notification received: {data}")
async with BleakClient(address) as client:
await client.start_notify(notify_char_uuid, notification_handler)
await client.write_gatt_char(write_char_uuid, b"Hello BLE!")
await asyncio.sleep(5) # Wait for notifications
await client.stop_notify(notify_char_uuid)
asyncio.run(write_and_notify("XX:XX:XX:XX:XX:XX", "write_uuid", "notify_uuid"))
Getting Started
-
Install Bleak using pip:
pip install bleak
-
Import the necessary modules in your Python script:
from bleak import BleakScanner, BleakClient import asyncio
-
Use the examples above as a starting point for your BLE application, replacing the placeholder UUIDs and addresses with your specific device information.
-
Run your script using Python 3.7 or later, as Bleak requires asyncio support.
Competitor Comparisons
Python interface to Bluetooth LE on Linux
Pros of bluepy
- More mature project with longer history and established user base
- Supports a wider range of Bluetooth LE operations and features
- Better documentation and examples for advanced use cases
Cons of bluepy
- Limited to Linux platforms only
- Requires compilation of C++ extensions, which can be challenging for some users
- Less active development and slower response to issues
Code Comparison
bluepy:
from bluepy import btle
scanner = btle.Scanner()
devices = scanner.scan(10.0)
for dev in devices:
print(f"Device {dev.addr} ({dev.addrType}), RSSI={dev.rssi} dB")
bleak:
import asyncio
from bleak import BleakScanner
async def scan():
devices = await BleakScanner.discover()
for d in devices:
print(f"Device {d.address} ({d.name}), RSSI={d.rssi} dB")
asyncio.run(scan())
Both libraries provide similar functionality for scanning Bluetooth LE devices, but bleak uses an asynchronous approach with modern Python syntax. Bleak offers cross-platform support and a more user-friendly API, while bluepy provides more low-level control and advanced features for Linux systems.
Bluetooth Python extension module
Pros of PyBluez
- Supports both Bluetooth Classic and Bluetooth Low Energy (BLE)
- More mature project with longer history and established user base
- Provides lower-level access to Bluetooth stack
Cons of PyBluez
- Limited cross-platform support (primarily Linux and Windows)
- Less active development and maintenance
- More complex API, requiring deeper understanding of Bluetooth protocols
Code Comparison
PyBluez example:
import bluetooth
nearby_devices = bluetooth.discover_devices(lookup_names=True)
for addr, name in nearby_devices:
print(f"Address: {addr}, Name: {name}")
Bleak example:
from bleak import BleakScanner
async def scan():
devices = await BleakScanner.discover()
for d in devices:
print(f"Address: {d.address}, Name: {d.name}")
Key Differences
- Bleak focuses on BLE, while PyBluez supports both Classic and BLE
- Bleak uses async/await syntax, making it more suitable for modern Python applications
- Bleak offers better cross-platform support, including macOS
- Bleak has a more active development community and frequent updates
- PyBluez provides lower-level access, while Bleak abstracts many Bluetooth complexities
Convert
designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
===== Bleak
.. image:: https://github.com/hbldh/bleak/workflows/Build%20and%20Test/badge.svg :target: https://github.com/hbldh/bleak/actions?query=workflow%3A%22Build+and+Test%22 :alt: Build and Test
.. image:: https://img.shields.io/pypi/v/bleak.svg :target: https://pypi.python.org/pypi/bleak
.. image:: https://img.shields.io/pypi/dm/bleak.svg :target: https://pypi.python.org/pypi/bleak :alt: PyPI - Downloads
.. image:: https://readthedocs.org/projects/bleak/badge/?version=latest :target: https://bleak.readthedocs.io/en/latest/?badge=latest :alt: Documentation Status
.. image:: https://img.shields.io/badge/code%20style-black-000000.svg :target: https://github.com/psf/black
.. container::
.. image:: Bleak_logo2.png
:target: https://github.com/hbldh/bleak
:alt: Bleak Logo
Bleak is an acronym for Bluetooth Low Energy platform Agnostic Klient.
- Free software: MIT license
- Documentation: https://bleak.readthedocs.io.
Bleak is a GATT client software, capable of connecting to BLE devices acting as GATT servers. It is designed to provide a asynchronous, cross-platform Python API to connect and communicate with e.g. sensors.
Installation
.. code-block:: bash
$ pip install bleak
Features
- Supports Windows 10, version 16299 (Fall Creators Update) or greater
- Supports Linux distributions with BlueZ >= 5.43
- OS X/macOS support via Core Bluetooth API, from at least OS X version 10.11
- Android backend compatible with python-for-android
Bleak supports reading, writing and getting notifications from GATT servers, as well as a function for discovering BLE devices.
Usage
To discover Bluetooth devices that can be connected to:
.. code-block:: python
import asyncio
from bleak import BleakScanner
async def main():
devices = await BleakScanner.discover()
for d in devices:
print(d)
asyncio.run(main())
Connect to a Bluetooth device and read its model number:
.. code-block:: python
import asyncio
from bleak import BleakClient
address = "24:71:89:cc:09:05"
MODEL_NBR_UUID = "2A24"
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))
DO NOT NAME YOUR SCRIPT bleak.py
! It will cause a circular import error.
See examples folder for more code, for instance example code for connecting to a
TI SensorTag CC2650 <http://www.ti.com/ww/en/wireless_connectivity/sensortag/>
_
Top Related Projects
Convert
designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot