Skip to content

How to setEventHandler in Custom Class?  #182

Open
@JAICHANGPARK

Description

@JAICHANGPARK

Hello I made my custom class
I used example code about callback led


#include <ArduinoBLE.h>
const int ledPin = LED_BUILTIN; // pin to use for the LED


class Sample0 {
  private:
  BLEService _ledService;
  BLEByteCharacteristic _switchCharacteristic;
public:
Sample0() : _ledService("19B10000-E8F2-537E-4F6C-D104768A1214"), _switchCharacteristic("19B10001-E8F2-537E-4F6C-D104768A1214", BLERead | BLEWrite){

  Serial.println("init Sample0");
}



  void init(){
// begin initialization
  if (!BLE.begin()) {
    Serial.println("starting BLE failed!");

    while (1)
      ;
  }

  // set the local name peripheral advertises
  BLE.setLocalName("LEDCallback");
  // set the UUID for the service this peripheral advertises
  BLE.setAdvertisedService(_ledService);

  // add the characteristic to the service
  _ledService.addCharacteristic(_switchCharacteristic);

  // add service
  BLE.addService(_ledService);

  // assign event handlers for connected, disconnected to peripheral
  BLE.setEventHandler(BLEConnected, blePeripheralConnectHandler);
  BLE.setEventHandler(BLEDisconnected, blePeripheralDisconnectHandler);

  // assign event handlers for characteristic
  _switchCharacteristic.setEventHandler(BLEWritten, switchCharacteristicWritten);
  // set an initial value for the characteristic
  _switchCharacteristic.setValue(0);

  // start advertising
  BLE.advertise();  

  Serial.println(("Bluetooth device active, waiting for connections..."));

  }

   void blePeripheralConnectHandler(BLEDevice central) {
  // central connected event handler
  Serial.print("Connected event, central: ");
  Serial.println(central.address());
}

 void blePeripheralDisconnectHandler(BLEDevice central) {
  // central disconnected event handler
  Serial.print("Disconnected event, central: ");
  Serial.println(central.address());
}

 void switchCharacteristicWritten(BLEDevice central, BLECharacteristic characteristic) {
  // central wrote new value to characteristic, update LED
  Serial.print("Characteristic event, written: ");

  if (characteristic.value()) {
    Serial.println("LED on");
    // digitalWrite(ledPin, HIGH);
  } else {
    Serial.println("LED off");
    // digitalWrite(ledPin, LOW);
  }
}
};

// BLEService ledService("19B10000-E8F2-537E-4F6C-D104768A1214"); // create service
// BLEByteCharacteristic switchCharacteristic("19B10001-E8F2-537E  4F6C-D104768A1214", BLERead | BLEWrite);

void setup() {
  Serial.begin(9600);
  while (!Serial);
  pinMode(ledPin, OUTPUT);  // use the LED pin as an output

   Sample0 sample;
   sample.init();
}

void loop() {
  // poll for BLE events
  BLE.poll();
}

Error code

error: invalid use of non-static member function

Metadata

Metadata

Assignees

No one assigned

    Labels

    topic: codeRelated to content of the project itselftype: enhancementProposed improvement

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions