BLE使用gatttool或bluepy订阅通知 [英] BLE subscribe to notification using gatttool or bluepy

查看:740
本文介绍了BLE使用gatttool或bluepy订阅通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用bluepy编写程序,以侦听蓝牙设备发送的特征.我还可以使用任何库或语言,唯一的限制是要在Linux上而不是在移动环境中运行(似乎仅在移动设备中广泛使用,没有人在桌面上使用BLE). 我使用bluepy注册了代理,并尝试注册通知write('\x01\x00')的通知后,如蓝牙rfc中所述. 但这是行不通的,不会收到有关该特性的任何通知. 在撰写订阅消息时,也许我是错误的. 我写的小片段中有错误吗?非常感谢.

I am writing a program using bluepy that listen for a characteristic sent by a bluetooth device. I can also use any library or language, the only constraint is to run on Linux and not in mobile environment (it seems is widely used only in mobile devices, no one use BLE with desktop). Using bluepy I register the delegate and after trying to register for notification calling write('\x01\x00') as described in the bluetooth rfc. But it doesn't work, any notification for the characteristic is received. Maybe I am wrong in writing the message for subscribing. Is there an error in the small snippet I wrote? Thank you so much.

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();

推荐答案

我自己为此苦苦挣扎,而jgrant的评论确实有所帮助.如果可以帮助任何人,我想分享我的解决方案.

I was struggling with this myself, and jgrant's comment really helped. I'd like to share my solution, if it could help anyone.

请注意,我需要指示,因此是x02而不是x01.

Note that I needed indication, hence the x02 rather than x01.

如果可以使用bluepy读取描述符,我会这样做,但是似乎不起作用(bluepy v 1.0.5).服务类中的方法似乎丢失了,而当我尝试使用它时,外围类中的方法被卡住了.

If it were possible to read the descriptors using bluepy, I would do that, but it doesn't seem to work (bluepy v 1.0.5). The method in the service class appears to be missing, and the method in the peripheral class gets stuck when I try to use it.

from bluepy import btle

class MyDelegate(btle.DefaultDelegate):
    def __init__(self):
        btle.DefaultDelegate.__init__(self)

    def handleNotification(self, cHandle, data):
        print("A notification was received: %s" %data)


p = btle.Peripheral(<MAC ADDRESS>, btle.ADDR_TYPE_RANDOM)
p.setDelegate( MyDelegate() )

# Setup to turn notifications on, e.g.
svc = p.getServiceByUUID( <UUID> )
ch = svc.getCharacteristics()[0]
print(ch.valHandle)

p.writeCharacteristic(ch.valHandle+1, "\x02\x00")

while True:
    if p.waitForNotifications(1.0):
        # handleNotification() was called
        continue

    print("Waiting...")
    # Perhaps do something else here

这篇关于BLE使用gatttool或bluepy订阅通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆