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

时间:2015-10-25 09:43:49

标签: c++ bluetooth bluetooth-lowenergy mbed

我正在尝试使用mbed API向我的自定义BLE GATT服务添加一些特征用户描述。到目前为止,我的工作基于this代码结构。但是,我想为这些特征添加名称。关于如何做到这一点,我找不到太多信息。但是,以下是来自论坛的评论,说明如何做到这一点。

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

它会在“大小”行上抛出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"

1 个答案:

答案 0 :(得分:1)

查看Characteristic类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 */
    }
};

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

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

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

希望这有助于(再次; - ))