首页 > 解决方案 > 用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);
}`

标签: bluetootharduinobluetooth-lowenergyesp32

解决方案


只需向您的 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);
    }
}; 

推荐阅读