bledev.heartrate
Heart Rate Service (0x180D): a heart rate monitor that fitness apps can read.
Serve it, advertise it, and send a measurement whenever you have one:
from bledev.heartrate import HeartRate, CHEST
monitor = HeartRate(location=CHEST)
ble.register_services(monitor.service)
connection = await ble.advertise(name="HR", services=[monitor.SERVICE])
monitor.update(72, rr_ms=(830, 842)) # subscribed centrals are notified
The measurement carries the flags the specification defines: an 8-bit or
16-bit rate (chosen by the value), sensor contact status when you pass
contact, energy expended when you pass energy_kj, and RR-intervals
when you pass rr_ms. A notification fits mtu - 3 bytes, 20 until the
central asks for more, so a measurement takes at most nine RR-intervals
beside an 8-bit rate and nothing else.
Energy expended has a control point a central writes to reset the counter.
Give energy_expended=True and run await monitor.reset_requested()
where you zero your own count.
ble.register_services() replaces whatever was registered before, so pass
this service together with the others. Any peripheral backend serves it:
MicroPython (bledev.mpble), CircuitPython (bledev.cpble) and the fake one
for tests.
- bledev.heartrate.OTHER = 0
Body Sensor Location values.
- bledev.heartrate.encode_measurement(bpm, rr_ms=(), energy_kj=None, contact=None)[source]
The Heart Rate Measurement bytes: flags, rate, energy, RR-intervals.
Raises
ValueErrorfor a value that doesn’t fit or more RR-intervals than one default-MTU notification holds.
- class bledev.heartrate.HeartRate(location=None, energy_expended=False)[source]
Bases:
objectA Heart Rate Service.
- Parameters:
location – Where the sensor sits (
CHEST,WRIST, …), or None to leave out the Body Sensor Location characteristic.energy_expended – True to serve the control point that resets the energy count.
- characteristic
notify only.
- Type:
The Heart Rate Measurement
bledev.Characteristic
- update(bpm, rr_ms=(), energy_kj=None, contact=None)[source]
Send a measurement to every central subscribed to it.
- Parameters:
bpm – Beats per minute.
rr_ms – RR-intervals in milliseconds, oldest first.
energy_kj – Energy expended since the last reset, in kilojoules.
contact – True or False when the sensor can tell whether it touches skin; None when it can’t.