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

后端 未结 1 463
后悔当初
后悔当初 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 <typename T, unsigned NUM_ELEMENTS, template <typename T, unsigned NUM_ELEMENTS> 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<T,NUM_ELEMENTS>( uuid, valuePtr, additionalProperties, descriptors, 1 );
        }
    
        ~CharacteristicWithNameDescrptorHelper()
        {
            delete charac;
            delete descriptor;
        }
    
        CharacType<T,NUM_ELEMENTS>* charac;
        std::string name;
        GattAttribute* descriptor;
        GattAttribute *descriptors[1];
    };
    

    Then, you simply do:

    CharacteristicWithNameDescrptorHelper<uint8_t,sizeof(percentageValue),WriteOnlyArrayGattCharacteristic> 
            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)
提交回复
热议问题