向多个自定义c++ BLE GATT服务添加特征用户描述
Adding Characteristic User Description to multiple custom C++ BLE GATT Service
我正在尝试使用mbed API为我的自定义BLE GATT服务添加一些特征用户描述。到目前为止,我的工作都是基于这种代码结构。然而,我想为这些特征添加一些名称。关于如何做到这一点,我找不到太多的信息。然而,下面是将信息添加到特征的代码。
GattCharacteristic()的构造函数接受一个gattattributes数组作为可选参数。您可以将您的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*) );
生成,但不给特征一个名称。
标题>这是一个模板助手类的命题,它可以封装特征对象及其描述符。如果你不熟悉模板的话,就有点难以理解了。
template <typename T, unsigned NUM_ELEMENTS, template <typename T, unsigned NUM_ELEMENTS> class CharacType>
class CharacteristicWithNameDescrptorHelper
{
public:
CharacteristicWithNameDescrptorHelper( const UUID &uuid,
T valuePtr[NUM_ELEMENTS],
uint8_t additionalProperties,
const std::string& characName ) :
name( characName )
{
descriptor = new GattAttribute( BLE_UUID_DESCRIPTOR_CHAR_USER_DESC, (uint8_t *)name.c_str(), name.size() )
// create descriptor array
descriptors[0] = descriptor;
// create characteristic object:
charac = new CharacType<T,NUM_ELEMENTS>( uuid, valuePtr, additionalProperties, descriptors, 1 );
}
~CharacteristicWithNameDescrptorHelper()
{
delete charac;
delete descriptor;
}
CharacType<T,NUM_ELEMENTS>* charac;
std::string name;
GattAttribute* descriptor;
GattAttribute *descriptors[1];
};
然后,你只需做:
CharacteristicWithNameDescrptorHelper<uint8_t,sizeof(percentageValue),WriteOnlyArrayGattCharacteristic>
percentageChar( PercentageUUID,
percentageValue,
GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_EXTENDED_PROPERTIES,
"Percentage" );
GattCharacteristic *characteristics[] = {percentageChar.charac};
GattService newService(newServiceUUID, characteristics, sizeof(characteristics) / sizeof(GattCharacteristic *));
相关文章:
- 将成员变量添加到共享库中的类中,不会破坏二进制兼容性吗
- 如何在C++中从两个单独的for循环中添加两个数组
- POCO::PostgreSQL:如何将std::vector支持添加到`Binder::bind`
- 如何仅为一个函数添加延迟
- 如何防止 c++ 在从浮点型转换为双精度型(不适用于 IO)时添加额外的小数?
- 使用std::transform将一个范围的元素添加到另一个范围中
- 如何将更多文件夹添加到c++include路径
- 如何将元素添加到数组的线程安全函数?
- QT通过C++添加映射QML项目
- 如何将点击的信号和插槽添加到qt中的自定义按钮中
- 如何使用重载的相等(==)运算符向测试用例添加描述
- 为什么Mat类的两个对象可以在不重载运算符+的情况下添加
- 如何防止clang格式在流运算符调用之间添加换行符<<
- 只能向C++添加一定数量的字符
- 将第二个 GATT 服务添加到 Movesense 容器
- 我应该在服务模板中的什么位置添加自己的代码?
- 将URL从服务添加到IE缓存
- 如何在服务exe中添加失败操作
- 向多个自定义c++ BLE GATT服务添加特征用户描述
- 向自定义c++ BLE GATT服务添加特征用户描述