用ESP32测量蓝牙连接力

Measuring bluetooth connection force with ESP32

如何测量ESP32的蓝牙连接力?我正在使用可用的 BLE 示例来检测连接的可能性,但我需要测量它的强度。谢谢。

我正在使用:

#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);
}`

在 wifi 中我正在使用这个功能:

// 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;
}

并且您的 BLE 功能与 WiFi.RSSI() 相同:(参见 github 源代码)

int BLEAdvertisedDevice::getRSSI() {
   return m_rssi;
} // getRSSI

只需将几行添加到您的 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);
    }
};