当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Arduino ArduinoBLE - bleCharacteristic.setEventHandler()用法及代码示例


设置指定事件发生时将调用的事件处理程序(回调)函数。

用法

bleCharacteristic.setEventHandler(eventType, callback)

参数

  • eventType:事件类型(BLESubscribed、BLEUnsubscribed、BLERead、BLEWritten)
  • callback: 事件发生时调用的函数

返回

示例


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

  

相关用法


注:本文由纯净天空筛选整理自arduino.cc大神的英文原创作品 ArduinoBLE - bleCharacteristic.setEventHandler()。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。