Bluetooth LE Scanner with Asyncio

0.2.14 · active · verified Thu Apr 16

aioblescan is a Python library that allows for scanning Bluetooth Low Energy (BLE) advertised information using `asyncio`. It enables developers to build asynchronous applications that interact with BLE devices, decoding various advertising packet types. The current version is 0.2.14, with releases occurring periodically to add new plugin support and fix issues.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize `BLEScanner` and register a plugin (Eddystone in this case) to decode specific advertisement types. It sets up a callback function to process detected data asynchronously and prints Eddystone frames. Remember to run this with root privileges or with appropriate capabilities (e.g., `sudo setcap 'cap_net_raw,cap_net_admin+eip' $(which python3)`) as Bluetooth HCI access typically requires elevated permissions.

import asyncio
from aioblescan.scanner import BLEScanner
from aioblescan.plugins import Eddystone

async def main():
    scanner = BLEScanner()
    eddystone = Eddystone()

    def my_detection_callback(data):
        try:
            ev = eddystone.decode(data)
            if ev:
                print(f"Detected Eddystone: {ev}")
        except Exception as e:
            print(f"Error decoding packet: {e}")

    scanner.register_plugin(eddystone)
    scanner.process_data = my_detection_callback # Direct callback for all processed data

    await scanner.start()
    print("BLE scanning started... Press Ctrl+C to stop.")
    try:
        await asyncio.Event().wait() # Keep the main task running indefinitely
    except asyncio.CancelledError:
        pass
    finally:
        await scanner.stop()
        print("BLE scanning stopped.")

if __name__ == "__main__":
    try:
        asyncio.run(main())
    except KeyboardInterrupt:
        print("Exiting.")

view raw JSON →