0

我正在开发一个 c++ 应用程序,用一些信息填充蓝牙阵列。它基于 mbed 平台 BLE_API,但我认为这不相关。我有以下代码,我试图将其重新考虑到一个函数中。

GattAttribute nameDescr1(BLE_UUID_DESCRIPTOR_CHAR_USER_DESC, (uint8_t *)"Percentage", strlen("Percentage"));
GattAttribute *pdescriptors[] = { &nameDescr1 };


  PercentageFill(PercentageUUID, valueBytes.getPointer(),
                   valueBytes.getNumValueBytes(), HeartRateValueBytes::MAX_VALUE_BYTES,
                   GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_EXTENDED_PROPERTIES,
                   pdescriptors,
                   sizeof(pdescriptors) / sizeof(GattAttribute*)),

到目前为止,我得到了这个:

   GattAttribute produceName (char title[]) { 
        GattAttribute nameDescr(BLE_UUID_DESCRIPTOR_CHAR_USER_DESC, (uint8_t *)title, strlen(title));
        GattAttribute *descriptors[] = { &nameDescr };
        return descriptors;
    }

但是,可以预见的是,我会抛出一个错误:

错误:不存在合适的构造函数来将“GattAttribute *[1]”转换为“GattAttribute”

我可以看到它为什么抛出这个,但我不确定我应该如何返回整个数组,因为这是“PercentageFill”构造函数所需的格式。

谢谢。

更新:

为了提供完整的上下文,这是我正在设置的其他 Characteristcis(每个都有不同的名称):

NewService(BLE &_ble, uint8_t percentageFill, uint8_t replacementDue) :
    ble(_ble),
    valueBytes(percentageFill),
    PercentageFill(PercentageUUID, valueBytes.getPointer(),
                   valueBytes.getNumValueBytes(), HeartRateValueBytes::MAX_VALUE_BYTES,
                   GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_EXTENDED_PROPERTIES,
                   pdescriptors,
                   sizeof(pdescriptors) / sizeof(GattAttribute*)),
    Time(   TimeUUID,
            &replacementDue,
            GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_EXTENDED_PROPERTIES,
            tdescriptors,
            sizeof(tdescriptors) / sizeof(GattAttribute*)),
    UseProfile( UseProfileUUID, 
                &controlPointValue,
                GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_EXTENDED_PROPERTIES,
                Udescriptors,
                sizeof(Udescriptors) / sizeof(GattAttribute*)),) {
                setupService();
}
4

2 回答 2

2

该函数被produceName声明为返回一个GattAttribute对象,但您试图返回一个指向GattAttribute对象的指针数组。很不一样。

但这不是代码中最糟糕的部分。如果你修复了声明返回类型以便构建代码,你会遇到一个更糟糕的问题,这将导致未定义的行为:你返回指向局部变量的指针。一旦函数返回,那些局部变量将不复存在,任何指向它们的指针都不能使用。

于 2015-11-18T15:32:59.517 回答
1

首先:注意pdescriptors原始代码中的数组只有一个元素长。因此,直接指向对象的指针可以正常工作,或者如果百分比填充不期望指针数组,则可以。我们可以通过传递一个指向指针和大小为 1 来模拟这一点。请注意:sizeof(...)/sizeof(...)原始代码中的计算也意味着返回 1,当您引入函数边界时(特别是当您将数组传递为函数的参数)。

除此之外,您的问题有些不清楚:您是否打算实现不同的GattAttribute值?如果没有,您可能可以执行以下操作:

void updatePercentage(WhateverTypeValueBytesIs valueBytes) {
    GattAttribute nameDescr(BLE_UUID_DESCRIPTOR_CHAR_USER_DESC, (uint8_t *)"Percentage", strlen("Percentage"));
    GattAttribute * ptr = &nameDescr; // needed, because we want to pass pointer-to-pointer-to-nameDescr
    PercentageFill(PercentageUUID, valueBytes.getPointer(),
               valueBytes.getNumValueBytes(), HeartRateValueBytes::MAX_VALUE_BYTES,
               GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_EXTENDED_PROPERTIES,
               &ptr, 1), /* are we sure about trailing comma here? */
    // other relevant trailing code?
}

从您在评论中提到的有关不可访问的复制构造函数的错误消息来看,GattAttribute它可能是一个常规构造函数,因此无需在那里创建额外的函数。如果你想把这个特殊GattAttribute的东西变成你可以隐藏在函数接口后面并在需要时“查找”的东西,那么你可以像这样把它变成一个单例(例如,存在实现相同目标的其他方法):

GattAttribute * getNameDescriptor(void) {
    static GattAttribute nameDescr(BLE_UUID_DESCRIPTOR_CHAR_USER_DESC, (uint8_t *)"Percentage", strlen("Percentage"));
    return &nameDescr;
}

然后您可以像这样使用该功能:

void updatePercentage(WhateverTypeValueBytesIs valueBytes) {
    GattAttribute * ptr = getNameDescriptor(); // needed, because we want to pass pointer-to-pointer-to-nameDescr
    PercentageFill(PercentageUUID, valueBytes.getPointer(),
               valueBytes.getNumValueBytes(), HeartRateValueBytes::MAX_VALUE_BYTES,
               GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_EXTENDED_PROPERTIES,
               &ptr, 1), /* are we sure about trailing comma here? */
    // other relevant trailing code?
}

编辑以根据评论添加其他选项:

void updatePercentage(WhateverTypeValueBytesIs valueBytes, const char* name) {
    GattAttribute nameDescr(BLE_UUID_DESCRIPTOR_CHAR_USER_DESC, (const uint8_t *) name, strlen(name));
    GattAttribute * ptr = &nameDescr; // needed, because we want to pass pointer-to-pointer-to-nameDescr
    PercentageFill(PercentageUUID, valueBytes.getPointer(),
               valueBytes.getNumValueBytes(), HeartRateValueBytes::MAX_VALUE_BYTES,
               GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_EXTENDED_PROPERTIES,
               &ptr, 1), /* are we sure about trailing comma here? */
    // other relevant trailing code?
}

GattAttribute或另一种选择:微不足道,通过引用传递完全初始化:

void updatePercentage(WhateverTypeValueBytesIs valueBytes, GattAttribute & descr) {
    GattAttribute * ptr = &descr; // needed, because we want to pass pointer-to-pointer-to-descr
    PercentageFill(PercentageUUID, valueBytes.getPointer(),
               valueBytes.getNumValueBytes(), HeartRateValueBytes::MAX_VALUE_BYTES,
               GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_EXTENDED_PROPERTIES,
               &ptr, 1), /* are we sure about trailing comma here? */
    // other relevant trailing code?
}

你这样称呼它:

void foo(WhateverTypeValueBytesIs valueBytes) {
    GattAttribute nameDescr(BLE_UUID_DESCRIPTOR_CHAR_USER_DESC, (uint8_t *)"Percentage", strlen("Percentage"));
    updatePercentage(valueBytes, nameDescr);
}

显然,除了使用引用之外,您还可以修改函数以获取指向GattAttribute对象的指针(并使用它而不是ptr变量,就像ptr示例中使用变量的方式一样)。

valueBytes进一步补充:请注意,当您将其传递给 时,您可能希望避免复制updatePercentage,在这里您可能希望通过引用传递它。

于 2015-11-18T16:06:00.467 回答