|
1 |
| -//This example code is in the Public Domain (or CC0 licensed, at your option.) |
2 |
| -//By Victor Tchistiak - 2019 |
| 1 | +// This example code is in the Public Domain (or CC0 licensed, at your option.) |
| 2 | +// By Victor Tchistiak - 2019 |
3 | 3 | //
|
4 |
| -//This example demostrates master mode bluetooth connection and pin |
5 |
| -//it creates a bridge between Serial and Classical Bluetooth (SPP) |
6 |
| -//this is an extention of the SerialToSerialBT example by Evandro Copercini - 2018 |
| 4 | +// This example demonstrates master mode Bluetooth connection to a slave BT device using PIN (password) |
| 5 | +// defined either by String "slaveName" by default "OBDII" or by MAC address |
7 | 6 | //
|
| 7 | +// This example creates a bridge between Serial and Classical Bluetooth (SPP) |
| 8 | +// This is an extension of the SerialToSerialBT example by Evandro Copercini - 2018 |
| 9 | +// |
| 10 | +// DO NOT try to connect to phone or laptop - they are master |
| 11 | +// devices, same as the ESP using this code - it will NOT work! |
| 12 | +// |
| 13 | +// You can try to flash a second ESP32 with the example SerialToSerialBT - it should |
| 14 | +// automatically pair with ESP32 running this code |
8 | 15 |
|
9 | 16 | #include "BluetoothSerial.h"
|
10 | 17 |
|
| 18 | +#define USE_NAME // Comment this to use MAC address instead of a slaveName |
| 19 | +const char *pin = "1234"; // Change this to reflect the pin expected by the real slave BT device |
| 20 | + |
11 | 21 | #if !defined(CONFIG_BT_SPP_ENABLED)
|
12 | 22 | #error Serial Bluetooth not available or not enabled. It is only available for the ESP32 chip.
|
13 | 23 | #endif
|
14 | 24 |
|
15 | 25 | BluetoothSerial SerialBT;
|
16 | 26 |
|
17 |
| -String MACadd = "AA:BB:CC:11:22:33"; |
18 |
| -uint8_t address[6] = {0xAA, 0xBB, 0xCC, 0x11, 0x22, 0x33}; |
19 |
| -//uint8_t address[6] = {0x00, 0x1D, 0xA5, 0x02, 0xC3, 0x22}; |
20 |
| -String name = "OBDII"; |
21 |
| -const char *pin = "1234"; //<- standard pin would be provided by default |
22 |
| -bool connected; |
| 27 | +#ifdef USE_NAME |
| 28 | + String slaveName = "ESP32-BT-Slave"; // Change this to reflect the real name of your slave BT device |
| 29 | +#else |
| 30 | + String MACadd = "AA:BB:CC:11:22:33"; // This only for printing |
| 31 | + uint8_t address[6] = {0xAA, 0xBB, 0xCC, 0x11, 0x22, 0x33}; // Change this to reflect real MAC address of your slave BT device |
| 32 | +#endif |
| 33 | + |
| 34 | +String myName = "ESP32-BT-Master"; |
23 | 35 |
|
24 | 36 | void setup() {
|
| 37 | + bool connected; |
25 | 38 | Serial.begin(115200);
|
26 |
| - //SerialBT.setPin(pin); |
27 |
| - SerialBT.begin("ESP32test", true); |
28 |
| - //SerialBT.setPin(pin); |
29 |
| - Serial.println("The device started in master mode, make sure remote BT device is on!"); |
30 |
| - |
31 |
| - // connect(address) is fast (upto 10 secs max), connect(name) is slow (upto 30 secs max) as it needs |
32 |
| - // to resolve name to address first, but it allows to connect to different devices with the same name. |
33 |
| - // Set CoreDebugLevel to Info to view devices bluetooth address and device names |
34 |
| - connected = SerialBT.connect(name); |
35 |
| - //connected = SerialBT.connect(address); |
36 |
| - |
| 39 | + |
| 40 | + SerialBT.begin(myName, true); |
| 41 | + Serial.printf("The device \"%s\" started in master mode, make sure slave BT device is on!\n", myName.c_str()); |
| 42 | + |
| 43 | + #ifndef USE_NAME |
| 44 | + SerialBT.setPin(pin); |
| 45 | + Serial.println("Using PIN"); |
| 46 | + #endif |
| 47 | + |
| 48 | + // connect(address) is fast (up to 10 secs max), connect(slaveName) is slow (up to 30 secs max) as it needs |
| 49 | + // to resolve slaveName to address first, but it allows to connect to different devices with the same name. |
| 50 | + // Set CoreDebugLevel to Info to view devices Bluetooth address and device names |
| 51 | + #ifdef USE_NAME |
| 52 | + connected = SerialBT.connect(slaveName); |
| 53 | + Serial.printf("Connecting to slave BT device named \"%s\"\n", slaveName.c_str()); |
| 54 | + #else |
| 55 | + connected = SerialBT.connect(address); |
| 56 | + Serial.print("Connecting to slave BT device with MAC "); Serial.println(MACadd); |
| 57 | + #endif |
| 58 | + |
37 | 59 | if(connected) {
|
38 |
| - Serial.println("Connected Succesfully!"); |
| 60 | + Serial.println("Connected Successfully!"); |
39 | 61 | } else {
|
40 | 62 | while(!SerialBT.connected(10000)) {
|
41 |
| - Serial.println("Failed to connect. Make sure remote device is available and in range, then restart app."); |
| 63 | + Serial.println("Failed to connect. Make sure remote device is available and in range, then restart app."); |
42 | 64 | }
|
43 | 65 | }
|
44 |
| - // disconnect() may take upto 10 secs max |
| 66 | + // Disconnect() may take up to 10 secs max |
45 | 67 | if (SerialBT.disconnect()) {
|
46 |
| - Serial.println("Disconnected Succesfully!"); |
| 68 | + Serial.println("Disconnected Successfully!"); |
47 | 69 | }
|
48 |
| - // this would reconnect to the name(will use address, if resolved) or address used with connect(name/address). |
| 70 | + // This would reconnect to the slaveName(will use address, if resolved) or address used with connect(slaveName/address). |
49 | 71 | SerialBT.connect();
|
| 72 | + if(connected) { |
| 73 | + Serial.println("Reconnected Successfully!"); |
| 74 | + } else { |
| 75 | + while(!SerialBT.connected(10000)) { |
| 76 | + Serial.println("Failed to reconnect. Make sure remote device is available and in range, then restart app."); |
| 77 | + } |
| 78 | + } |
50 | 79 | }
|
51 | 80 |
|
52 | 81 | void loop() {
|
|
0 commit comments