目前正在使用OH1心率传感器(tiny.cc/mom03y)
我正在寻找使用bluepy订阅人力资源通知。我有通知工作,但OH1设备在bluepy和gatttool(远程用户终止)约20-30secs后断开,但不在bluetoothctl。
寻找连接在bluetoothctl中保持活动的原因,而不是在bluepy或gatttool,代码和hcidump下面,在rasbian 4.14上使用bluez 5.50和bluepy 1.30。
Bluepy
#packet count
packets = 0
class hrCallback(btle.DefaultDelegate):
def __init__(self):
btle.DefaultDelegate.__init__(self)
def handleNotification(self, cHandle, data):
global packets
packets += 1
print("packet: %s Handle: %s HR (bpm): %s " % (packets, cHandle, data[1]))
#connect to OH1
mac = "a0:9e:1a:4f:ef:8b"
oh1 = btle.Peripheral( mac )
oh1.setDelegate( hrCallback() )
#start hr notification
oh1.writeCharacteristic(38, b"\x01\x00", True)
#listen for notifications
while True:
try:
if oh1.waitForNotifications(1.0):
continue
except btle.BTLEDisconnectError:
pass
hcidump
> HCI Event: Command Complete (0x0e) plen 4
LE Set Scan Parameters (0x08|0x000b) ncmd 1
status 0x00
> HCI Event: Command Complete (0x0e) plen 4
LE Set Scan Enable (0x08|0x000c) ncmd 1
status 0x00
> HCI Event: Command Complete (0x0e) plen 4
LE Set Scan Enable (0x08|0x000c) ncmd 1
status 0x00
> HCI Event: Command Status (0x0f) plen 4
LE Create Connection (0x08|0x000d) status 0x00 ncmd 1
> HCI Event: Command Status (0x0f) plen 4
LE Read Remote Used Features (0x08|0x0016) status 0x00 ncmd 1
> HCI Event: Disconn Complete (0x05) plen 4
status 0x00 handle 64 reason 0x13
Reason: Remote User Terminated Connection
您使用的是哪个版本的BlueZ?几个工具(例如gatttool,hcitool,hciconfig)已弃用并由bluetoothctl和btmgmt取代,BlueZ团队的建议是使用新工具。请看看以下链接: -
新工具和旧工具之间的区别在于旧工具能够直接与内核连接,而新工具则通过与D-Bus接口来执行操作。
因此,建议始终使用bluetoothctl,因为旧工具不能维护,这可能是您看到问题的原因。
我希望这有帮助。