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

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

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

The constructor for GattCharacteristic() takes an array of GattAttribtues as an optional argument. You can populate your User-Description into a GattAttribute and pass it along to the Characteristic.

到目前为止,我已经有了这个结构来设置我的特征。

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};
WriteOnlyArrayGattCharacteristic<uint8_t,
        sizeof(percentageValue)> percentageChar(PercentageUUID, percentageValue);

static uint8_t timeValue[10] = {0};
ReadWriteArrayGattCharacteristic<uint8_t, 
        sizeof(timeValue)> timeChar(TimeUUID, timeValue);

static uint8_t UseProfileValue[10] = {0};
WriteOnlyArrayGattCharacteristic<uint8_t, 
        sizeof(UseProfileValue)> UseProfileChar(UseProfileUUID, UseProfileValue);

// Set up custom service

GattCharacteristic *characteristics[] = {&percentageChar, &timeChar, &UseProfileChar};
GattService        newService(newServiceUUID, characteristics, sizeof(characteristics) / sizeof(GattCharacteristic *));

如何为这 3 个特征添加描述?

编辑

我现在有:

static uint8_t percentageValue[10] = {0};
GattAttribute descr( BLE_UUID_DESCRIPTOR_CHAR_USER_DESC, (uint8_t *)"Percentage", strlen("Percentage"));
WriteOnlyArrayGattCharacteristic<uint8_t, 
        sizeof(percentageValue)> percentageChar( PercentageUUID, 
                                                 percentageValue,
                                                 GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_EXTENDED_PROPERTIES,
                                                 &descr,
                                                 1 ); 

它在 "size of" 行抛出一个 Error: No instance of constructor "WriteOnlyArrayGattCharacteristic<T, NUM_ELEMENTS>::WriteOnlyArrayGattCharacteristic [with T=std::uint8_t, NUM_ELEMENTS=10U]" matches the argument list in "main.cpp"

查看 Characteristic class API: https://developer.mbed.org/teams/Bluetooth-Low-Energy/code/BLE_API/file/d494ad3e87bd/ble/GattCharacteristic.h:

template <typename T>
class WriteOnlyGattCharacteristic : public GattCharacteristic {
public:
    WriteOnlyGattCharacteristic<T>(const UUID     &uuid,
                                   T              *valuePtr,
                                   uint8_t        additionalProperties = BLE_GATT_CHAR_PROPERTIES_NONE,
                                   GattAttribute *descriptors[]        = NULL,
                                   unsigned       numDescriptors       = 0) :
        GattCharacteristic(uuid, reinterpret_cast<uint8_t *>(valuePtr), sizeof(T), sizeof(T),
                           BLE_GATT_CHAR_PROPERTIES_WRITE | additionalProperties, descriptors, numDescriptors) {
        /* empty */
    }
};

附加到特性的描述符必须作为您正在创建的 *ArrayGattCharacteristic 对象的第四个参数(GattAttribute *descriptors[],默认情况下它是 NULL,表示特性没有描述符)传递。它们是 GattAttribute,在您的特征之前创建并在创建时传递给它。

也许这可以添加一个描述符(未测试),应该使用数组来添加更多(就像您对特征所做的那样):

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

希望这对您有所帮助(再次 ;-))