A class to manage Bluetooth functionality as both central and peripheral device.
This class provides a unified interface for: - Central mode: Scan for, connect to, and communicate with BLE peripherals - Peripheral mode: Advertise, accept connections, and communicate with BLE centrals
- Example (Peripheral mode):
bt = Bluetooth() bt.start_peripheral(name="MyDevice") bt.on_write(lambda data: print("Received:", data)) while True:
- if bt.is_peripheral_connected:
- bt.send("Hello from peripheral!")
time.sleep(1)
- Example (Central mode):
- bt = Bluetooth() bt.on_notify(lambda data: print("Received:", data)) bt.scan(callback=my_scan_callback) # After finding device... bt.connect(addr_type, addr) bt.write("Hello from central!")
| Method | __del__ |
Clean up Bluetooth resources. |
| Method | __init__ |
Initialize Bluetooth. |
| Method | advertise |
Start or stop BLE advertising to make this device discoverable. |
| Method | callback |
Set the Bluetooth callback function. |
| Method | connect |
Connect to a BLE peripheral device (central mode). |
| Method | decode |
Decode device name from advertising data. |
| Method | decode |
Decode service UUIDs from advertising data. |
| Method | disconnect |
Disconnect from a device. |
| Method | discover |
Discover characteristics for a service. |
| Method | discover |
Discover services on the connected peripheral. |
| Method | is |
Check if a device address is in the paired devices list. |
| Method | is |
Check if UART service is fully discovered and ready (central mode). |
| Method | load |
Load paired devices from storage. |
| Method | on |
Set callback for when data is received from a peripheral (central mode). |
| Method | on |
Set callback for scan results (central mode). |
| Method | on |
Set callback for when data is received from a central (peripheral mode). |
| Method | pair |
Initiate pairing/bonding with the connected device. |
| Method | passkey |
Reply to a passkey request during pairing. |
| Method | read |
Read data from a characteristic. |
| Method | register |
Register the device as a GATT server with UART service. |
| Method | remove |
Remove a paired device from storage. |
| Method | save |
Save a paired device to storage. |
| Method | scan |
Start scanning for BLE devices (central mode). |
| Method | scan |
Scan for devices advertising the UART service (central mode). |
| Method | scan |
Stop an ongoing scan. |
| Method | send |
Send data to all connected central devices (peripheral mode). |
| Method | start |
Start as a BLE peripheral (GATT server) with UART service. |
| Method | stop |
Stop peripheral mode (stop advertising). |
| Method | subscribe |
Subscribe to notifications from a characteristic (central mode). |
| Method | write |
Write data to the connected peripheral (central mode). |
| Property | callback |
Get the current Bluetooth callback. |
| Property | central |
Get the set of connected central device handles (peripheral mode). |
| Property | characteristics |
Get discovered characteristics. |
| Property | connected |
Get the address of the connected device. |
| Property | is |
Check if connected to a peripheral (central mode). |
| Property | is |
Check if currently in pairing process. |
| Property | is |
Check if any central is connected (peripheral mode). |
| Property | is |
Check if Bluetooth is currently scanning. |
| Property | mac |
Get the MAC address of this Bluetooth device. |
| Property | passkey |
Get the current passkey during pairing (if any). |
| Property | services |
Get discovered services. |
| Method | __irq |
Handle Bluetooth IRQ events for both central and peripheral modes. |
| Method | _advertising |
Generate advertising payload. |
| Method | _start |
Start advertising with the registered payload. |
| Instance Variable | _adv |
Undocumented |
| Instance Variable | _ble |
Undocumented |
| Instance Variable | _callback |
Undocumented |
| Instance Variable | _central |
Undocumented |
| Instance Variable | _characteristics |
Undocumented |
| Instance Variable | _conn |
Undocumented |
| Instance Variable | _connected |
Undocumented |
| Instance Variable | _connected |
Undocumented |
| Instance Variable | _end |
Undocumented |
| Instance Variable | _notify |
Undocumented |
| Instance Variable | _pairing |
Undocumented |
| Instance Variable | _passkey |
Undocumented |
| Instance Variable | _peripheral |
Undocumented |
| Instance Variable | _peripheral |
Undocumented |
| Instance Variable | _peripheral |
Undocumented |
| Instance Variable | _rx |
Undocumented |
| Instance Variable | _scan |
Undocumented |
| Instance Variable | _scanning |
Undocumented |
| Instance Variable | _services |
Undocumented |
| Instance Variable | _start |
Undocumented |
| Instance Variable | _storage |
Undocumented |
| Instance Variable | _tx |
Undocumented |
| Instance Variable | _write |
Undocumented |
Initialize Bluetooth.
| Parameters | |
storage:Storage | Storage instance for saving paired device keys. Defaults to None. |
Start or stop BLE advertising to make this device discoverable.
| Parameters | |
intervalint | Advertising interval in microseconds. None stops advertising. Defaults to None. |
name:str | Device name to advertise. Defaults to "Picoware". |
| Returns | |
bool | True if advertising started or stopped successfully. |
Set the Bluetooth callback function.
The callback receives (event, data) where event is one of the _IRQ_* constants.
| Parameters | |
func:callable | The callback function to set. |
Connect to a BLE peripheral device (central mode).
| Parameters | |
addrint | Address type (0 = public, 1 = random). |
addr:bytes | Device address as bytes. |
timeoutint | Connection timeout in milliseconds. Defaults to 10000. |
autobool | Whether to automatically discover services. Defaults to True. |
| Returns | |
bool | True if the connection was initiated successfully. |
Decode device name from advertising data.
| Parameters | |
advbytes | Raw advertising data bytes. |
| Returns | |
str | The device name or empty string if not found. |
Decode service UUIDs from advertising data.
| Parameters | |
advbytes | Raw advertising data bytes. |
| Returns | |
list | List of UUID objects found in the advertising data. |
Disconnect from a device.
Works for both modes: - Central mode: Disconnects from the connected peripheral - Peripheral mode: Disconnects a specific central if conn_handle provided
| Parameters | |
connint | Connection handle to disconnect. Uses the current connection if None. Defaults to None. |
Discover characteristics for a service.
Results are delivered via callback with _IRQ_GATTC_CHARACTERISTIC_RESULT events.
| Parameters | |
startint | Service start handle. |
endint | Service end handle. |
| Returns | |
bool | True if discovery was initiated. |
Discover services on the connected peripheral.
Results will be delivered via callback with _IRQ_GATTC_SERVICE_RESULT events.
Check if a device address is in the paired devices list.
| Parameters | |
addr:str | The device address to check. |
| Returns | |
bool | True if the device is paired. |
Check if UART service is fully discovered and ready (central mode).
| Returns | |
bool | True if both TX and RX handles are discovered |
Set callback for when data is received from a peripheral (central mode).
The callback receives the data bytes as its argument.
| Parameters | |
callback:callable | Function to call with received data. |
Set callback for scan results (central mode).
The callback receives (addr_type, addr, name, rssi, adv_data) per device.
| Parameters | |
callback:callable | Function to call for each scan result. |
Set callback for when data is received from a central (peripheral mode).
The callback receives the data bytes as its argument.
| Parameters | |
callback:callable | Function to call with received data. |
Initiate pairing/bonding with the connected device.
Call this after connecting to establish an encrypted bond. The device will be remembered for future connections.
Reply to a passkey request during pairing.
| Parameters | |
accept:bool | Whether to accept the pairing. Defaults to True. |
passkey:int | The passkey to use for input actions. Defaults to None. |
Read data from a characteristic.
Result is delivered via callback with _IRQ_GATTC_READ_RESULT event.
| Parameters | |
handle:int | Characteristic handle to read from. |
| Returns | |
bool | True if the read was initiated. |
Register the device as a GATT server with UART service.
This allows the device to accept incoming connections and provide services/characteristics to connected centrals.
| Returns | |
bool | True if registration successful |
Remove a paired device from storage.
| Parameters | |
addr:str | Device address string to remove. |
| Returns | |
bool | True if the device was removed. |
Save a paired device to storage.
| Parameters | |
addr:str | Device address string. |
name:str | Device name. Defaults to "". |
| Returns | |
bool | True if the device was saved. |
Start scanning for BLE devices (central mode).
| Parameters | |
durationint | How long to scan in milliseconds. Defaults to 5000; 0 scans continuously. |
intervalint | Scan interval in microseconds. Defaults to 30000. |
windowint | Scan window in microseconds. Defaults to 30000. |
active:bool | Whether to request scan response data. Defaults to True. |
callback:callable | Optional callback for scan results (addr_type, addr, name, rssi, adv_data). Defaults to None. |
| Returns | |
bool | True if the scan started successfully. |
Scan for devices advertising the UART service (central mode).
| Parameters | |
callback:callable | Function called with (addr_type, addr, name) per UART device found. |
durationint | Scan duration in milliseconds. Defaults to 5000. |
| Returns | |
bool | True if the scan started. |
Send data to all connected central devices (peripheral mode).
Sends a notification to all connected centrals.
| Parameters | |
data:bytes or str | Data to send. |
| Returns | |
bool | True if sent to at least one central. |
Start as a BLE peripheral (GATT server) with UART service.
Registers the UART service and starts advertising so central devices can connect and communicate.
| Parameters | |
name:str | Device name to advertise. Defaults to "Picoware". |
intervalint | Advertising interval in microseconds. Defaults to 500000. |
| Returns | |
bool | True if started successfully. |
Subscribe to notifications from a characteristic (central mode).
| Parameters | |
handle:int | Characteristic value handle. Uses the UART TX handle if None. Defaults to None. |
notify:bool | True for notifications, False to unsubscribe. Defaults to True. |
| Returns | |
bool | True if the subscription request was sent. |
Write data to the connected peripheral (central mode).
| Parameters | |
data:bytes or str | Data to write. |
handle:int | Characteristic handle to write to. Uses the UART RX handle if None. Defaults to None. |
response:bool | Whether to request a write response. Defaults to False. |
| Returns | |
bool | True if the write was initiated. |
Handle Bluetooth IRQ events for both central and peripheral modes.
| Parameters | |
event:int | The IRQ event constant. |
data:tuple | Event-specific data. |
Generate advertising payload.
| Parameters | |
name:str | Device name to advertise. Defaults to None. |
services:list | List of service UUIDs to advertise. Defaults to None. |
appearance:int | Device appearance code. Defaults to 0. |
| Returns | |
bytearray | The advertising payload. |