找到 BLE 扫描仪设备 0

BLE Scanner Device Found 0

请帮助我,我尝试了 BLE 扫描,但是当我调用 MyAdvertisedDeviceCallbacks class 时,设备发现的结果是 0,但是当我没有调用 class 时,发现的设备显示结果是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!

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

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

作为后续,在查看最近对 ESP32 Arduino github 站点上的库文件 BLEScan.cpp 的更改时,它似乎是BLE 扫描操作的变化来自 2020 年晚些时候 (October/November) 的 2 次变化。

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

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

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

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

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

m_pAdvertisedDeviceCallbacks->onResult(*advertisedDevice);

之后
if (!m_wantDuplicates && !found) {

(然后你可以擦除空的if)

那么你将拥有

if (!m_wantDuplicates && !found) {
    m_pAdvertisedDeviceCallbacks->onResult(*advertisedDevice); 
    m_scanResults.m_vectorAdvertisedDevices.insert(std::pair<std::string, BLEAdvertisedDevice*>(advertisedAddress.toString(), advertisedDevice));
    shouldDelete = false;
}