B4R Question ESP32 BLE Scanning - Inline C

Tron71

Member
Licensed User
Hi,

Would anyone have some sample in-line C for the ESP32 to perform scanning of MAC/Bluetooth addresses and their RSSI levels.

Thanks in advance.
M.
 

thetahsk

Active Member
Licensed User
Longtime User
Hi,
...
M.

..here is a very simple quick and simple port from https://github.com/espressif/arduino-esp32/blob/master/libraries/BLE/examples/BLE_scan/BLE_scan.ino

B4X:
#Region Project Attributes
    #AutoFlushLogs: True
    #CheckArrayBounds: True
    #StackBufferSize: 300
#End Region

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'Public variables can be accessed from all modules.
    Public Serial1 As Serial
End Sub

Private Sub AppStart
    Serial1.Initialize(115200)
    Log("AppStart")
    RunNative("setup", Null)
    AddLooper("Looper1")
End Sub

Private Sub Looper1
    RunNative("loop", Null)
End Sub

#if C

/*
  https://github.com/espressif/arduino-esp32/blob/master/libraries/BLE/examples/BLE_scan/BLE_scan.ino
*/

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

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

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

void setup(B4R::Object* o) {
  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(B4R::Object* o) {
  // put your main code here, to run repeatedly:
  BLEScanResults foundDevices = pBLEScan->start(scanTime, true);
  Serial.print("Devices found: ");
  Serial.println(foundDevices.getCount());
 
  // you can access first device with foundDevices.getDevice(0).getAddress()
 
  Serial.println("Scan done!");
  pBLEScan->clearResults();   // delete results fromBLEScan buffer to release memory
  delay(2000);
}

#End if
 
Last edited:
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…