How to write a client characteristic configuration

2019-09-06 23:50发布

问题:

I am working with the example-gatt-server.py script that comes with bluez on my linux board. I want to add notification to one of my custom characteristics. For that I need to define the Client Characteristic Configuration Descriptor and add it to my custom characteristic. Here is how I am doing this -

class ClientCharacteristicConfigurationDescriptor(Descriptor):

CCCD_UUID = '2902'

def __init__(self, bus, index, characteristic):
    self.value = array.array('B')
    self.value = self.value.tolist()
    #self.value = []

    Descriptor.__init__(
            self, bus, index,
            self.CCCD_UUID,
            ['read', 'write'],
            characteristic)

def ReadValue(self):
    print("I am reading CCCD value")
    print(self.value)
    return self.value

def WriteValue(self, value):
    print("I am writing CCCD value")
    print type(value)
    #self.value = value
    print(value)

This code was inspired by the CharacteristicUserDescriptionDescriptor class that already comes defined in the example-gatt-server file. The above code gives me errors while reading or writing. It doesn't even print the "I am reading CCCD value" statement. What am I missing here?

Thanks!

回答1:

Bluez handles the Client Characteristic Configuration Descriptor (CCCD). You should not need to define it yourself in your code.
Notification support should transparently be handled by Bluez if you have defined the flag 'notify' for the corresponding characteristic.

As you noticed example-gatt-server defines Characteristic User Description (CUD) and not CCCD.