Skip to content

Commit fc3fe6a

Browse files
committed
Updated BT Serial examples
1 parent 9eae97b commit fc3fe6a

File tree

2 files changed

+76
-29
lines changed

2 files changed

+76
-29
lines changed

libraries/BluetoothSerial/examples/SerialToSerialBT/SerialToSerialBT.ino

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@
66

77
#include "BluetoothSerial.h"
88

9+
//#define USE_PIN // Uncomment this to use PIN during pairing. The pin is specified on the line below
10+
const char *pin = "1234"; // Change this to more secure PIN.
11+
12+
String device_name = "ESP32-BT-Slave";
13+
914
#if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED)
1015
#error Bluetooth is not enabled! Please run `make menuconfig` to and enable it
1116
#endif
@@ -18,8 +23,13 @@ BluetoothSerial SerialBT;
1823

1924
void setup() {
2025
Serial.begin(115200);
21-
SerialBT.begin("ESP32test"); //Bluetooth device name
22-
Serial.println("The device started, now you can pair it with bluetooth!");
26+
SerialBT.begin(device_name); //Bluetooth device name
27+
Serial.printf("The device with name \"%s\" is started.\nNow you can pair it with Bluetooth!\n", device_name.c_str());
28+
//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
29+
#ifdef USE_PIN
30+
SerialBT.setPin(pin);
31+
Serial.println("Using PIN");
32+
#endif
2333
}
2434

2535
void loop() {

libraries/BluetoothSerial/examples/SerialToSerialBTM/SerialToSerialBTM.ino

Lines changed: 64 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,89 @@
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
33
//
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
76
//
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
815

916
#include "BluetoothSerial.h"
1017

18+
#define USE_NAME // Comment this to use MAC address instead of a slaveName
19+
//#define USE_PIN // Uncomment this to use PIN during pairing. The pin is specified on the line below
20+
const char *pin = "1234"; // Change this to reflect the pin expected by the real slave BT device
21+
22+
#ifndef USE_NAME
23+
#define USE_MAC
24+
#endif
25+
1126
#if !defined(CONFIG_BT_SPP_ENABLED)
1227
#error Serial Bluetooth not available or not enabled. It is only available for the ESP32 chip.
1328
#endif
1429

1530
BluetoothSerial SerialBT;
1631

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;
32+
#ifdef USE_NAME
33+
String slaveName = "ESP32-BT-Slave"; // Change this to reflect the real name of your slave BT device
34+
#endif
35+
36+
#ifdef USE_MAC
37+
String MACadd = "AA:BB:CC:11:22:33"; // This only for printing
38+
uint8_t address[6] = {0xAA, 0xBB, 0xCC, 0x11, 0x22, 0x33}; // Change this to reflect real MAC address of your slave BT device
39+
#endif
40+
41+
String myName = "ESP32-BT-Master";
2342

2443
void setup() {
44+
bool connected;
2545
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-
46+
47+
SerialBT.begin(myName, true);
48+
Serial.printf("The device \"%s\" started in master mode, make sure slave BT device is on!\n", myName.c_str());
49+
50+
#ifdef USE_PIN
51+
SerialBT.setPin(pin);
52+
Serial.println("Using PIN");
53+
#endif
54+
55+
// connect(address) is fast (up to 10 secs max), connect(slaveName) is slow (up to 30 secs max) as it needs
56+
// to resolve slaveName to address first, but it allows to connect to different devices with the same name.
57+
// Set CoreDebugLevel to Info to view devices Bluetooth address and device names
58+
#ifdef USE_NAME
59+
connected = SerialBT.connect(slaveName);
60+
Serial.printf("Connecting to slave BT device named \"%s\"\n", slaveName.c_str());
61+
#endif
62+
#ifdef USE_MAC
63+
connected = SerialBT.connect(address);
64+
Serial.print("Connecting to slave BT device with MAC "); Serial.println(MACadd);
65+
#endif
66+
3767
if(connected) {
38-
Serial.println("Connected Succesfully!");
68+
Serial.println("Connected Successfully!");
3969
} else {
4070
while(!SerialBT.connected(10000)) {
41-
Serial.println("Failed to connect. Make sure remote device is available and in range, then restart app.");
71+
Serial.println("Failed to connect. Make sure remote device is available and in range, then restart app.");
4272
}
4373
}
44-
// disconnect() may take upto 10 secs max
74+
// Disconnect() may take up to 10 secs max
4575
if (SerialBT.disconnect()) {
46-
Serial.println("Disconnected Succesfully!");
76+
Serial.println("Disconnected Successfully!");
4777
}
48-
// this would reconnect to the name(will use address, if resolved) or address used with connect(name/address).
78+
// This would reconnect to the slaveName(will use address, if resolved) or address used with connect(slaveName/address).
4979
SerialBT.connect();
80+
if(connected) {
81+
Serial.println("Reconnected Successfully!");
82+
} else {
83+
while(!SerialBT.connected(10000)) {
84+
Serial.println("Failed to reconnect. Make sure remote device is available and in range, then restart app.");
85+
}
86+
}
5087
}
5188

5289
void loop() {

0 commit comments

Comments
 (0)