通过 BLE 从 PolarH10 获取数据

Getting Data from PolarH10 via BLE

我一直在尝试使用我的树莓派从我的 PolarH10 获取数据。我已经使用 bluez 通过命令行成功获取数据,但无法在 python 中重现该数据。我正在使用 pygatt(gatttool 绑定)和 python3.

我一直在密切关注 bitbucket 上提供的示例,并且能够检测到我的设备并通过按名称过滤来过滤掉它的 MAC 地址。然而,我无法让 "reading data asyncronously" 中的任何一个示例正常工作。


#This doesnt work...
req = gattlib.GATTRequester(mymac)
response = gattlib.GATTResponse()

req.read_by_handle_async(0x15, response) # what does the 0x15 mean?
while not response.received():
    time.sleep(0.1)

steps = response.received()[0]


...

#This doesn't work either
class NotifyYourName(gattlib.GATTResponse):
    def on_response(self, data):
        print("your data is: {}".format(data))

response = NotifyYourName()
req = gattlib.GATTRequester(mymac)
req.read_by_handle_async(0x15, response)

while True:
    # here, do other interesting things
    time.sleep(1)

我不知道也无法从 "documentation(s)" 中提取如何从我的传感器 (PolarH10) 的特征(心率)订阅 to/read 通知。我得到的错误是调用 GATTRequester.connect(True)

RuntimeError: Channel or attrib not ready.

请告诉我如何在 Debian 上通过 Python 正确连接到 BLE 设备以及如何以编程方式识别提供的服务及其特征以及如何在 python 中使用 gattlib(pygatt ) 或任何其他库。谢谢!

答案是:只需使用bleak

我的设备出现了相同的行为。在我的例子中,问题是它没有 public 类型的通道,我应该使用 random 代替(如 gatttool -b BE:BA:CA:FE:BA:BE -I -t random)。

只需调用带有参数 channel_typerandomconnect() 方法即可修复它:

requester.connect(True, channel_type="random")

PD:抱歉回复晚了(也许对其他人有帮助)。