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;


