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

眉间皱痕 提交于 2019-12-02 11:27:43

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 *));
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!