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
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 *));