ArduinoBLE - bleCharacteristic.setEventHandler()

Set the event handler (callback) function that will be called when the specified event occurs.

Syntax

bleCharacteristic.setEventHandler(eventType, callback)

Parameters

  • eventType: event type (BLESubscribed, BLEUnsubscribed, BLERead, BLEWritten)
  • callback: function to call when the event occurs

Returns

Nothing

Example


// create switch characteristic and allow remote device to read and write
BLEByteCharacteristic switchCharacteristic("19B10001-E8F2-537E-4F6C-D104768A1214", BLERead | BLEWrite);




  // assign event handlers for characteristic
  switchCharacteristic.setEventHandler(BLEWritten, switchCharacteristicWritten);



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

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