我的ESP32實做書籍:https://youyouyou.pixnet.net/blog/post/121105860
博客來網址:https://www.books.com.tw/products/0010901195


ESP32的藍芽2.0也就是BluetoothSerial真的是很方便,不過有一個小問題就是有時候距離太遠可能會連線中斷,如果斷線以後沒有任何提醒給使用者,那麼後續的工作就沒辦法進行了。

以下小程式可以建立一個callback事件,當裝置連線成功或連線中斷時,都會送出一個序列視窗的提醒(當然也可以改寫成LED燈號),給大家參考。


#include <BluetoothSerial.h>
BluetoothSerial BT; //藍芽callback事件
void callback(esp_spp_cb_event_t event, esp_spp_cb_param_t *param) {
  //藍芽連線完成
  if (event == ESP_SPP_SRV_OPEN_EVT) Serial.println("藍芽連線完成");
  //藍芽連線中斷
  if (event == ESP_SPP_CLOSE_EVT ) Serial.println("藍芽連線中斷");
}

void setup() {
  Serial.begin(115200);
  //建立callback事件
  BT.register_callback(callback);
  BT.begin("ESP32");
}

void loop() {
  //do what you want
}


image

本文參考網址:https://t.ly/dsR1

arrow
arrow

    夜市 小霸王 發表在 痞客邦 留言(1) 人氣()