SimplePyBLE

0.12.1 · active · verified Thu Apr 16

SimplePyBLE provides Python bindings for SimpleBLE, a fully cross-platform Bluetooth Low Energy (BLE) library written in C++. It is designed for simplicity and ease of use, offering a consistent API across Windows, macOS, and Linux, with some support for iOS and Android. The library is currently at version 0.12.1 and maintains an active release cadence, with frequent updates addressing features, stability, and bug fixes.

Common errors

Warnings

Install

Imports

Quickstart

This asynchronous quickstart demonstrates how to initialize an adapter, scan for Bluetooth Low Energy (BLE) peripherals, and connect to a discovered device. It uses the `simplepyble.aio` module, which offers an `asyncio`-compatible API with automatic callback cleanup and context manager support for robust asynchronous operations.

import asyncio
from simplepyble.aio import Adapter

async def main():
    adapters = Adapter.get_adapters()
    if not adapters:
        print("No adapters found. Ensure Bluetooth is on and permissions are granted.")
        return

    # Use the first adapter found
    adapter = adapters[0]
    print(f"Selected adapter: {adapter.identifier()} [{adapter.address()}]")

    # Use the adapter within an async context manager
    async with adapter:
        print("Scanning for 5 seconds...")
        await adapter.scan_for(5000) # Scan for 5000 milliseconds

        peripherals = adapter.scan_get_results()
        if not peripherals:
            print("No peripherals found.")
            return

        print("Found peripherals:")
        for i, peripheral in enumerate(peripherals):
            print(f"  [{i}] {peripheral.identifier()} [{peripheral.address()}] - Connectable: {peripheral.is_connectable()}")

        # Example: Connect to the first connectable peripheral found
        connectable_peripherals = [p for p in peripherals if p.is_connectable()]
        if connectable_peripherals:
            selected_peripheral = connectable_peripherals[0]
            print(f"Attempting to connect to: {selected_peripheral.identifier()}")
            await selected_peripheral.connect()
            print(f"Successfully connected to {selected_peripheral.identifier()}")

            # Discover services and characteristics (example)
            services = selected_peripheral.services()
            for service in services:
                print(f"  Service: {service.uuid()}")
                for characteristic in service.characteristics():
                    print(f"    Characteristic: {characteristic.uuid()} (Can Read: {characteristic.can_read()})")
            
            await selected_peripheral.disconnect()
            print(f"Disconnected from {selected_peripheral.identifier()}")
        else:
            print("No connectable peripherals found to demonstrate connection.")

if __name__ == "__main__":
    asyncio.run(main())

view raw JSON →