Skip to content

Commit 38c4c06

Browse files
IonicEVme-no-dev
authored andcommitted
Support for Master mode, Pin and SSP (#3219)
* 20190916 - initial: support for Master mode, Pin and SSP * 20190916 - initial: Add example app for Master mode * 20190916 - initial: Force another build * 20190916 - connect would use resolved address as preference and remove now redundant _remote_address * 20190916 - rework set/reset/default pin logic * 20190916 - cleanup: remove static vars, add/use constants, fix typos * 20190916 - fix build issues and implement geoup events for status verification. * 20190916 - remove extra lines,misc * 20190916 - rework ESP_BT_GAP_DISC_RES_EVT, added SPP_DISCONNECTED bit for disconnect event. + timeout for disconnect() * 20190916 - Small log change to improve log sequencing * 20190916 - remove static from local vars * 20190916 - Limited scope and duration for the scan, log device address during scan in info mode as it is very difficult to find out sometimes. Fixed get_name_from_eir() not resetting the name when called. * 20190916 - break property for loop during scan when name matches. * 20190916 - misc * 20190916 - SPP_DISCONNECT state updates * 20190916 - formatting, remove some strange syntax from initiator code * 20190916 - Add comments to the example about connect(...) usage and timeouts * 20190916 - fix disconnect() without timeout * 20190916 - Add example comment to view BT address and name during connect(name) * 20190916 - wording in example comments * 20190916 - rework connect() and disconnect() methods to help with concurrency and more authoritative status returned back to caller. Automatic disconnect in connect() methods * 20190916 - optimize code * 20190916 - optimize code - more * 20190916 - add timeout for pin set * 20190916 - change scan mode to ESP_BT_SCAN_MODE_CONNECTABLE_DISCOVERABLE * 20190916 - update example code slightly * 20190916 - increase READY_TIMEOUT to 10 secs * 20190916 - typo in example and move waitForConnect() to static area * 20190916 - update example comments * 20190916 - update example comments * 20190916 - update example comments * 20190916 - add new example to remove paired devices from ESP32 * 20190916 - correct typo in example * 20190916 - update example comment, add remove_bond_device() method for convenience. * 20190916 - reword example comment. * 20190916 - rename remove_bond_device() * 20190916 - rename removePairedDevice() to unpairDevice() * 20190916 - code review changes * 20190916 - fix return value in setup() od example
1 parent b334b2c commit 38c4c06

File tree

4 files changed

+503
-8
lines changed

4 files changed

+503
-8
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
//This example code is in the Public Domain (or CC0 licensed, at your option.)
2+
//By Victor Tchistiak - 2019
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
7+
//
8+
9+
#include "BluetoothSerial.h"
10+
11+
BluetoothSerial SerialBT;
12+
13+
String MACadd = "AA:BB:CC:11:22:33";
14+
uint8_t address[6] = {0xAA, 0xBB, 0xCC, 0x11, 0x22, 0x33};
15+
//uint8_t address[6] = {0x00, 0x1D, 0xA5, 0x02, 0xC3, 0x22};
16+
String name = "OBDII";
17+
char *pin = "1234"; //<- standard pin would be provided by default
18+
bool connected;
19+
20+
void setup() {
21+
Serial.begin(115200);
22+
//SerialBT.setPin(pin);
23+
SerialBT.begin("ESP32test", true);
24+
//SerialBT.setPin(pin);
25+
Serial.println("The device started in master mode, make sure remote BT device is on!");
26+
27+
// connect(address) is fast (upto 10 secs max), connect(name) is slow (upto 30 secs max) as it needs
28+
// to resolve name to address first, but it allows to connect to different devices with the same name.
29+
// Set CoreDebugLevel to Info to view devices bluetooth address and device names
30+
connected = SerialBT.connect(name);
31+
//connected = SerialBT.connect(address);
32+
33+
if(connected) {
34+
Serial.println("Connected Succesfully!");
35+
} else {
36+
while(!SerialBT.connected(10000)) {
37+
Serial.println("Failed to connect. Make sure remote device is available and in range, then restart app.");
38+
}
39+
}
40+
// disconnect() may take upto 10 secs max
41+
if (SerialBT.disconnect()) {
42+
Serial.println("Disconnected Succesfully!");
43+
}
44+
// this would reconnect to the name(will use address, if resolved) or address used with connect(name/address).
45+
SerialBT.connect();
46+
}
47+
48+
void loop() {
49+
if (Serial.available()) {
50+
SerialBT.write(Serial.read());
51+
}
52+
if (SerialBT.available()) {
53+
Serial.write(SerialBT.read());
54+
}
55+
delay(20);
56+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
//This example code is in the Public Domain (or CC0 licensed, at your option.)
2+
//By Victor Tchistiak - 2019
3+
//
4+
//This example demonstrates reading and removing paired devices stored on the ESP32 flash memory
5+
//Sometimes you may find your ESP32 device could not connect to the remote device despite
6+
//many successful connections earlier. This is most likely a result of client replacing your paired
7+
//device info with new one from other device. The BT clients store connection info for paired devices,
8+
//but it is limited to a few devices only. When new device pairs and number of stored devices is exceeded,
9+
//one of the previously paired devices would be replaced with new one.
10+
//The only remedy is to delete this saved bound device from your device flash memory
11+
//and pair with the other device again.
12+
//
13+
#include "esp_bt_main.h"
14+
#include "esp_bt_device.h"
15+
#include"esp_gap_bt_api.h"
16+
#include "esp_err.h"
17+
18+
#define REMOVE_BONDED_DEVICES 0 // <- Set to 0 to view all bonded devices addresses, set to 1 to remove
19+
20+
#define PAIR_MAX_DEVICES 20
21+
uint8_t pairedDeviceBtAddr[PAIR_MAX_DEVICES][6];
22+
char bda_str[18];
23+
24+
bool initBluetooth()
25+
{
26+
if(!btStart()) {
27+
Serial.println("Failed to initialize controller");
28+
return false;
29+
}
30+
31+
if(esp_bluedroid_init() != ESP_OK) {
32+
Serial.println("Failed to initialize bluedroid");
33+
return false;
34+
}
35+
36+
if(esp_bluedroid_enable() != ESP_OK) {
37+
Serial.println("Failed to enable bluedroid");
38+
return false;
39+
}
40+
return true;
41+
}
42+
43+
char *bda2str(const uint8_t* bda, char *str, size_t size)
44+
{
45+
if (bda == NULL || str == NULL || size < 18) {
46+
return NULL;
47+
}
48+
sprintf(str, "%02x:%02x:%02x:%02x:%02x:%02x",
49+
bda[0], bda[1], bda[2], bda[3], bda[4], bda[5]);
50+
return str;
51+
}
52+
53+
void setup() {
54+
Serial.begin(115200);
55+
56+
initBluetooth();
57+
Serial.print("ESP32 bluetooth address: "); Serial.println(bda2str(esp_bt_dev_get_address(), bda_str, 18));
58+
// Get the numbers of bonded/paired devices in the BT module
59+
int count = esp_bt_gap_get_bond_device_num();
60+
if(!count) {
61+
Serial.println("No bonded device found.");
62+
} else {
63+
Serial.print("Bonded device count: "); Serial.println(count);
64+
if(PAIR_MAX_DEVICES < count) {
65+
count = PAIR_MAX_DEVICES;
66+
Serial.print("Reset bonded device count: "); Serial.println(count);
67+
}
68+
esp_err_t tError = esp_bt_gap_get_bond_device_list(&count, pairedDeviceBtAddr);
69+
if(ESP_OK == tError) {
70+
for(int i = 0; i < count; i++) {
71+
Serial.print("Found bonded device # "); Serial.print(i); Serial.print(" -> ");
72+
Serial.println(bda2str(pairedDeviceBtAddr[i], bda_str, 18));
73+
if(REMOVE_BONDED_DEVICES) {
74+
esp_err_t tError = esp_bt_gap_remove_bond_device(pairedDeviceBtAddr[i]);
75+
if(ESP_OK == tError) {
76+
Serial.print("Removed bonded device # ");
77+
} else {
78+
Serial.print("Failed to remove bonded device # ");
79+
}
80+
Serial.println(i);
81+
}
82+
}
83+
}
84+
}
85+
}
86+
87+
void loop() {}

0 commit comments

Comments
 (0)