无法弄清楚为什么我没有收到通知



我从未收到来自 6E40000 6-B5A3-F393-E0A9-E50E24DCCA9E 的通知。我必须通过写入特性 6E40000 9-B5A3-F393-E0A9-E50E24DCCA9E0x01来启用传感器。我正在使用Bluepy Python库。

使用LightBlue iOS应用程序验证了服务和特征。 日志:

21:58:33.956 — Characteristic (6E400009-B5A3-F393-E0A9-E50E24DCCA9E) wrote new value: <01>
21:58:33.999 — Characteristic (6E400009-B5A3-F393-E0A9-E50E24DCCA9E) read: (null)
21:58:39.339 — Characteristic (6E400006-B5A3-F393-E0A9-E50E24DCCA9E) notified: <06020341 090000>
21:58:39.849 — Characteristic (6E400006-B5A3-F393-E0A9-E50E24DCCA9E) notified: <06020338 090000>
21:58:40.329 — Characteristic (6E400006-B5A3-F393-E0A9-E50E24DCCA9E) notified: <0602033f 090000>
21:58:40.840 — Characteristic (6E400006-B5A3-F393-E0A9-E50E24DCCA9E) notified: <06020338 090000>
21:58:41.349 — Characteristic (6E400006-B5A3-F393-E0A9-E50E24DCCA9E) notified: <0602033a 090000>
from bluepy.btle import UUID, Peripheral, DefaultDelegate
import time
import struct
class MyDelegate(DefaultDelegate):
def __init__(self):
DefaultDelegate.__init__(self)
def handleNotification(self, cHandle, data):
print(cHandle, data)
#Should recieve notification from 6e400006-b5a3-f393-e0a9-e50e24dcca9e
#Connect to device
print('Connecting...')    
dev = Peripheral("df:4c:cf:e7:9b:1f")
#Setup Notifications
dev.setDelegate( MyDelegate() )
#Get sensor services
sensorServices = dev.getServiceByUUID( UUID("6E400000-B5A3-F393-E0A9-E50E24DCCA9E") )
print(sensorServices)
#Get Stream Characteristic
enableStreamCharacteristic = sensorServices.getCharacteristics( UUID('6E400009-B5A3-F393-E0A9-E50E24DCCA9E') )[0]
print(enableStreamCharacteristic)
#Write 0x01 to 6e400009-b5a3-f393-e0a9-e50e24dcca9e
enableStreamCharacteristic.write( struct.pack('<B', 0x01) )
print("Waiting...")
while True:
if dev.waitForNotifications(1.0):
# handleNotification() was called
continue
dev.disconnect()
print('Done')

结果:

Connecting...
Service <uuid=6e400000-b5a3-f393-e0a9-e50e24dcca9e handleStart=9 handleEnd=65535>
Characteristic <6e400009-b5a3-f393-e0a9-e50e24dcca9e>
Waiting...

您没有收到通知的原因是您需要先启用特定特征的通知。将此添加到您的代码中:

声明一些值以供在必要时使用

notiOn = b"x1x00"
notiOff = b"x1x00"

在开始侦听通知之前,请为发送通知的特征启用它们(在您的情况下可能是 6e400001-6e400008 之间的特征(:

yourCharacteristicDesc = yourCharacteristic.getDescriptors()[0]
yourCharacteristicDesc.write(notifOn, withResponse=True)

添加此代码后,您应该开始接收通知。

相关内容

最新更新