【问题标题】:ESP32_BLE_Arduino | BLEAdvertisedDevice.getServiceData() does not return what i expectESP32_BLE_Arduino | BLEAdvertisedDevice.getServiceData() 没有返回我所期望的
【发布时间】:2020-11-30 18:19:16
【问题描述】:

我一直在尝试将 esp 设置为 BLE 放大器。我正在尝试获取传感器信标的广播/广告数据(在服务下发送)并在短时间内广告相同的数据。整个过程应该在没有连接到客户端的情况下工作。

问题是我无法正确读取传感器信标(客户端)的数据。我正在 Rpi3 上运行扫描仪,可以检查,这里一切都按预期工作。高贵的输出

noble.on('discover', async function(bleResult) {

if(bleResult.advertisement.localName 
  && bleResult.advertisement.localName.localeCompare("RDL52832") == 0 {
       console.log(bleResult.advertisement.serviceData);
}
...

[ { uuid: '0318',
    data: <Buffer 13 1b 2e 2f 01 00 00 02 00 00 00 01 00 00 09 07> } ]

在 ESP32 上获取相同数据的代码如下所示

BLEScanResults foundDevices = pBLEScan->start(scanTime, true);

  for (uint32_t i = 0; i < foundDevices.getCount(); i++) {
    if (foundDevices.getDevice(i).getName() == ("RDL52832")) {
       Serial.printf("\nAddress: %s \n", foundDevices.getDevice(i).getAddress().toString().c_str());
       Serial.printf("ServiceData as Hex: %X \n", foundDevices.getDevice(i).getServiceData().c_str());
       startServerWithCharacteristics(foundDevices.getDevice(i).getServiceData(), "ant_node");
    };
  }

这段代码的输出是

Address: e9:81:f5:86:63:a5 
ServiceData as Hex: 3FFE7514 
Try Broadcasting ant_node 

如何从 ESP32 的信标中获取广告数据? 我的错在哪里?

提前致谢!

【问题讨论】:

    标签: c++ bluetooth-lowenergy esp32 arduino-ide


    【解决方案1】:

    我在 ESP32 上使用 Xiaomi Ble Gateway 时遇到了类似的问题。 我的假设是问题出在 BLEAdvertisedDevice 类中。 我发现 getServiceData() 返回字符串,这可能有问题。

    /**
    * @brief Get the service data.
    * @return The ServiceData of the advertised device.
    */
    std::string BLEAdvertisedDevice::getServiceData() {
    return m_serviceData;
    } //getServiceData
    

    如果有效负载中有 0x0,则字符串将被终止并且您不会获得完整的有效负载。

    这是我的情况,看起来也和你的差不多。

    我的解决方案是:

    uint8_t cServiceData[100];
    uint8_t* payloadPtr = advertisedDevice.getPayload();
    
    for (int i = 0; i < advertisedDevice.getPayloadLength(); i++)
    {
        cServiceData[i] = *(payloadPtr + i);
    }
    

    我希望它也对你有用。

    【讨论】:

      猜你喜欢
      • 2021-05-30
      • 1970-01-01
      • 2021-12-11
      • 2018-05-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-24
      • 1970-01-01
      相关资源
      最近更新 更多