我正在尝试使用以下命令将一些特征用户描述添加到我的自定义 BLE GATT 服务中mbed API https://developer.mbed.org/teams/Bluetooth-Low-Energy/。到目前为止我的工作是基于this https://developer.mbed.org/teams/Bluetooth-Low-Energy/code/BLE_GATT_Example/代码结构。不过,我想为这些特征添加名称。我找不到太多关于如何执行此操作的信息。但是,下面是将信息添加到特征的代码。
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 *));
EDIT
通过下面的讨论,我现在有了:
#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*) );
这会构建,但不会给该特征命名。