小编Ale*_*.It的帖子

BLE使用gatttool或bluepy订阅通知

我正在编写一个程序,使用bluepy来监听蓝牙设备发送的特性.我也可以使用任何库或语言,唯一的限制是在Linux上运行而不是在移动环境中运行(它似乎只在移动设备中广泛使用,没有人使用BLE与桌面).使用bluepy我注册了委托,并在尝试注册write('\x01\x00')蓝牙rfc中描述的通知调用之后.但它不起作用,收到任何关于特征的通知.也许我在编写订阅消息时错了.我写的小片段中有错误吗?非常感谢.

class MyDelegate(btle.DefaultDelegate):

    def __init__(self, hndl):
        btle.DefaultDelegate.__init__(self)
   self.hndl=hndl;

   def handleNotification(self, cHandle, data):
   if (cHandle==self.hndl):
            val = binascii.b2a_hex(data)
            val = binascii.unhexlify(val)
            val = struct.unpack('f', val)[0]
            print str(val) + " deg C"


p = btle.Peripheral("xx:xx:xx:xx", "random")

try:
   srvs = (p.getServices());
   chs=srvs[2].getCharacteristics();
   ch=chs[1];
   print(str(ch)+str(ch.propertiesToString()));
   p.setDelegate(MyDelegate(ch.getHandle()));
   # Setup to turn notifications on, e.g.
   ch.write("\x01\x00");

   # Main loop --------
   while True:
      if p.waitForNotifications(1.0):
      continue

      print "Waiting..."
finally:
    p.disconnect();
Run Code Online (Sandbox Code Playgroud)

python linux bluetooth-lowenergy gatt

5
推荐指数
2
解决办法
6366
查看次数

StreamReader.Read()是阻塞方法吗?

StreamReader.Read()特别是如果应用到阻塞的方法NetworkStream

c# networkstream streamreader

0
推荐指数
1
解决办法
666
查看次数