-
Notifications
You must be signed in to change notification settings - Fork 7.6k
/
Copy pathSerialToSerialBT_Legacy.ino
65 lines (56 loc) · 1.84 KB
/
SerialToSerialBT_Legacy.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// This example code is in the Public Domain (or CC0 licensed, at your option.)
//
// This example creates a bridge between Serial and Classical Bluetooth (SPP with authentication)
// and also demonstrate that SerialBT have the same functionalities of a normal Serial
// Legacy pairing TODO
// Must be run as idf component ... todo
#include "BluetoothSerial.h"
// Check if Bluetooth is available
#if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED)
#error Bluetooth is not enabled! Please run `make menuconfig` to and enable it
#endif
// Check Serial Port Profile
#if !defined(CONFIG_BT_SPP_ENABLED)
#error Serial Port Profile for Bluetooth is not available or not enabled. It is only available for the ESP32 chip.
#endif
// Check Simple Secure Pairing
#if defined(CONFIG_BT_SSP_ENABLED)
#warning Legacy Pairing is disabled (CONFIG_BT_SSP_ENABLED is enabled. Disable it in menuconfig).
void setup(){}
void loop(){}
#else
const char * deviceName = "ESP32_Legacy_example";
BluetoothSerial SerialBT;
bool confirmRequestDone = false;
void BTAuthCompleteCallback(boolean success){
if (success){
confirmRequestDone = true;
Serial.println("Pairing success!!");
} else {
Serial.println("Pairing failed, rejected by user!!");
}
}
void serial_response(){
if (Serial.available()){
SerialBT.write(Serial.read());
}
if (SerialBT.available()){
Serial.write(SerialBT.read());
}
delay(20);
}
void setup(){
Serial.begin(115200);
SerialBT.onAuthComplete(BTAuthCompleteCallback);
SerialBT.begin(deviceName); // Initiate Bluetooth device with name in parameter
SerialBT.setPin("1234", 4);
Serial.printf("The device started with name \"%s\", now you can pair it with Bluetooth!\n", deviceName);
}
void loop(){
if (confirmRequestDone){
serial_response();
} else {
delay(1); // Feed the watchdog
}
}
#endif