0

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

GattCharacteristic() 的构造函数将 GattAttribtues 数组作为可选参数。您可以将 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*) );

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

4

1 回答 1

0

这里有一个模板助手类的命题,可以封装特征对象及其描述符。如果您不熟悉模板,则有点难以理解。

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 *));
于 2015-10-27T05:45:27.117 回答