I've attached something I did similar for WiFi on the ESP8266. 'tis been a while and both ESP8266 core and IDF have advanced as has ArduinoIDE ... may not compile now error-free wo some tinkering
As knownAddrIndex is 0, the for loop will not execute at all because the condition i < knownAddrIndex is immediately false
And your logic is wrong : your for loop - if it was looping at all - would check only the first item in the array, then if that item is not the same String, it will add the String in the array without checking all other items.
You have to check all items of the array before deciding to add your String or not
Below is the script for the use case applied to this question.
It does send an alert via telegram app when the first bluetooth device within the signal strength threshold is detected, however, it fails to send alerts for any further devices detected after that.
I am trying to send an alert message upon detection of every new bluetooth device scanned, only once per device ble address.
I think it has something to do with the nested loops logic but I don't know for certain.
#include <BLEDevice.h>
#include <WiFi.h>
#include <WiFiClientSecure.h>
#include <UniversalTelegramBot.h>
#include <ArduinoJson.h>
#include <BLEUtils.h>
#include <BLEScan.h>
#include <BLEAdvertisedDevice.h>
// Replace with your network credentials
const char* ssid = "mywifiapssid";
const char* password = "mywifipassword";
// Initialize Telegram BOT
#define BOTtoken "mybottoken" // your Bot Token (Get from Botfather)
// Use @myidbot to find out the chat ID of an individual or a group
// Also note that you need to click "start" on a bot before it can
// message you
#define CHAT_ID "mychatid"
int scanTime = 5; //In seconds
int RSSI_THRESHOLD = -99;
String notifyAlert = "";
String knownAddr[20];
int knownAddrIndex = 0;
boolean found = false;
boolean sendfound = false;
BLEScan* pBLEScan;
WiFiClientSecure client;
UniversalTelegramBot bot(BOTtoken, client);
class MyAdvertisedDeviceCallbacks: public BLEAdvertisedDeviceCallbacks {
void onResult(BLEAdvertisedDevice advertisedDevice) {
//Serial.printf("Advertised Device: %s \n", advertisedDevice.toString().c_str());
notifyAlert = (advertisedDevice.getAddress().toString().c_str());
for (int i = 0; i < knownAddrIndex; i++) {
if (notifyAlert == knownAddr[i]) {
Serial.println("Matching Addr already inside array.");
found = true;
}
}
if (!found)
{
knownAddr[knownAddrIndex++] = notifyAlert;
Serial.println("new device added to known device array!");
Serial.print("array index:");
Serial.println(knownAddrIndex);
sendfound = true;
found = false; //added 12/26/2021 to allow more than one result.
}
}
};
void setup() {
Serial.begin(115200);
// Attempt to connect to Wifi network:
Serial.print("Connecting Wifi: ");
Serial.println(ssid);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
client.setCACert(TELEGRAM_CERTIFICATE_ROOT); // Add root certificate for api.telegram.org
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(500);
}
Serial.println("");
Serial.println("WiFi connected");
//Serial.print("IP address: ");
//Serial.println(WiFi.localIP());
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
bot.sendMessage(CHAT_ID, "Bot started up", "");
Serial.println("Sent startup notification");
}
void loop() {
BLEScanResults foundDevices = pBLEScan->start(scanTime, false);
Serial.print("Devices found: ");
Serial.println(foundDevices.getCount());
Serial.println("Scan done!");
for (int i = 0; i < foundDevices.getCount(); i++)
{
BLEAdvertisedDevice device = foundDevices.getDevice(i);
int rssi = device.getRSSI();
Serial.print("RSSI: ");
Serial.println(rssi);
if (rssi > RSSI_THRESHOLD)
{
Serial.println("RSSI past threshold");
if (sendfound == true) {
Serial.println("New device detected!");
Serial.println(notifyAlert);
bot.sendMessage(CHAT_ID, notifyAlert, "");
Serial.println("notified");
sendfound = false;
}
}
}
pBLEScan->clearResults(); // delete results fromBLEScan buffer to release memory
delay(2000);
}