Select once of all devices founded!!! ESP32

Hello, I´m testing my new ESP32 dev board. I´m trying of to modify BLE Scan example... This sketch return a list of devices avaibles... I would like launch a action (pinout) if ESP32 detect a concret Device. I have tryed some ways but all times return errors... Someone could help me for fixing it?

This is the 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 = 30; //In seconds

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("");
  BLEScan* pBLEScan = BLEDevice::getScan(); //create new scan
  pBLEScan->setAdvertisedDeviceCallbacks(new MyAdvertisedDeviceCallbacks());
  pBLEScan->setActiveScan(true); //active scan uses more power, but get results faster
  BLEScanResults foundDevices = pBLEScan->start(scanTime);
  Serial.print("Devices found: ");
char X = (foundDevices.getCount());
 Serial.println(X);
//Serial.println(foundDevices.getCount());
  if (X == "Parking"){
    Serial.print("we found Parking"");
    }else{
      Serial.print("Don´t match");
      }
  // put your main code here, to run repeatedly:
  
}

void loop() {


  delay(2000);
}

Thanks!!

  char X = (foundDevices.getCount());
  if (X == "Parking"){

X is a char. It can hold a single character so it can never equal "Parking"

Suppose that foundDevices.getCount() returned 12, because it found 12 devices.

if (X == "Parking"){

Now, what are the odds that 12 is going to equal "Parking"? Not a snowball's chance in hell.

You need to look at the definition of BLEScanResults to see how the results are stored. For example, BLEScanResults .getCount() returns an int, not a char, and BLEScanResults .getDevice(uint32_t i) returns a device from the list. You then have to look at the definition of BLEAdvertisedDevice to see what attribute you want to search for. For this example, I chose 'name'.

  Serial.print("Devices found: ");
  int deviceCount = foundDevices.getCount();
  Serial.println(deviceCount);
  for (uint32_t i = 0; i < deviceCount; i++)
  {
    BLEAdvertisedDevice device = foundDevices.getDevice(i);


    if (strcmp(device.getName(), "Parking") == 0)
    {
      Serial.print("We found a device named \"Parking\"");
    }
  }
  int deviceCount = foundDevices.getCount();
  Serial.println(deviceCount);
  for (uint32_t i = 0; i < deviceCount; i++)
  {
    BLEAdvertisedDevice device = foundDevices.getDevice(i);

So, there can be a negative number of devices, or 0 or some positive value up to 32767. At least, that's the range of values that fit in an int.

Then, you iterate through devices from 0 up to 4294967295. That seems useless.

Why are you using int for the number of devices and then unsigned long for the index?

PaulS:
Why are you using int for the number of devices and then unsigned long for the index?

Because the library author chose to have .getCount() return 'int' and have .getDevice() take 'uint32_t'. See: esp32-snippets/cpp_utils/BLEScan.h at master · nkolban/esp32-snippets · GitHub

If you want that changed I suggest you take it up with the library author.

class BLEScanResults {
public:
	void                dump();
	int                 getCount();
	BLEAdvertisedDevice getDevice(uint32_t i);

private:
	friend BLEScan;
	std::vector<BLEAdvertisedDevice> m_vectorAdvertisedDevices;
};

johnwasser:
You need to look at the definition of BLEScanResults to see how the results are stored. For example, BLEScanResults .getCount() returns an int, not a char, and BLEScanResults .getDevice(uint32_t i) returns a device from the list. You then have to look at the definition of BLEAdvertisedDevice to see what attribute you want to search for. For this example, I chose 'name'.

  Serial.print("Devices found: ");

int deviceCount = foundDevices.getCount();
  Serial.println(deviceCount);
  for (uint32_t i = 0; i < deviceCount; i++)
  {
    BLEAdvertisedDevice device = foundDevices.getDevice(i);

if (strcmp(device.getName(), "Parking") == 0)
    {
      Serial.print("We found a device named "Parking"");
    }
  }




Soo, how could I print the device.getName?

Have you tried Serial.print(device.getName()); ?

I'm unable to print the 'toString()' output, I know it will be something stupid, but would appreciate if someone could point it out for me:

Code is based on the BLE proximity example here: BLE_Scan.ino · GitHub

Code:

void loop()
{
BLEScan *scan = BLEDevice::getScan();
scan->setActiveScan(true);
BLEScanResults results = scan->start(1);
int best = CUTOFF;
for (int i = 0; i < results.getCount(); i++)
{
BLEAdvertisedDevice device = results.getDevice(i);

int rssi = device.getRSSI();
Serial.print("Device Number: ");
Serial.print(i);
Serial.print(" RSSI: ");
Serial.print(rssi);
Serial.print(" ");
Serial.println(device.toString());

if (rssi > best)
{
best = rssi;
}
}
digitalWrite(PIN, best > CUTOFF ? HIGH : LOW);
}

The line ' Serial.println(device.toString()); ' fails to compile.

'no matching function for call to 'HardwareSerial::println(std::__cxx11::string)'

As I said in reply #7: "Have you tried Serial.print(device.getName()); ?"

It looks like ".toString()" returns a type 'std::__cxx11::string' and not a type 'String'. Since device.getName() returns a 'char *' you should be able to print that. There are probably other methods of 'BLEAdvertisedDevice' to get other attributes of the device if you want to print more.