【问题标题】:Adding Characteristic User Description to multiple custom C++ BLE GATT Service向多个自定义 C++ BLE GATT 服务添加特征用户描述
【发布时间】:2016-01-25 09:37:47
【问题描述】:

我正在尝试使用mbed API 将一些特征用户描述添加到我的自定义 BLE GATT 服务中。到目前为止,我的工作都是基于this 代码结构。但是,我想为这些特征添加名称。我找不到太多关于如何执行此操作的信息。但是,下面是将信息添加到特征的代码。

GattCharacteristic() 的构造函数将 GattAttribtues 数组作为可选参数。您可以将 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*) );

这会构建,但不会为特征命名。

【问题讨论】:

  • does not give the characteristic a name是什么意思,连接设备时字符有描述符吗?如果有,它的内容是什么?
  • @jpo38 服务已创建,但特征只有0xA001描述,没有其他内容。
  • 你有没有试过在这两种情况下调试WriteOnlyArrayGattCharacteristic函数的执行?
  • 这很棘手,因为它不包含在函数中,所以我无法执行 printf。你要我检查什么?
  • 使用调试器并检查两种解决方案在运行时有什么不同......

标签: c++ bluetooth bluetooth-lowenergy mbed


【解决方案1】:

这是一个模板帮助类的命题,可以封装特征对象及其描述符。不熟悉模板的话有点难理解。

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

【讨论】:

  • 好的,现在就解决这个问题,我在GattCharacteristic *characteristics[] = {percentageChar-&gt;charac}; 线上收到了一个Error: Expression must have pointer typedescriptor ( BLE_UUID_DESCRIPTOR_CHAR_USER_DESC, (uint8_t *)name.c_str(), sizeof(name) ) ) 的结构似乎也有问题,因为它会引发错误 Error: Expected a "{"
  • 我做了一些其他的小修改来修复其他一些构建错误,当我们构建它时我会发布。
  • 你更新你的答案了吗?当前的代码只是抛出相同的错误?
  • 我删除了之前不起作用的答案,这是一个全新的答案,其模板类同时封装了特征对象和描述符对象。
  • 是的,但是这个会抛出上述错误。我现在只有这个错误:Error: Expression must have pointer type in "main.cpp" on the GattCharacteristic *characteristics[] = {percentageChar->charac};` 行。我也有两个警告,模板声明上都有Warning: Declaration of "T" hides template parameter in "main.cpp"Warning: Declaration of "NUM_ELEMENTS" hides template parameter in "main.cpp"
猜你喜欢
  • 1970-01-01
  • 2016-07-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-07-05
  • 2021-03-14
  • 1970-01-01
相关资源
最近更新 更多