无法检索iBeacon UUID(ESP32 BLE扫描仪)

时间:2019-12-29 14:47:17

标签: bluetooth-lowenergy uuid arduino-ide esp32

我一直在使用ESP32扫描附近的iBeacon,但无法检索信标的UUID。串行监视器一直显示“ N / a”。但是,使用普通的android BLE扫描仪应用程序时,我能够获得信标的UUID。我的代码中缺少什么吗?

/*
   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 <BLEUUID.h>
#include <BLEAdvertisedDevice.h>

BLEScan* pBLEScan;

int scanTime = 5; //In seconds
class MyAdvertisedDeviceCallbacks: public BLEAdvertisedDeviceCallbacks 
{
    void onResult(BLEAdvertisedDevice advertisedDevice) 
    {
      //Print Name
      Serial.print("Name :");
      if (advertisedDevice.haveName())
      {
      Serial.println(advertisedDevice.getName().c_str());
      }
      else
      {
        Serial.println("N/a");
      }

      //Print RSSI
      Serial.print("RSSI :");
      Serial.println(advertisedDevice.getRSSI());

      //Print UUID
      Serial.print("UUID :");
      if (advertisedDevice.haveServiceUUID())
      {        
        Serial.println(advertisedDevice.getServiceUUID().toString().c_str());
      }
      else
      {
        Serial.println("N/a");
      }
      Serial.println("");  
    }

};


void setup() {
  Serial.begin(115200);
  Serial.println("Scanning...");
}

void loop() {
  BLEDevice::init("");
  pBLEScan = BLEDevice::getScan(); //create new scan
  pBLEScan->setActiveScan(true); //active scan uses more power, but get results faster
  pBLEScan->setAdvertisedDeviceCallbacks(new MyAdvertisedDeviceCallbacks());
  BLEScanResults foundDevices = pBLEScan->start(scanTime);
  Serial.print("Devices found: ");
  Serial.println(foundDevices.getCount());
  Serial.println("Scan done!");

  delay(1000);
}

0 个答案:

没有答案