Im trying to make an ESP32 scan for multiple beacons and turn on an LED once if the beacons are found.
I have been working with this code but I cant get it to stop scanning once the beacon is found.
What am I doing wrong here?
TIA!
Code: Select all
/*
*
* This detects advertising messages of BLE devices and compares it with stored MAC addresses.
* If one matches, it sends an MQTT message to swithc something
Copyright <2017> <Andreas Spiess>
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
Based on Neil Kolban's example file: https://github.com/nkolban/ESP32_BLE_Arduino
*/
#include "BLEDevice.h"
static BLEAddress *pServerAddress;
#define LED_GREEN 27
#define LED_RED 14
#define LED_BLUE 26
#define LED_WHITE 12
#define LED_CLEAR 13
BLEScan* pBLEScan;
BLEClient* pClient;
bool deviceFound = false;
bool device2Found = false;
String knownAddresses[] = { "e0:e1:e2:e3:f1:97"};
String knownAddresses2[] = { "ff:ff:50:05:62:4a"};
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 {
/**
Called for each advertising BLE server.
*/
void onResult(BLEAdvertisedDevice advertisedDevice) {
Serial.print("BLE Advertised Device found: ");
Serial.println(advertisedDevice.toString().c_str());
pServerAddress = new BLEAddress(advertisedDevice.getAddress());
bool known = 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(advertisedDevice.getRSSI());
if (advertisedDevice.getRSSI() > -100) deviceFound = true;
else deviceFound = false;
Serial.println(pServerAddress->toString().c_str());
advertisedDevice.getScan()->stop();
}
for (int i = 0; i < (sizeof(knownAddresses2) / sizeof(knownAddresses2[0])); i++) {
if (strcmp(pServerAddress->toString().c_str(), knownAddresses2[i].c_str()) == 0) known = true;
}
if (known) {
Serial.print("Device found: ");
Serial.println(advertisedDevice.getRSSI());
if (advertisedDevice.getRSSI() > -100) device2Found = true;
else device2Found = false;
Serial.println(pServerAddress->toString().c_str());
advertisedDevice.getScan()->stop();
}
}
}; // MyAdvertisedDeviceCallbacks
void setup() {
Serial.begin(115200);
Serial.println("Starting Arduino BLE Client application...");
pinMode(LED_BLUE, OUTPUT);
pinMode(LED_RED, OUTPUT);
pinMode(LED_GREEN, OUTPUT);
pinMode(LED_WHITE, OUTPUT);
pinMode(LED_CLEAR, OUTPUT);
digitalWrite(LED_BLUE, HIGH);
BLEDevice::init("");
pClient = BLEDevice::createClient();
Serial.println(" - Created client");
pBLEScan = BLEDevice::getScan();
pBLEScan->setAdvertisedDeviceCallbacks(new MyAdvertisedDeviceCallbacks());
pBLEScan->setActiveScan(true);
}
void loop() {
device1();
device2();
}
void device1() {
Serial.println();
Serial.println("BLE Scan restarted.....");
deviceFound = false;
BLEScanResults scanResults = pBLEScan->start(30);
if (deviceFound) {
Serial.println("on");
digitalWrite(LED_GREEN, HIGH);
digitalWrite(LED_RED, LOW);
delay(5000);
}
else {
Serial.println("off");
digitalWrite(LED_GREEN, LOW);
digitalWrite(LED_RED, HIGH);
}
}
void device2() {
Serial.println();
Serial.println("BLE Scan restarted.....");
device2Found = false;
BLEScanResults scanResults = pBLEScan->start(30);
if (device2Found) {
Serial.println("on");
digitalWrite(LED_WHITE, HIGH);
digitalWrite(LED_CLEAR, LOW);
delay(5000);
}
else {
Serial.println("off");
digitalWrite(LED_WHITE, LOW);
digitalWrite(LED_CLEAR, HIGH);
}
}
// End of loop