Adding Characteristic User Description to multiple custom C++ BLE GATT Service

后端 未结 1 480
后悔当初
后悔当初 2021-01-27 15:19

I am trying to add some Characteristic User Descriptions to my custom BLE GATT Service, using the mbed API. My work has so far been based on this code structure. However, I woul

1条回答
  •  天涯浪人
    2021-01-27 15:57

    Here is a proposition of template helper class that can encapsulate the characteristic object and its descriptor. It's a bit difficult to understand if you are not familiar with templates.

    template  class CharacType>
    class CharacteristicWithNameDescrptorHelper
    {
    public:
        CharacteristicWithNameDescrptorHelper( const          UUID &uuid,
                                               T              valuePtr[NUM_ELEMENTS],
                                               uint8_t        additionalProperties,
                                               const std::string& characName ) : 
            name( characName )
        {
            descriptor = new GattAttribute( BLE_UUID_DESCRIPTOR_CHAR_USER_DESC, (uint8_t *)name.c_str(), name.size() ) 
            // create descriptor array
            descriptors[0] = descriptor;
            // create characteristic object:
            charac = new CharacType( uuid, valuePtr, additionalProperties, descriptors, 1 );
        }
    
        ~CharacteristicWithNameDescrptorHelper()
        {
            delete charac;
            delete descriptor;
        }
    
        CharacType* charac;
        std::string name;
        GattAttribute* descriptor;
        GattAttribute *descriptors[1];
    };
    

    Then, you simply do:

    CharacteristicWithNameDescrptorHelper 
            percentageChar( PercentageUUID, 
                            percentageValue,
                            GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_EXTENDED_PROPERTIES,
                            "Percentage" );
    
    GattCharacteristic *characteristics[] = {percentageChar.charac};
    GattService        newService(newServiceUUID, characteristics, sizeof(characteristics) / sizeof(GattCharacteristic *));
    

    0 讨论(0)
提交回复
热议问题