Object oriented with ESP32 BLE

Hey all,

I need some help with my C++ skills. I've created a new class to handle BLE connections, it looks like this:

Connection.h

#ifndef MY_BUTTON_H
#define MY_BUTTON_H

#include <Arduino.h>
#include <BLEDevice.h>

class Connection {
  private: 
    BLEUUID serviceUUID;
    BLEUUID charUUID;
    BLEScan* pBLEScan;
    bool connected = false;
    void callback();
  
  public:
    Connection(BLEUUID serviceUUID, BLEUUID charUUID);
    void init();
    bool isConnected();
    void listen();

};

#endif

Connection.cpp

#include "connection.h"

Connection::Connection(BLEUUID serviceUUID, BLEUUID charUUID) {
  this->serviceUUID = serviceUUID;
  this->charUUID = charUUID;
};

void Connection::init() {
  BLEDevice::init("");
  this->pBLEScan = BLEDevice::getScan();
  this->pBLEScan->setAdvertisedDeviceCallbacks(this);
  pBLEScan->setInterval(1349);
  pBLEScan->setWindow(449);
  pBLEScan->setActiveScan(true);
  pBLEScan->start(5, false);
}


void Connection::listen() {

}

bool Connection::isConnected() {
  return connected;
}

void Connection::callback() {
   Serial.println("callback"); 
}

The problem is, I need to give BLEScan a class that inherent BLEAdvertisedDeviceCallbacks. This is how it looks like in the sample codes:

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

Now, I'm not really sure how I can make sure the instance of Connection class can be called on when onResult method will be called.

How do I set this to call the instance of the Connection class? Ideally I'd like to call the callback method but not sure how to do that.

this->pBLEScan->setAdvertisedDeviceCallbacks(this->callback());

Any tips please?

Yea, rather than making us piece together your code snippets, please post a small, complete code that we can put in the IDE and experiment with.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.