Continuous BLE Scan + get RSSI

ripplingwater
Posts: 2
Joined: Wed Oct 10, 2018 3:18 am

Continuous BLE Scan + get RSSI

Postby ripplingwater » Wed Oct 10, 2018 3:31 am

hi,

Couple of simple question here related to BLE Scan on ESP32 from a beginner. The below code comes as one of the examples in Arduino IDE after installing the ESP32 board. There are 2 things that I want to change here

1) Is it possible to get the scan to go forever (without looping or something)? Or will passing a value (not sure a zero?) as scanTime keep it scanning for BLEs forever? I think I hit a compile error when I didn't pass scanTime parameter

2) Is it possible to get the RSSI of the beacons as they are scanned?

Thanks a zillion.

Code: Select all

/*
   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 = 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);
}

HexaPro
Posts: 2
Joined: Mon Jul 20, 2020 5:15 am

Re: Continuous BLE Scan + get RSSI

Postby HexaPro » Mon Jul 20, 2020 5:59 am

Try using this:

Code: Select all

class MyAdvertisedDeviceCallbacks: public BLEAdvertisedDeviceCallbacks
{
    void onResult(BLEAdvertisedDevice advertisedDevice)
    {
      Serial.printf("Advertised Device: %s", advertisedDevice.toString().c_str());
      Serial.print(" RSSI: ");
      Serial.println(advertisedDevice.getRSSI());
    }
};

Who is online

Users browsing this forum: Bing [Bot] and 67 guests