【发布时间】:2022-12-10 21:42:45
【问题描述】:
我有一个 AS97 健身手表,想使用 pygatt 通过树莓派接收活动数据。
我必须写入自定义服务 uuid。
我需要 device.char_write 到 uuid ff01 并收到来自 ff02 的指示。
如果我只使用device.char_read,我只会得到第一个指示。
我尝试了 device.subscribe 函数,但这不起作用。
#!/usr/bin/env python3
import pygatt
import logging
from binascii import hexlify
from time import sleep
logging.basicConfig()
logging.getLogger('pygatt').setLevel(logging.DEBUG) # Logger to see the debug
watch = "C4:C1:88:F8:58:59" # watch bluetooth-adress
adapter = pygatt.GATTToolBackend()
adapter.start() # start bluetooth
device = adapter.connect(watch, address_type=pygatt.BLEAddressType.random, timeout=20)
#connects with watch
try:
device.char_write('7905ff01-b5ce-4e99-a40f-4b1e122d00d0',
bytearray([0xBE, 0x02, 0x01, 0xFE, 0x07, 0xE6, 0x01, 0x19, 0x00,
0x00])) #command bytes - 07e6 year - 01 month
# - 19 day - 0000 - minute start
activity_data = device.char_read('7905ff02-b5ce-4e99-a40f-4b1e122d00d0')
except:
print("Can't read informations")
adapter.stop()
print(activity_data)
adapter.stop()
我收到第一个指示:
bytearray(b'\xde\x02\x01\xfe\x07\xe6\x01\x19\x00\x00\x0bz\x00\x00\x07M\x00\xc8\x00')
Get activity data 2nd 3rd indication
我猜读取功能没有很好地工作,因为该特性仅使用写入和指示。
感谢大家的帮助!
我也试过类似的东西:
def data_handler_cb(handle, value):
print("Data: {}".format(value.hex()))
print("Handle: {}".format(handle))
try:
device.char_write('7905ff01-b5ce-4e99-a40f-4b1e122d00d0',
bytearray([0xBE, 0x02, 0x01, 0xFE, 0x07, 0xE6, 0x01, 0x19, 0x00, 0x00]))
device.subscribe("7905ff02-b5ce-4e99-a40f-4b1e122d00d0", callback=data_handler_cb, indication = True)
print("Wait for callback")
sleep(3)
finally:
adapter.stop()
我也换了顺序,先订阅,然后就等了
DEBUG:pygatt.device:Looking up handle for characteristic 7905ff02-b5ce-4e99-a40f-4b1e122d00d0
DEBUG:pygatt.device:Found <Characteristic uuid=7905ff02-b5ce-4e99-a40f-4b1e122d00d0 handle=26>
DEBUG:pygatt.backends.gatttool.gatttool:Sending cmd=char-write-req 0x1b 0200
并因错误而中断:
ERROR:pygatt.backends.gatttool.gatttool:No response received
【问题讨论】:
-
您使用 pygatt 库是否有特殊原因?它似乎是基于
gatttool而一直是 deprecated。如果您确实想使用它,则需要使用 subscribe 功能从设置了indicate标志的特征接收数据。 -
不,我没有理由使用 pygatt,但我没有在树莓派上找到 python 3.x 的其他库。你能帮我选择另一个图书馆或解决这个订阅问题吗?
标签: bluetooth raspberry-pi bluez gatt