请帮帮我,我尝试过 BLE 扫描,但是当我调用 MyAdvertisedDeviceCallbacks 类时,设备找到的结果为 0,但是当我不调用该类时,找到的设备显示结果为 3。我的代码有问题吗?我使用的是 ESP32 Dev Kit 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!
答案 0 :(得分:0)
您没有说明您使用的是哪个版本的 ESP32 Arduino 代码,但是 ESP32 Arduino BLE 1.0.5 版和新发布的 1.0.6 版中的 BLE 扫描码有问题。在较早的 1.0.4 版本中,扫描结果每次扫描仅报告每个设备一次,并正确报告找到的唯一设备的数量。在这些更高版本中,相同的设备在扫描过程中被多次错误报告,并且总数为零。这似乎是最新版本的 BLE 扫描代码中的错误,而不是您的示例草图。
根据您得到的结果,它应该只显示两个唯一的广告设备,并且设备数量:2
答案 1 :(得分:0)
作为后续,在查看最近对 ESP32 Arduino github 站点上的库文件 BLEScan.cpp 的更改时,似乎 BLE 扫描操作更改的来源来自 2 个更改2020 年晚些时候(10 月/11 月)。
多次显示找到的 BLE 设备:
这是由于 BLEScan 构造函数发生了变化,将 scan_duplicate 参数更改为使用的扫描参数。这并没有考虑到调用 BLEScan.setAdvertisedDeviceCallbacks()
时,wantDuplicates
参数显然被忽略的事实。
设备计数始终为 0: 这是由于跟踪扫描结果的操作发生了变化。该操作的一个明显变化是,当 BLE 扫描操作具有用户扫描回调时,扫描设备的跟踪被禁用(这就是计数始终为 0 的原因)!尽管这在源代码中很明显(注释说明了这一点),但对于库用户来说并不明显,因为使用 BLE 扫描的示例没有提到这一点。
总而言之,您所看到的似乎就是编写代码的目的。就我个人而言,我不同意这些操作上的更改,并且在我的使用中,我修改了 BLEScan 库,使其像在这 2 次更改之前一样操作。