Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Measuring bluetooth connection force with ESP32

How can I measure the bluetooth connection force with ESP32? I'm using the available example of BLE to detect the possibility of connection, but I need to measure its strength. Thank you.

I'm using:

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

int scanTime = 30; //In seconds

class MyAdvertisedDeviceCallbacks: public BLEAdvertisedDeviceCallbacks {
    void onResult(BLEAdvertisedDevice advertisedDevice) {
      Serial.printf("Advertised Device: %s \n", advertisedDevice.toString().c_str());
    }
};

void setup() {
  Serial.begin(115200);
  Serial.println("Scanning...");

  BLEDevice::init("");
  BLEScan* pBLEScan = BLEDevice::getScan(); //create new scan
  pBLEScan->setAdvertisedDeviceCallbacks(new MyAdvertisedDeviceCallbacks());
  pBLEScan->setActiveScan(true); //active scan uses more power, but get results faster
  BLEScanResults foundDevices = pBLEScan->start(scanTime);
  Serial.print("Devices found: ");
  Serial.println(foundDevices.getCount());
  Serial.println("Scan done!");
}

void loop() {
  // put your main code here, to run repeatedly:
  delay(2000);
}`
like image 878
Lucas Castro Avatar asked Sep 08 '25 09:09

Lucas Castro


2 Answers

Just a few lines added to your MyAdvertisedDeviceCallbacks: public BLEAdvertisedDeviceCallbacks :

class MyAdvertisedDeviceCallbacks: public BLEAdvertisedDeviceCallbacks {
    void onResult(BLEAdvertisedDevice advertisedDevice) {
      Serial.printf("Advertised Device: %s \n", advertisedDevice.toString().c_str
      int rssi = advertisedDevice.getRSSI();
      Serial.print("Rssi: ");
      Serial.println(rssi);
    }
}; 
like image 193
GrooverFromHolland Avatar answered Sep 10 '25 07:09

GrooverFromHolland


in wifi i am using this function:

// Take a number of measurements of the WiFi strength and return the average result.
int getStrength(int points){
  long rssi = 0;
  long averageRSSI=0;

  for (int i=0;i < points;i++){
    rssi += WiFi.RSSI(); // put your BLE function here
    delay(20);
  }

  averageRSSI=rssi/points;
  return averageRSSI;
}

and you have the same function for BLE than WiFi.RSSI(): (see in github source code)

int BLEAdvertisedDevice::getRSSI() {
   return m_rssi;
} // getRSSI
like image 34
Frenchy Avatar answered Sep 10 '25 07:09

Frenchy