ESP32 as a proximity sensor coding help

I'm using a ESP32 to as a proximity sensor to safeguard several android devices that are running an ibeacon simulator that's always on that the ESP32 is capable of detecting - which I've tested to make sure it works (see attached image).

Although, the mac addresses that the ibeacon simulator outputs constantly changes and therefore cannot be used, however the manufactures id/data from the ibeacon remains constant. As such I've been using the code below to instead use the scanned manufactures id/data from the ibeacon simulartor and the rssi value to execute a conditional function (which for now is just a blinking LED), but I cannot get it to work. What am I doing wrong?

I'm thinking the code isn't written properly to obtain the manufactures id/data correctly. I know the code works because, even though the mac address is constantly changing, I can edit the code to read the mac address of the ibeacon and everything works fine and can see other bluetooth beacons in the area and well as the ibeacon simulator which can be seen in the aforementioned a image I've attached.

It's only when I change the code to read the manufactures id/data instead that the ESP32 doesn't link to the ibeacon. I'm adding both versions of the code (the version that functions using the mac address and the version that's suppose to function using the manufactures id/data) for comparison and understanding. Thank you in advance.

MAC Address Version:`

#include "BLEDevice.h"
#define LED 2
int Contador = 0;
static BLEAddress *pServerAddress;
BLEScan* pBLEScan;
BLEClient*  pClient;
bool deviceFound = false;
bool Encendida = false;
bool BotonOff = false;
String knownAddresses[] = {"6f:b6:bc:f2:dc:e5"};
unsigned long entry;
static void notifyCallback(
  BLERemoteCharacteristic* pBLERemoteCharacteristic,
  uint8_t* pData,
  size_t length,
  bool isNotify) {
  Serial.print("Notify callback for characteristic ");
  Serial.print(pBLERemoteCharacteristic->getUUID().toString().c_str());
  Serial.print(" of data length ");
  Serial.println(length);
}
class MyAdvertisedDeviceCallbacks: public BLEAdvertisedDeviceCallbacks {
    void onResult(BLEAdvertisedDevice Device){
      //Serial.print("BLE Advertised Device found: ");
      //Serial.println(Device.toString().c_str());
      pServerAddress = new BLEAddress(Device.getAddress()); 
      bool known = false;
      bool Master = false;
      for (int i = 0; i < (sizeof(knownAddresses) / sizeof(knownAddresses[0])); i++) {
        if (strcmp(pServerAddress->toString().c_str(), knownAddresses[i].c_str()) == 0) 
          known = true;
      }
      if (known) {
        Serial.print("Device found: ");
        Serial.println(Device.getRSSI());
        if (Device.getRSSI() > -85) {
          deviceFound = true;
        }
        else {
          deviceFound = false;
        }
        Device.getScan()->stop();
        delay(100);
      }
    }
};
void setup() {
  Serial.begin(115200);
  pinMode(LED,OUTPUT);
  digitalWrite(LED,LOW);
  BLEDevice::init("");
  pClient  = BLEDevice::createClient();
  pBLEScan = BLEDevice::getScan();
  pBLEScan->setAdvertisedDeviceCallbacks(new MyAdvertisedDeviceCallbacks());
  pBLEScan->setActiveScan(true);
  Serial.println("Done");
}
void Bluetooth() {
  Serial.println();
  Serial.println("BLE Scan restarted.....");
  deviceFound = false;
  BLEScanResults scanResults = pBLEScan->start(5);
  if (deviceFound) {
    Serial.println("Connection Established");
    Encendida = true;
    digitalWrite(LED,HIGH);
    Contador = 0;
    delay(1000);
  }
  else{
    digitalWrite(LED,LOW);
    delay(1000);
  }
}
void loop() { 
  Bluetooth();
}

Manufactures ID/Data Version (not working):

#include "BLEDevice.h"
#define LED 2
int Contador = 0;
static BLEAddress *pServerAddress;
BLEScan* pBLEScan;
BLEClient*  pClient;
bool deviceFound = false;
bool Encendida = false;
bool BotonOff = false;
String knownAddresses[] = {"4c000215b9a6adffaea94703903a58437a4d9d2400000000fd"};
unsigned long entry;
static void notifyCallback(
  BLERemoteCharacteristic* pBLERemoteCharacteristic,
  uint8_t* pData,
  size_t length,
  bool isNotify) {
  Serial.print("Notify callback for characteristic ");
  Serial.print(pBLERemoteCharacteristic->getUUID().toString().c_str());
  Serial.print(" of data length ");
  Serial.println(length);
}
class MyAdvertisedDeviceCallbacks: public BLEAdvertisedDeviceCallbacks {
    void onResult(BLEAdvertisedDevice Device) {
      //Serial.print("BLE Advertised Device found: ");
      //Serial.println(Device.toString().c_str());
      pServerAddress = new BLEAddress(Device.getAddress());
      bool known = false;
      bool Master = false;
      for (int i = 0; i < (sizeof(knownAddresses) / sizeof(knownAddresses[0])); i++) {
        if (strcmp(Device.getManufacturerData().c_str(), knownAddresses[i].c_str()) == 0)
          known = true;
      }
      if (known) {
        Serial.print("Device found: ");
        Serial.println(Device.getRSSI());
        if (Device.getRSSI() > -85) {
          deviceFound = true;
        }
        else {
          deviceFound = false;
        }
        Device.getScan()->stop();
        delay(100);
      }
    }
};
void setup() {
  Serial.begin(115200);
  pinMode(LED, OUTPUT);
  digitalWrite(LED, LOW);
  BLEDevice::init("");
  pClient  = BLEDevice::createClient();
  pBLEScan = BLEDevice::getScan();
  pBLEScan->setAdvertisedDeviceCallbacks(new MyAdvertisedDeviceCallbacks());
  pBLEScan->setActiveScan(true);
  Serial.println("Done");
}
void Bluetooth() {
  Serial.println();
  Serial.println("BLE Scan restarted.....");
  deviceFound = false;
  BLEScanResults scanResults = pBLEScan->start(5);
  if (deviceFound) {
    Serial.println("Connection Established");
    Encendida = true;
    digitalWrite(LED, HIGH);
    Contador = 0;
    delay(1000);
  }
  else {
    digitalWrite(LED, LOW);
    delay(1000);
  }
}
void loop() {
  Bluetooth();
}

I'm almost certain the issue has something to do with this line of code here.

 if (strcmp(Device.getManufacturerData().c_str(), knownAddresses[i].c_str()) == 0)
          known = true;

Before the if statement what results do you get when the Device.getManufacturerData().c_str() and knownAddresses[i].c_str() are printed?

The output you show doesn't have any match for "4c000215b9a6adffaea94703903a58437a4d9d2400000000fd". It has a match for the first 40 characters ("4c000215b9a6adffaea94703903a58437a4d9d24") but the data after that is different. Maybe you should only compare the first 40 characters.

Hi, the image was just to show that the ESP32 is capable of reading ibeacon simulator. This image was from a previous scan before I created a new ibeacon within the simulator that had a different manufacturer ID/Data (which is what I posted in the code).

Hi, I'm afraid I do not understand. How do you get a result before the if statement?

Your test is


if (strcmp(Device.getManufacturerData().c_str(), knownAddresses[i].c_str()) == 0)
          known = true;

The suggestion is to print the values that you are testing before they are tested

Serial.println(Device.getManufacturerData().c_str());
Serial.println(knownAddresses[i].c_str());


You will then know what values are being compared

Aw, I thought that is what you meant. Which is what I said I did here in this post - https://forum.arduino.cc/t/how-to-parse-data-to-use-in-a-conditional-statement/1021181

Did you note the observation from @johnwasser in post #3 ?

yes - and I responded in post #4

I think what I need is a way to parse the third data set that gets output from this

advertisedDevice.toString().c_str()

That would give me the manufacture data (which is what is underlined in blue) but how do I do that?

Post an example of the output from the device in code tags. It should be easy to parse it if you put it in an array as you receive each character

Unfortunately, I don't know how to do any of that. Give you give me some guidance?

Do what ?

Post the example output or parse it ?

How do you read the output ?

How to parse this example output?

I repeat

Apologies, but I do know what code tags are.

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use code tags (the </> icon above the compose window) to make it easier to read and copy for examination