我正在尝试使用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*) );
这会构建,但不会为特征命名。