-1

Please help me, I have try BLE Scan but when I call the MyAdvertisedDeviceCallbacks class the device found result is 0 but when I am not calling the class the device found show the result is 3. is there any wrong with my code? I am using ESP32 Dev Kit V1

Here is my code:

/*
   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);
}

And here is the Result:

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!
Hendri
  • 3
  • 3

3 Answers3

0

You didn't say which version of the ESP32 Arduino code you are using, but something is wrong with the BLE scanning code in ESP32 Arduino BLE version 1.0.5 and the newly released version 1.0.6. In the earlier version 1.0.4, the scan results only report each device once per scan and correctly report the number of unique devices found. In these later versions, the same devices are incorrectly reported multiple times during a scan and the total count is zero. This appears to be a bug in the latest versions of the BLE scan code, not in your example sketch.

Based on the results you got, it should have shown only two unique Advertised Devices and had a Devices Count: 2

Dan K
  • 46
  • 2
0

As a followup, in looking at the recent changes to the library file BLEScan.cpp on the ESP32 Arduino github site, it appears the source of the changes in BLE scanning operation were from 2 changes made late (October/November) in 2020.

  1. Showing found BLE devices multiple times: This was due to a change in the BLEScan constructor to change the scan_duplicate parameter to the scan parameters used. This does not take into account the fact that when calling BLEScan.setAdvertisedDeviceCallbacks(), the wantDuplicates parameter is apparently ignored.

  2. Devices count always 0: This is due to a change in operation of tracking the scan results. An obvious change to the operation is that when a BLE scan operation has user scanning callbacks in place, the tracking of scanned devices is disabled (which is why the count is always 0)! Even though this is obvious in the source code (a comment states this), it is not obvious to the library user as the examples using BLE scanning do not mention this.

In summary, what you are seeing appears to be what the code is written to do. Personally, I do not agree with these changes in operation and, in my use, have modified the BLEScan library to operate as it did prior to these 2 changes.

Dan K
  • 46
  • 2
0

Yes, i see it too. Current implementation suggests that you'll handle it all in your callback. To revert it to old good behaviour you should just open the (you arduino folder) /packages/esp32/hardware/esp32/1.0.6/libraries/BLE/src/BLEScan.cpp

and then (about line 124) move the line

m_pAdvertisedDeviceCallbacks->onResult(*advertisedDevice);

after the

if (!m_wantDuplicates && !found) {

(then you can erase empty if)

Then you'll have

if (!m_wantDuplicates && !found) {
    m_pAdvertisedDeviceCallbacks->onResult(*advertisedDevice); 
    m_scanResults.m_vectorAdvertisedDevices.insert(std::pair<std::string, BLEAdvertisedDevice*>(advertisedAddress.toString(), advertisedDevice));
    shouldDelete = false;
}
Antoine
  • 1,393
  • 4
  • 20
  • 26
Erinaceto
  • 21
  • 1