ESP32 BLE scan, example works but devices found is always 0

Hi there! I'm using the example "BLE_scan" for ESP32 BLE Arduino on my Sparkfun Thing Plus. The code works and on the serial monitor I see at least two distinct BLE-devices (see below snippet of serial monitor). However, it seems that the function "foundDevices.getCount()" which is executed after each scan always returns 0 devices? Am I missing something? Is this a bug?
Best regards, Baelor

#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);
}

00:42:31.889 -> Advertised Device: Name: Ruuvi BB43, Address: c5:cf:10:5c:a8:7b, manufacturer data: 99040fffffffffffffffffffffffffffffffffffff, serviceUUID: 6e400001-b5a3-f393-e0a9-e50e24dcca9e
00:42:31.889 -> Advertised Device: Name: , Address: 5c:a3:d3:28:ed:7e, serviceUUID: 0000fd6f-0000-1000-8000-00805f9b34fb
00:42:33.049 -> Devices found: 0
00:42:33.049 -> Scan done!

There is a known bug in the code reported on GitHub here
https://github.com/espressif/arduino-esp32/issues/4627

There was recently a commit of a fix
https://github.com/espressif/arduino-esp32/pull/5241/commits/2841ef957f6f6520d128b6d50ce62bb586e9c0a6

I do not think that this is available yet in either of the core versions available from the board manager.
Latest Stable Release v1.06
Latest Development Release v2.0.0-alpha1

Rather than load the development repository it is likely easier to just modify/edit the library in the Arduino 15 file and save the change
Arduino15\packages\esp32\hardware\esp32\2.0.0-alpha1\libraries\BLE\src\BLEScan.cpp

if (m_pAdvertisedDeviceCallbacks) { // if has callback, no need to record to vector
						m_pAdvertisedDeviceCallbacks->onResult(*advertisedDevice);
					} //else commented for count 0 issue
					if (!m_wantDuplicates && !found) {   // if no callback and not want duplicate, and not already in vector, record it
						m_scanResults.m_vectorAdvertisedDevices.insert(std::pair<std::string, BLEAdvertisedDevice*>(advertisedAddress.toString(), advertisedDevice));
						shouldDelete = false;
					}
2 Likes

Many thanks for the rapid reply, I'm relieved! I'll try to fix it directly :slight_smile:

Edit: It did the trick, thanks @cattledog !

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.