添加特性用户说明到多个自定义C ++ BLE GATT服务 [英] Adding Characteristic User Description to multiple custom C++ BLE GATT Service
问题描述
我想一些特性用户说明添加到我的自定义BLE GATT服务,使用的 mbed API 。我的工作迄今已根据这 code结构。不过,我想名称添加到这些特性。没有太多的信息,我能找到如何做到这一点。然而,下面是code它增加了信息的特性。
有关GattCharacteristic的构造()接受GattAttribtues数组作为一个可选的参数。可以填充你的用户描述成一个GattAttribute,并通过它传递给特征。我有这样的结构特征之一的工作,但我在努力复制它的3个字符。我不能复制整个事情的3倍,因为我运行它来大量有关阵列等被已经定义的问题。如果I堆栈描述了阵列中,它不会由GattArray接受?
uint16_t newServiceUUID = 0xA000;
uint16_t PercentageUUID = 0xA001异或;
uint16_t TimeUUID = 0xA002;
uint16_t UseProfileUUID = 0xA003;常量静态字符DEVICE_NAME [] =设备; // 设备名称
静态常量uint16_t uuid16_list [] = {} 0xFFF的;
静态uint8_t有percentageValue [10] = {0};
GattAttribute nameDescr(BLE_UUID_DESCRIPTOR_CHAR_USER_DESC,(uint8_t有*)比例的strlen(百分比));
GattAttribute *描述符[] = {&放大器; nameDescr};WriteOnlyArrayGattCharacteristic< uint8_t有,的sizeof(percentageValue)GT;
percentageChar(PercentageUUID,
percentageValue,
GattCharacteristic :: BLE_GATT_CHAR_PROPERTIES_EXTENDED_PROPERTIES,
描述符,
的sizeof(描述)/ sizeof的(GattAttribute *));GattCharacteristic *特征[] = {&放大器; percentageChar,&安培; timeChar,&安培; UseProfileChar};
GattService newService(newServiceUUID,特性的sizeof(特性)/的sizeof(GattCharacteristic *));
修改
与下面的讨论工作,我现在有:
的#include<串GT;
类MyGattArray
{上市:
MyGattArray(常量为std :: string&放大器;名称):
ATTR(BLE_UUID_DESCRIPTOR_CHAR_USER_DESC,(uint8_t有*)name.c_str(),(name.size()+ 1))
{
描述[0] =&放大器; attr指示;
} GattAttribute ATTR;
GattAttribute *描述符[1];
};
和
静态uint8_t有percentageValue [10] = {0};
MyGattArray PercentageName(百分比);
GattAttribute *描述符[] = {及(PercentageName.attr)};WriteOnlyArrayGattCharacteristic< uint8_t有,的sizeof(percentageValue)GT;
percentageChar(PercentageUUID,
percentageValue,
GattCharacteristic :: BLE_GATT_CHAR_PROPERTIES_EXTENDED_PROPERTIES,
描述符,
的sizeof(描述)/ sizeof的(GattAttribute *));
这个版本,但不给特性的名字。
下面是模板辅助类的命题,即可以封装特性对象及其描述符。这是一个有点难以理解,如果你不熟悉的模板。
模板< typename的T,无符号NUM_ELEMENTS,模板< typename的T,无符号NUM_ELEMENTS>类CharacType>
类CharacteristicWithNameDescrptorHelper
{
上市:
CharacteristicWithNameDescrptorHelper(const的UUID和放大器; UUID,
ŧValuePtr中[NUM_ELEMENTS]
uint8_t有additionalProperties,
常量标准::字符串&安培; characName):
名字(characName)
{
描述符=新GattAttribute(BLE_UUID_DESCRIPTOR_CHAR_USER_DESC,(uint8_t有*)name.c_str(),name.size())
//创建描述符数组
描述符[0] =描述符;
//创建对象的特点:
CHARAC =新CharacType< T,NUM_ELEMENTS>(UUID,ValuePtr中,additionalProperties,描述符,1);
} 〜CharacteristicWithNameDescrptorHelper()
{
删除CHARAC;
删除描述;
} CharacType< T,NUM_ELEMENTS> * CHARAC;
性病::字符串名称;
GattAttribute *描述;
GattAttribute *描述符[1];
};
然后,你只需做的:
<$p$p><$c$c>CharacteristicWithNameDescrptorHelper<uint8_t,sizeof(percentageValue),WriteOnlyArrayGattCharacteristic>percentageChar(PercentageUUID,
percentageValue,
GattCharacteristic :: BLE_GATT_CHAR_PROPERTIES_EXTENDED_PROPERTIES,
百分比);GattCharacteristic *特征[] = {} percentageChar.charac;
GattService newService(newServiceUUID,特性的sizeof(特性)/的sizeof(GattCharacteristic *));
I am trying to add some Characteristic User Descriptions to my custom BLE GATT Service, using the mbed API. My work has so far been based on this code structure. However, I would like to add names to these characteristics. There isn't much info I could find on how to do this. However, below is the code which adds the information to a characteristic.
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. I have this structure working for one Characteristic, but am struggling to replicate it for 3 Characters. I can't replicate the whole thing 3 times, as I run it to lots of issues about arrays etc. being already defined. If I stack the descriptions up in the array, it won't be accepted by the 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
Working with the discussion below, I now have:
#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*) );
This builds, but does not give the characteristic a name.
Here is a proposition of template helper class that can encapsulate the characteristic object and its descriptor. It's a bit difficult to understand if you are not familiar with templates.
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];
};
Then, you simply do:
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 ++ BLE GATT服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!