使用ESP32测量蓝牙连接力

时间:2019-01-06 10:38:31

标签: bluetooth arduino bluetooth-lowenergy 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);
}`

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:0)

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

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

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