向多个自定义c++ BLE GATT服务添加特征用户描述



我正在尝试使用mbed API为我的自定义BLE GATT服务添加一些特征用户描述。到目前为止,我的工作都是基于这种代码结构。然而,我想为这些特征添加一些名称。关于如何做到这一点,我找不到太多的信息。然而,下面是将信息添加到特征的代码。

GattCharacteristic()的构造函数接受一个gattattributes数组作为可选参数。您可以将您的User-Description填充到GattAttribute中,并将其传递给Characteristic。我有这个结构工作的一个特点,但我努力复制它的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 <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 *));

最新更新