BLE get rssi from a device in a list

Greetings,
I am working on a BLE scanner using ESP32 using this
library. I got it working to scan nearby devices and getting rssi from those devices.

The problem is I only need to get rssi from a known device, and using the BLE_scan example I get the rssi from all of the devices that are scanned. I don't really know what to do since I am new to ESP and programming. This is the code that I write, any help is really appreciated guys, thank you.

#include <BLEDevice.h>
#include <BLEUtils.h>
#include <BLEScan.h>
#include <BLEAdvertisedDevice.h>

String knownBLEAddress[] = {"ff:ff:10:02:c8:57"};

bool device_found;
int scanTime = 5; //In seconds
BLEScan* pBLEScan;

class MyAdvertisedDeviceCallbacks: public BLEAdvertisedDeviceCallbacks {
    void onResult(BLEAdvertisedDevice advertisedDevice) {
      for (int i = 0; i < (sizeof(knownBLEAddress) / sizeof(knownBLEAddress[0])); i++)
      {
        if (strcmp(advertisedDevice.getAddress().toString().c_str(), knownBLEAddress[i].c_str()) == 0)
                        {
          device_found = true;
                          break;
                        }
        else
          device_found = false;
      }
      //Serial.printf("Advertised Device: %s \n", advertisedDevice.toString().c_str());
    }
};

void setup() {
  Serial.begin(115200); //Enable UART on ESP32
  Serial.println("Scanning..."); // Print Scanning
  BLEDevice::init("");
  pBLEScan = BLEDevice::getScan(); //create new scan
  pBLEScan->setAdvertisedDeviceCallbacks(new MyAdvertisedDeviceCallbacks()); //Init Callback Function
  pBLEScan->setActiveScan(true); //active scan uses more power, but get results faster
  pBLEScan->setInterval(100); // set Scan interval
  pBLEScan->setWindow(99);  // less or equal setInterval value
}

void loop() {
  // put your main code here, to run repeatedly:
  BLEScanResults foundDevices = pBLEScan->start(scanTime, false);
  for (int i = 0; i < foundDevices.getCount(); i++)
  {
    BLEAdvertisedDevice device = foundDevices.getDevice(i);
    if (device_found == true){
      int rssi = device.getRSSI();
      Serial.print("RSSI : ");
      Serial.println(rssi);
    }
    else {
      Serial.print("None");
    }
  }
  pBLEScan->clearResults();   // delete results fromBLEScan buffer to release memory
}

Welcome to the forum and thanks for using code tags in your first post.

Your topic has been moved to a more suitable location on the forum. Installation and Troubleshooting is not for problems with (nor for advise on) your project

Thank you I'll be careful in choosing the category on my next post!

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.