bledev.battery

Battery Service (0x180F): report a battery level that a phone or laptop can read.

Serve it beside your own service, or alone:

import bledev
from bledev.battery import Battery

battery = Battery(level=87)
ble.register_services(battery.service)
connection = await ble.advertise(name="sensor", services=[battery.SERVICE])
battery.set_level(86)       # centrals that subscribed are notified

ble.register_services() replaces whatever was registered before, so pass this service together with the others. Centrals that haven’t subscribed to Battery Level read the current value instead of being notified.

Any peripheral backend serves it: MicroPython (bledev.mpble), CircuitPython (bledev.cpble) and the fake one for tests.

class bledev.battery.Battery(level=100)[source]

Bases: object

A Battery Service holding one Battery Level characteristic.

Parameters:

level – The charge, 0 to 100 percent.

characteristic

read and notify.

Type:

The Battery Level bledev.Characteristic

property level

The charge last set, 0 to 100 percent.

set_level(level)[source]

Set the charge in percent and notify every central subscribed to it.