//This example code is in the Public Domain (or CC0 licensed, at your option.)
//By Evandro Copercini - 2018
//
//This example creates a bridge between Serial and Classical Bluetooth (SPP)
//and also demonstrate that SerialBT have the same functionalities of a normal Serial
#include "BluetoothSerial.h"
//#define USE_PIN // Uncomment this to use PIN during pairing. The pin is specified on the line below
const char *pin = "1234"; // Change this to more secure PIN.
#define BLUETOOTH_DEVICE_NAME "ESP32"
#if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED)
#error Bluetooth is not enabled! Please run `make menuconfig` to and enable it
#endif
#if !defined(CONFIG_BT_SPP_ENABLED)
#error Serial Bluetooth not available or not enabled. It is only available for the ESP32 chip.
#endif
BluetoothSerial SerialBT;
void setup() {
Serial.begin(115200);
SerialBT.begin(BLUETOOTH_DEVICE_NAME); //Bluetooth device name
//Serial.println("The device with name \"%s\" is started.\nNow you can pair it with Bluetooth!\n", BLUETOOTH_DEVICE_NAME);
//Serial.printf("The device with name "%s" and MAC address %s is started.\nNow you can pair it with Bluetooth!\n", device_name.c_str(), SerialBT.getMacString()); // Use this after the MAC method is implemented
//Serial.println("Bluetooth started");
#ifdef USE_PIN
SerialBT.setPin(pin);
Serial.println("Using PIN");
#endif
}
void loop() {
if (Serial.available()) { // Check if there is any data available on the Serial interface.
SerialBT.write(Serial.read()); // Read the available data and send it over Bluetooth.
}
if (SerialBT.available()) { // Check if there is any data available on the Bluetooth interface.
Serial.write(SerialBT.read()); // Read the available data and send it over Serial.
}
delay(20); // Delay for 20 milliseconds before checking for available data again.
}