BLE sign up for a notification using gatttool or bluepy

I am writing a program using bluepy that listens for a feature sent by a Bluetooth device. I can also use any library or language, the only restriction is to work on Linux, and not in a mobile environment (it seems to be widely used only on mobile devices, no one uses BLE with the desktop). Using bluepy, I will register the delegate and after trying to register for the notification calling write('\x01\x00') , as described in bluetooth rfc. But this does not work, a notification about this symptom is received. Perhaps I am mistaken in writing a subscription message. Is there a mistake in the small snippet I wrote? Thank you very 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, eg ch.write("\x01\x00"); # Main loop -------- while True: if p.waitForNotifications(1.0): continue print "Waiting..." finally: p.disconnect(); 
+5
source share
3 answers

I struggled with this myself and the jgrant comment really helped. I would like to share my decision if this can help anyone.

Note that I need an indication, therefore, x02, not x01.

If you could read the descriptors using bluepy, I would do it, but it does not work (bluepy v 1.0.5). The method in the service class seems 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, eg 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 
+4
source

It seems like the problem is that you are trying to write \x01\x00 to the \x01\x00 itself. You need to write it to the configuration descriptor of the characteristics of the client that executes it (0x2902). The handle is probably 1 larger than the characteristic (but you can confirm by reading the descriptors).

 ch=chs[1] cccd = ch.valHandle + 1 cccd.write("\x01\x00") 
+2
source

What bothered me was that in the https://ianharvey.imtqy.com/bluepy-doc/notifications.html the part that included the notifications was in the comments, so it did not look obligatory for me.

minimum minimum (given that you already know the MAC address and you included everything and declared a delegate class) for me

 p1 = Peripheral(<MAC>) ch1 = p1.getCharacteristics()[3] p1.setDelegate(MyDelegate()) p1.writeCharacteristic(ch1.valHandle + 1, b"\x01\x00") 

Please note that I already knew that I want to receive notifications from feature # 3. Also, without the 'b' -bytesprefix before "\ x0 \ x00" this would not work for me.

0
source

Source: https://habr.com/ru/post/1232390/


All Articles