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

时间:2019-09-27 02:16:22

标签: python linux bluetooth-lowenergy btle

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

已使用LightBlue iOS App验证了服务和特性。日志:

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...

1 个答案:

答案 0 :(得分:0)

之所以没有收到通知,是因为您需要首先为特定特征启用通知。将此添加到您的代码中:

声明一些必要时使用的值

notiOn = b"\0x1\x00"
notiOff = b"\0x1\x00"

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

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

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