Callback onResult Not Triggered Using NimBLE Library to Find BLE Beacons #788
-
Hi everyone, I'm trying to scan for BLE beacons using the NimBLE library. However, the onResult callback is not being triggered. Previously, I was using the original ESP32 library, and I was able to find my beacon without any issues. I switched to NimBLE because it allows me to use callbacks only by setting maxResults to zero, which is a feature I need for my project. However, with NimBLE, the callback doesn't seem to be called. Here's the code I'm using for testing: #include <ArduinoJson.h>
#include <NimBLEDevice.h>
#include <HardwareSerial.h>
#define COMPANY_UUID_PREFIX "06721162"
NimBLEScan *pBLEScan;
int scanTime = 5; // Tempo de escaneamento BLE em segundos
class MyAdvertisedDeviceCallbacks : public NimBLEScanCallbacks {
void onResult(NimBLEAdvertisedDevice* advertisedDevice) {
Serial.println("Scanning device...");
if (advertisedDevice->haveManufacturerData()) {
std::string strManufacturerData = advertisedDevice->getManufacturerData();
Serial.print("Manufacturer Data Length: ");
Serial.println(strManufacturerData.length());
Serial.print("First Two Bytes: ");
Serial.print(strManufacturerData[0], HEX);
Serial.println(strManufacturerData[1], HEX);
std::string UUID = advertisedDevice->getServiceUUID().toString();
Serial.print("Full UUID: ");
Serial.println(UUID.c_str());
std::string proximityUUID = UUID.substr(0, 8);
Serial.print("Proximity UUID: ");
Serial.println(proximityUUID.c_str());
}
}
};
void setup() {
Serial.begin(115200);
// Inicializa o BLE
NimBLEDevice::init("RJRegister");
pBLEScan = NimBLEDevice::getScan();
pBLEScan->setScanCallbacks(new MyAdvertisedDeviceCallbacks());
pBLEScan->setActiveScan(true);
pBLEScan->setMaxResults(0xFF);
pBLEScan->setInterval(160);
pBLEScan->setWindow(80);
}
void BLEScan() {
pBLEScan->start(scanTime, false, false);
pBLEScan->clearResults();
}
void loop() {
BLEScan();
delay(5000);
} Am I missing something in the configuration, or is there a known issue that could cause the onResult callback not to be called? For reference, my scan works fine with the original ESP32 BLE library. Any help would be greatly appreciated! Edit: I tried to use the Details: |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
If you are using 2.0.0 (master branch) then you need to change the callback function signature. I recommend to always add the |
Beta Was this translation helpful? Give feedback.
If you are using 2.0.0 (master branch) then you need to change the callback function signature.
void onResult(NimBLEAdvertisedDevice* advertisedDevice) {
should bevoid onResult(const NimBLEAdvertisedDevice* advertisedDevice) {
I recommend to always add the
override
keyword to the functions you are overriding to catch errors like this at compile time.