如果您查看Bluetooth assigned 16-bit UUIDs numbers,那么您认为0x180D 是心率服务是正确的
心率测量特性 0x2A37 将具有您正在寻找的值:
我没有用于测试此功能的设备,而且我不知道您正在为哪个平台编写代码。
因此,我使用了bleak 库,因为它是最跨平台的库。
该示例还订阅了来自设备的通知,而不是读取值。这是获取定期更新的值的更典型方法。
import asyncio
import bitstruct
import struct
from bleak import BleakClient
HR_MEAS = "00002A37-0000-1000-8000-00805F9B34FB"
async def run(address, debug=False):
async with BleakClient(address) as client:
connected = await client.is_connected()
print("Connected: {0}".format(connected))
def hr_val_handler(sender, data):
"""Simple notification handler for Heart Rate Measurement."""
print("HR Measurement raw = {0}: {1}".format(sender, data))
(hr_fmt,
snsr_detect,
snsr_cntct_spprtd,
nrg_expnd,
rr_int) = bitstruct.unpack("b1b1b1b1b1<", data)
if hr_fmt:
hr_val, = struct.unpack_from("<H", data, 1)
else:
hr_val, = struct.unpack_from("<B", data, 1)
print(f"HR Value: {hr_val}")
await client.start_notify(HR_MEAS, hr_val_handler)
while await client.is_connected():
await asyncio.sleep(1)
if __name__ == "__main__":
address = ("xx:xx:xx:xx:xx:xx") # Change to address of device
loop = asyncio.get_event_loop()
loop.run_until_complete(run(address))
您可以阅读更多有关心率测量值构建于:
https://www.bluetooth.com/specifications/specs/gatt-specification-supplement-6/