gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 06:00:45 25 4
gpt4 key购买 nike

我正在尝试使用 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"在“大小”行。

最佳答案

查看 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[] 的第四个参数( *ArrayGattCharacteristic ,默认为 NULL,表示特性没有描述符)传递您正在创建的对象。他们是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*) );

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

关于c++ - 将特征用户描述添加到自定义 C++ BLE GATT 服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33328272/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com