2

I'm able to traverse the services and characteristics of my BLE device in bluepy, but I'm unable to get asynchronous indications to print. I've looked at the suggestions in this question, but I can't get it working.

It does work when using gattool:

$ sudo gatttool -b "DF:01:93:A9:86:FF" -t random  --char-write-req --handle=0x002c --value=0200 --listen
Characteristic value was written successfully
Indication   handle = 0x002b value: 00 8a 00 
Indication   handle = 0x002b value: 00 8a 00 
Indication   handle = 0x002b value: 30 30 2d

But I can't get bluepy to do the same:

from bluepy.btle import Scanner, DefaultDelegate, Peripheral, ADDR_TYPE_RANDOM

class ReadCharacteristicDelegate(DefaultDelegate):
    def __init__(self):
        DefaultDelegate.__init__(self)

    def handleNotification(self, cHandle, data):
        print "  got data: 0x%x" % data

periph = Peripheral('df:01:93:a9:86:ff', addrType=ADDR_TYPE_RANDOM)
periph.setDelegate(ReadCharacteristicDelegate())
periph.writeCharacteristic(0x002c, "\0x02\0x00")
print "Enabled indications"
while True:
    if periph.waitForNotifications(3.0):
        # handleNotification() was called
        continue
    print("Waiting")

It runs, but no indications ever print out:

$ sudo python simple.py 
Enabled indications
Waiting
Waiting

1 Answers1

1

I found the issue. There was a typo on the writeCharacteristic() call. Apparently the bytes shouldn't have a leading '0' in them. Once that was fixed, the data printing callback also had to be updated to handle the 3 bytes of data coming in. This now works:

from bluepy.btle import Scanner, DefaultDelegate, Peripheral, ADDR_TYPE_RANDOM
from struct import unpack

class ReadCharacteristicDelegate(DefaultDelegate):
    def __init__(self):
        DefaultDelegate.__init__(self)

    def handleNotification(self, cHandle, data):
        print "got handle: 0x%x  data: 0x%x" % (cHandle, unpack('>i','\x00'+data)[0])

periph = Peripheral('df:01:93:a9:86:ff', addrType=ADDR_TYPE_RANDOM)
periph.setDelegate(ReadCharacteristicDelegate())
periph.writeCharacteristic(0x002c, b"\x02\x00")
print "Enabled indications"
while True:
    if periph.waitForNotifications(3.0):
        # handleNotification() was called
        continue
    print("Waiting")