當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


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()。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。