Bluetooth lowenergy 如何使用esp32提高信标扫描采样率

Bluetooth lowenergy 如何使用esp32提高信标扫描采样率,bluetooth-lowenergy,arduino-ide,esp32,Bluetooth Lowenergy,Arduino Ide,Esp32,我使用esp32通过信标的rssi来检测信标,但我遇到了一些问题,第一个是rssi值不稳定,所以我需要更多的rssi样本,第二个问题是esp32扫描样本速率太慢,速度越快,只有一秒的扫描速率,下面是我使用的代码 #include <BLEAdvertisedDevice.h> #include <BLEDevice.h> #include <BLEScan.h> const int PIN = 2; const int CUTOFF = -60; void

我使用esp32通过信标的rssi来检测信标,但我遇到了一些问题,第一个是rssi值不稳定,所以我需要更多的rssi样本,第二个问题是esp32扫描样本速率太慢,速度越快,只有一秒的扫描速率,下面是我使用的代码

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

const int PIN = 2;
const int CUTOFF = -60;

void setup() {
pinMode(PIN, OUTPUT);
BLEDevice::init("");
}

void loop() {
BLEScan *scan = BLEDevice::getScan();
scan->setActiveScan(true);
BLEScanResults results = scan->start(1);
int best = CUTOFF;
for (int i = 0; i < results.getCount(); i++) 
{
BLEAdvertisedDevice device = 
results.getDevice(i);
int rssi = device.getRSSI();
if (rssi > best) {
  best = rssi;
 }
}
digitalWrite(PIN, best > CUTOFF ? HIGH : 
LOW);
}
#包括
#包括
#包括
常数int PIN=2;
常数int截止=-60;
无效设置(){
引脚模式(引脚,输出);
BLEDevice::init(“”);
}
void循环(){
BLEScan*scan=BLEDevice::getScan();
扫描->设置活动扫描(真);
BLEScanResults=扫描->开始(1);
int最佳=截止;
对于(int i=0;i最佳){
最佳=rssi;
}
}
数字写入(引脚,最佳>截止?高:
低),;
}

但我想修改rssi样本,我尝试将扫描->开始(1)更改为扫描->开始(0),但结果不返回,如何做才能解决低样本问题,或者使用另一块板?

问题是,您要完成循环中的所有“工作”。
您是否知道ESP32微控制器以240 MHz的频率运行 我猜你的循环时间大约是400纳秒。 那么,您的设备如何知道是否找到了设备以及找到了多少设备。
您正试图在扫描开始后对扫描结果进行处理。
为了解决这个问题,Arduino for ESP32代码中有一个BLEAdvertisedDeviceCallbacks。
此回调将为找到的每个设备返回一个结果

下面是GITHUB的一个例子(Neil Kolban的ESP32_BLE_Arduino)

/*
基于IDF的Neil Kolban示例:https://github.com/nkolban/esp32-snippets 
/blob/master/cpp_utils/tests/BLE%20Tests/SampleScan.cpp
埃文德罗·科佩尔西尼将其移植到Arduino ESP32
*/
#包括
#包括
#包括
#包括
int扫描时间=5//几秒钟内
BLEScan*pBLEScan;
类MyAdvertisedDeviceCallbacks:public BleadAdvertisedDeviceCallbacks{
无效onResult(BLEAdvertisedDevice advertisedDevice){
Serial.printf(“广告设备:%s\n”,广告设备.toString().c_str());
}
};
无效设置(){
序列号开始(115200);
Serial.println(“扫描…”);
BLEDevice::init(“”);
pBLEScan=BLEDevice::getScan();//创建新扫描
pBLEScan->setAdvertisedDeviceCallbacks(新的MyAdvertisedDeviceCallbacks());
pBLEScan->setActiveScan(true);//活动扫描使用更大的功率,但会得到结果
//更快
pBLEScan->setInterval(100);
pBLEScan->setWindow(99);//小于或等于setInterval值
}
void循环(){
//将主代码放在此处,以便重复运行:
BLEScanResults foundDevices=pBLEScan->start(扫描时间,false);
串行打印(“找到的设备:”);
Serial.println(foundDevices.getCount());
Serial.println(“扫描完成!”);
pBLEScan->clearResults();//从BLEScan缓冲区删除结果以释放内存
延迟(2000年);
}
这是为了给你一个如何工作的想法,调整它以满足你自己的需要

/*
Based on Neil Kolban example for IDF: https://github.com/nkolban/esp32-snippets 
/blob/master/cpp_utils/tests/BLE%20Tests/SampleScan.cpp
Ported to Arduino ESP32 by Evandro Copercini
*/

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

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

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("");
pBLEScan = BLEDevice::getScan(); //create new scan
pBLEScan->setAdvertisedDeviceCallbacks(new MyAdvertisedDeviceCallbacks());
pBLEScan->setActiveScan(true); //active scan uses more power, but get results
//faster
pBLEScan->setInterval(100);
pBLEScan->setWindow(99);  // less or equal setInterval value
}

void loop() {
// put your main code here, to run repeatedly:
BLEScanResults foundDevices = pBLEScan->start(scanTime, false);
Serial.print("Devices found: ");
Serial.println(foundDevices.getCount());
Serial.println("Scan done!");
pBLEScan->clearResults();   // delete results fromBLEScan buffer to release memory
delay(2000);
}