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

你。 提交于 2019-12-04 06:09:46

问题


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 would like to add names to these characteristics. There isn't much info I could find on how to do this. However, below is the code which adds the information to a characteristic.

The constructor for GattCharacteristic() takes an array of GattAttribtues as an optional argument. You can populate your User-Description into a GattAttribute and pass it along to the Characteristic. I have this structure working for one Characteristic, but am struggling to replicate it for 3 Characters. I can't replicate the whole thing 3 times, as I run it to lots of issues about arrays etc. being already defined. If I stack the descriptions up in the array, it won't be accepted by the GattArray?

uint16_t newServiceUUID         = 0xA000;
uint16_t PercentageUUID         = 0xA001;
uint16_t TimeUUID               = 0xA002;
uint16_t UseProfileUUID         = 0xA003;

const static char     DEVICE_NAME[]        = "Device"; // Device name
static const uint16_t uuid16_list[]        = {0xFFF};  
static uint8_t percentageValue[10] = {0};
GattAttribute nameDescr( BLE_UUID_DESCRIPTOR_CHAR_USER_DESC, (uint8_t *)"Percentage", strlen("Percentage"));
GattAttribute *descriptors[] = {&nameDescr};

WriteOnlyArrayGattCharacteristic<uint8_t,sizeof(percentageValue)> 
        percentageChar( PercentageUUID, 
                        percentageValue,
                        GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_EXTENDED_PROPERTIES,
                        descriptors, 
                        sizeof(descriptors) / sizeof(GattAttribute*) );

GattCharacteristic *characteristics[] = {&percentageChar, &timeChar, &UseProfileChar};
GattService        newService(newServiceUUID, characteristics, sizeof(characteristics) / sizeof(GattCharacteristic *));

EDIT

Working with the discussion below, I now have:

#include <string>
class MyGattArray
{

public:
    MyGattArray( const std::string& name ) : 
        attr( BLE_UUID_DESCRIPTOR_CHAR_USER_DESC, (uint8_t *)name.c_str(), (name.size()+1) )
    {
        descriptors[0] = &attr;
    }

    GattAttribute attr;
    GattAttribute *descriptors[1];
};

and

static uint8_t percentageValue[10] = {0};
MyGattArray PercentageName( "Percentage" );
GattAttribute *descriptors[] = {&(PercentageName.attr)};

WriteOnlyArrayGattCharacteristic<uint8_t,sizeof(percentageValue)> 
        percentageChar( PercentageUUID, 
                        percentageValue,
                        GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_EXTENDED_PROPERTIES,
                        descriptors, 
                        sizeof(descriptors) / sizeof(GattAttribute*) );

This builds, but does not give the characteristic a name.


回答1:


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


来源:https://stackoverflow.com/questions/33343188/adding-characteristic-user-description-to-multiple-custom-c-ble-gatt-service

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