将特征用户描述添加到多个自定义 C++ BLE GATT 服务

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

我正在尝试使用 mbed API. My work has so far been based on this 代码结构向我的自定义 BLE GATT 服务添加一些特征用户描述。但是,我想为这些特征添加名称。关于如何执行此操作,我找不到太多信息。但是,下面是将信息添加到特征的代码。

GattCharacteristic() 的构造函数将 GattAttribtues 数组作为可选参数。您可以将用户描述填充到 GattAttribute 中并将其传递给特性。我有一个适用于一个特征的结构,但我正在努力为 3 个字符复制它。我不能将整个事情复制 3 次,因为我 运行 它涉及许多关于数组等的问题。已经定义。如果我将描述堆叠在数组中,它不会被 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 *));

编辑

通过下面的讨论,我现在有:

#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];
};

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

这会构建,但不会为特征命名。

这里有一个template helper的命题class,可以封装特征对象及其描述符。如果对模板不熟悉,理解起来有点困难。

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];
};

然后,您只需执行:

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