请帮助我,我尝试了 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!