[英]Trying to finish this program for my python class, but can't figure out why I'm receiving a TypeError
[英]Can't figure out why I'm not receiving notifications
我從未收到來自 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...
您沒有收到通知的原因是您需要首先為特定特征啟用通知。 將此添加到您的代碼中:
聲明一些值以在必要時使用
notiOn = b"\0x1\x00"
notiOff = b"\0x1\x00"
在您開始收聽通知之前,請為發送通知的特征啟用它們(在您的情況下,可能是 6e400001-6e400008 之間的一個):
yourCharacteristicDesc = yourCharacteristic.getDescriptors()[0]
yourCharacteristicDesc.write(notifOn, withResponse=True)
添加此代碼后,您應該開始接收通知。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.