#include "BluetoothSerial.h"
BluetoothSerial SerialBT;
bool isconnected = false;
void Bt_Status (esp_spp_cb_event_t event, esp_spp_cb_param_t *param) {
if (event == ESP_SPP_SRV_OPEN_EVT) {
Serial.println ("Bluetooth: Connected");
isconnected = true;
} else if (event == ESP_SPP_CLOSE_EVT ) {
Serial.println ("Bluetooth: Disconnected");
isconnected = false;
}
}
void setup() {
Serial.begin(115200);
while (!Serial);
Serial.print("\n");
Serial.println("dbg> ESP32 Test program. Start ...");
SerialBT.register_callback (Bt_Status);
Serial.println("The device started, now you can pair it with bluetooth!");
if(!SerialBT.begin("ESP32")){
Serial.println("An error occurred initializing Bluetooth");
}else{
Serial.println("Bluetooth initialized. Bluetooth Started!");
Serial.println("Bluetooth device name(ESP32), Ready to pair...");
}
}
void loop() {
if (Serial.available()){
char command = Serial.read();
Serial.print("Recived command : "); Serial.print(command); Serial.print(", ");
if(command == '3'){
Serial.println("REQ#3:");
cmd_tx_result_data1(); //test1: make string & TX data: swing raw data, bluetooth
} else{
Serial.println("Wrong command(3)");
Serial.println("Usage: ");
Serial.println(" REQ#3: make string & TX data");
}
}
}
void cmd_tx_result_data1() {
String tx_data1 = "here is some format code";
if (isconnected) {
Serial.print("dbg> tx_data1 = "); Serial.println(tx_data1);
SerialBT.print(tx_data1);
} else {
Serial.println ("dbg> Bluetooth: Client Not Connected");
}
}
Topic:
App & System Services
SubTopic:
Hardware
Tags: