请帮我!我正在尝试从我的 Iphone 的 Beacon 模拟器中读取 RSSI 值,但是,我遇到了以下几个问题。
- 在我激活我的信标之前,ESP32 总是得到 RSSI 值
- 停用后,我仍然可以获得 RSSI 和 UUID
我使用的代码和库:https ://github.com/nkolban/ESP32_BLE_Arduino
ESP32 版本:NODE32MCU-32S 1.0.4
编码:
#include <BLEDevice.h>
#include <BLEUtils.h>
#include <BLEScan.h>
#include <BLEUUID.h>
#include <BLEAdvertisedDevice.h>
#include <BLEBeacon.h>
BLEScan* pBLEScan;
BLEBeacon id;
int scanTime = 5; //In seconds
String reverse(String str) {
String rev;
for (int i = str.length() - 1; i >= 0; i--) {
rev = rev + str[i];
}
return rev;
}
class MyAdvertisedDeviceCallbacks: public BLEAdvertisedDeviceCallbacks
{
void onResult(BLEAdvertisedDevice advertisedDevice)
{
id.setData(advertisedDevice.getManufacturerData());
//Print UUID
Serial.print("UUID :");
String bUUID = id.getProximityUUID().toString().c_str();
//bUUID = reverse(bUUID);
Serial.print(bUUID);
//Print RSSI
Serial.print(",RSSI :");
int bRSSI = advertisedDevice.getRSSI();
Serial.print(bRSSI);
//Print Major
Serial.print(",Major :");
int bMajor = id.getMajor() / 256;
Serial.print(bMajor);
//Print Minor
Serial.print(",Minor :");
int bMinor = id.getMinor() / 256;
Serial.print(bMinor);
Serial.println("");
if ( bUUID == "00999999-9999-5555-5555-551111111111" && bRSSI >= -50) {
digitalWrite(2, HIGH);
} else {
digitalWrite(2, LOW);
}
}
};
void setup() {
Serial.begin(115200);
BLEDevice::init("");
pBLEScan = BLEDevice::getScan(); //create new scan
pBLEScan->setAdvertisedDeviceCallbacks(new MyAdvertisedDeviceCallbacks());
pBLEScan->setActiveScan(true);
Serial.println("Scanning...");
pinMode(2, OUTPUT);
}
void loop()
{
BLEScanResults foundDevices = pBLEScan->start(scanTime);
Serial.print("Devices found: ");
Serial.println(foundDevices.getCount());
Serial.println("Scan done!");
pBLEScan->clearResults(); // delete results fromBLEScan buffer to release memory
delay(1000);
}
这是我遇到的问题,读取的 UUID 仍然未读;但是,可以读取 RSSI