通过BLE从PolarH10获取数据

时间:2019-06-19 13:40:17

标签: python-3.x bluetooth-lowenergy raspberry-pi3 gatt

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

我一直密切关注bitbucket上提供的示例,并且能够通过按名称过滤设备来检测我的设备并过滤出其MAC地址。但是,我无法使任何一个“异步读取数据”示例都能正常工作。


#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)

我不知道并且不能从“文档”中提取如何从传感器(PolarH10)的特征(心率)中订阅/读取通知。我收到的错误是调用GATTRequester.connect(True)

RuntimeError: Channel or attrib not ready

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

2 个答案:

答案 0 :(得分:2)

我有一个呈现相同行为的设备。就我而言,问题在于它没有类型为public的频道,我应该改用random(例如在gatttool -b BE:BA:CA:FE:BA:BE -I -t random中)。

只需使用参数connect()channel_type调用random方法就可以解决此问题:

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

PD:对不起,您的回复太晚了(也许会对其他人有帮助)。

答案 1 :(得分:0)

答案是:只需使用{{3}}。

相关问题