-1

请帮助我,我尝试了 BLE 扫描,但是当我调用 MyAdvertisedDeviceCallbacks 类时,找到的设备结果为 0,但是当我不调用类时,找到的设备显示结果为 3。我的代码有什么问题吗?我正在使用 ESP32 开发套件 V1

这是我的代码:

/*
   Based on Neil Kolban example for IDF: https://github.com/nkolban/esp32-snippets/blob/master/cpp_utils/tests/BLE%20Tests/SampleScan.cpp
   Ported to Arduino ESP32 by Evandro Copercini
*/

#include <BLEDevice.h>
#include <BLEUtils.h>
#include <BLEScan.h>
#include <BLEAdvertisedDevice.h>

int scanTime = 5; //In seconds
BLEScan* pBLEScan;

class MyAdvertisedDeviceCallbacks: public BLEAdvertisedDeviceCallbacks {
    void onResult(BLEAdvertisedDevice advertisedDevice) {
      Serial.printf("Advertised Device: %s \n", advertisedDevice.toString().c_str());
    }
};

void setup() {
  Serial.begin(115200);
  Serial.println("Scanning...");

  BLEDevice::init("");
  pBLEScan = BLEDevice::getScan(); //create new scan
  pBLEScan->setAdvertisedDeviceCallbacks(new MyAdvertisedDeviceCallbacks());
  pBLEScan->setActiveScan(true); //active scan uses more power, but get results faster
  pBLEScan->setInterval(100);
  pBLEScan->setWindow(99);  // less or equal setInterval value
}

void loop() {
  // put your main code here, to run repeatedly:
  BLEScanResults foundDevices = pBLEScan->start(scanTime, false);
  Serial.print("Devices found: ");
  Serial.println(foundDevices.getCount());
  Serial.println("Scan done!");
  pBLEScan->clearResults();   // delete results fromBLEScan buffer to release memory
  delay(2000);
}

这是结果:

Advertised Device: Name: honor Band 3-f15, Address: 34:12:f9:03:1f:15, appearance: 0, manufacturer data: 7d02010300ffcc, serviceUUID: 00001812-0000-1000-8000-00805f9b34fb, txPower: 4 
Advertised Device: Name: honor Band 3-f15, Address: 34:12:f9:03:1f:15, appearance: 0, manufacturer data: 7d02010300ffcc, serviceUUID: 00001812-0000-1000-8000-00805f9b34fb, txPower: 4 
Advertised Device: Name: Charge HR, Address: f5:02:71:f9:46:a7, serviceUUID: adabfb00-6e7d-4601-bda2-bffaa68956ba, txPower: -6 
Advertised Device: Name: honor Band 3-f15, Address: 34:12:f9:03:1f:15, appearance: 0, manufacturer data: 7d02010300ffcc, serviceUUID: 00001812-0000-1000-8000-00805f9b34fb, txPower: 4 
Advertised Device: Name: honor Band 3-f15, Address: 34:12:f9:03:1f:15, appearance: 0, manufacturer data: 7d02010300ffcc, serviceUUID: 00001812-0000-1000-8000-00805f9b34fb, txPower: 4 
Devices found: 0
Scan done!
4

3 回答 3

0

作为后续,在查看 ESP32 Arduino github 站点上库文件BLEScan.cpp的最新更改时,BLE 扫描操作的更改来源似乎来自 2020 年晚些时候(10 月/11 月)进行的 2 次更改。

  1. 多次显示找到的 BLE 设备:这是由于 BLEScan 构造函数中的更改将 scan_duplicate 参数更改为使用的扫描参数。这没有考虑到调用 时BLEScan.setAdvertisedDeviceCallbacks()wantDuplicates参数显然被忽略了。

  2. 设备计数始终为 0:这是由于跟踪扫描结果的操作发生了变化。操作的一个明显变化是,当 BLE 扫描操作有用户扫描回调时,扫描设备的跟踪被禁用(这就是计数始终为 0 的原因)!尽管这在源代码中很明显(注释说明了这一点),但对于图书馆用户来说并不明显,因为使用 BLE 扫描的示例没有提到这一点。

总之,您所看到的似乎是编写代码的目的。就我个人而言,我不同意这些操作上的更改,并且在我的使用中,已修改 BLEScan 库以像这两个更改之前一样操作。

于 2021-03-30T13:48:42.060 回答
0

是的,我也看到了。当前的实现建议您将在回调中处理这一切。要将其恢复为旧的良好行为,您只需打开(您的 arduino 文件夹)/packages/esp32/hardware/esp32/1.0.6/libraries/BLE/src/BLEScan.cpp

然后(关于第 124 行)移动该行

m_pAdvertisedDeviceCallbacks->onResult(*advertisedDevice);

之后

if (!m_wantDuplicates && !found) {

(然后你可以擦除 empty if

然后你会有

if (!m_wantDuplicates && !found) {
    m_pAdvertisedDeviceCallbacks->onResult(*advertisedDevice); 
    m_scanResults.m_vectorAdvertisedDevices.insert(std::pair<std::string, BLEAdvertisedDevice*>(advertisedAddress.toString(), advertisedDevice));
    shouldDelete = false;
}
于 2021-11-15T02:48:01.247 回答
0

您没有说您使用的是哪个版本的 ESP32 Arduino 代码,但是 ESP32 Arduino BLE 版本 1.0.5 和新发布的版本 1.0.6 中的 BLE 扫描代码有问题。在早期版本 1.0.4 中,扫描结果每次扫描只报告每个设备一次,并正确报告找到的唯一设备的数量。在这些更高版本中,相同的设备在扫描期间被多次错误报告,并且总数为零。这似乎是最新版本的 BLE 扫描代码中的错误,而不是您的示例草图中的错误。

根据您获得的结果,它应该只显示两个唯一的广告设备并且设备数:2

于 2021-03-29T23:49:10.027 回答