Skip to content

Support for Master mode, Pin and SSP #3219

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 44 commits into from
Oct 1, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
44 commits
Select commit Hold shift + click to select a range
bc2cafc
20190916 - initial: support for Master mode, Pin and SSP
Sep 16, 2019
94abf9f
20190916 - initial: Add example app for Master mode
Sep 17, 2019
9eb5e3a
20190916 - initial: Force another build
Sep 17, 2019
b346290
20190916 - connect would use resolved address as preference and remov…
Sep 17, 2019
dcefa81
20190916 - rework set/reset/default pin logic
Sep 17, 2019
bc06174
20190916 - cleanup: remove static vars, add/use constants, fix typos
Sep 17, 2019
c6efba2
20190916 - fix build issues and implement geoup events for status ver…
Sep 17, 2019
784e388
20190916 - remove extra lines,misc
Sep 17, 2019
c786489
20190916 - rework ESP_BT_GAP_DISC_RES_EVT, added SPP_DISCONNECTED bit…
Sep 17, 2019
04783c0
20190916 - Small log change to improve log sequencing
Sep 17, 2019
d9dfb29
20190916 - remove static from local vars
Sep 17, 2019
ad72ac3
20190916 - Limited scope and duration for the scan, log device addres…
Sep 18, 2019
0531490
20190916 - break property for loop during scan when name matches.
Sep 18, 2019
25facff
20190916 - misc
Sep 18, 2019
21248d0
20190916 - SPP_DISCONNECT state updates
Sep 18, 2019
e17beb1
20190916 - formatting, remove some strange syntax from initiator code
Sep 18, 2019
54c2a0a
20190916 - Add comments to the example about connect(...) usage and t…
Sep 18, 2019
e7c049a
20190916 - fix disconnect() without timeout
Sep 18, 2019
5254e6c
20190916 - Add example comment to view BT address and name during con…
Sep 18, 2019
9d92709
20190916 - wording in example comments
Sep 18, 2019
86c3e04
20190916 - rework connect() and disconnect() methods to help with con…
Sep 18, 2019
55793e6
20190916 - optimize code
Sep 18, 2019
d6c8137
20190916 - optimize code - more
Sep 18, 2019
8bc26ef
20190916 - add timeout for pin set
Sep 18, 2019
d975955
20190916 - change scan mode to ESP_BT_SCAN_MODE_CONNECTABLE_DISCOVERABLE
Sep 18, 2019
9e5c678
20190916 - update example code slightly
Sep 18, 2019
470d592
20190916 - increase READY_TIMEOUT to 10 secs
Sep 18, 2019
58ad4c0
20190916 - typo in example and move waitForConnect() to static area
Sep 18, 2019
a3e8b48
20190916 - update example comments
Sep 19, 2019
1b36dad
20190916 - update example comments
Sep 19, 2019
2ea5089
20190916 - update example comments
Sep 19, 2019
095978a
Merge branch 'master' into master
me-no-dev Sep 20, 2019
305b7a1
Merge branch 'master' into master
me-no-dev Sep 22, 2019
d7cf736
Merge branch 'master' into master
me-no-dev Sep 23, 2019
839baa8
20190916 - add new example to remove paired devices from ESP32
Sep 28, 2019
4f0727d
20190916 - correct typo in example
Sep 28, 2019
557221b
20190916 - update example comment, add remove_bond_device() method fo…
Sep 28, 2019
25cf9d9
20190916 - reword example comment.
Sep 28, 2019
8105bc9
20190916 - rename remove_bond_device()
Sep 28, 2019
8483a12
20190916 - rename removePairedDevice() to unpairDevice()
Sep 28, 2019
69b37b6
Merge branch 'master' into master
me-no-dev Oct 1, 2019
1578866
20190916 - code review changes
Oct 1, 2019
7add4bc
20190916 - fix return value in setup() od example
Oct 1, 2019
b8dbaf0
Merge branch 'master' into master
me-no-dev Oct 1, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
//This example code is in the Public Domain (or CC0 licensed, at your option.)
//By Victor Tchistiak - 2019
//
//This example demostrates master mode bluetooth connection and pin
//it creates a bridge between Serial and Classical Bluetooth (SPP)
//this is an extention of the SerialToSerialBT example by Evandro Copercini - 2018
//

#include "BluetoothSerial.h"

BluetoothSerial SerialBT;

String MACadd = "AA:BB:CC:11:22:33";
uint8_t address[6] = {0xAA, 0xBB, 0xCC, 0x11, 0x22, 0x33};
//uint8_t address[6] = {0x00, 0x1D, 0xA5, 0x02, 0xC3, 0x22};
String name = "OBDII";
char *pin = "1234"; //<- standard pin would be provided by default
bool connected;

void setup() {
Serial.begin(115200);
//SerialBT.setPin(pin);
SerialBT.begin("ESP32test", true);
//SerialBT.setPin(pin);
Serial.println("The device started in master mode, make sure remote BT device is on!");

// connect(address) is fast (upto 10 secs max), connect(name) is slow (upto 30 secs max) as it needs
// to resolve name to address first, but it allows to connect to different devices with the same name.
// Set CoreDebugLevel to Info to view devices bluetooth address and device names
connected = SerialBT.connect(name);
//connected = SerialBT.connect(address);

if(connected) {
Serial.println("Connected Succesfully!");
} else {
while(!SerialBT.connected(10000)) {
Serial.println("Failed to connect. Make sure remote device is available and in range, then restart app.");
}
}
// disconnect() may take upto 10 secs max
if (SerialBT.disconnect()) {
Serial.println("Disconnected Succesfully!");
}
// this would reconnect to the name(will use address, if resolved) or address used with connect(name/address).
SerialBT.connect();
}

void loop() {
if (Serial.available()) {
SerialBT.write(Serial.read());
}
if (SerialBT.available()) {
Serial.write(SerialBT.read());
}
delay(20);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
//This example code is in the Public Domain (or CC0 licensed, at your option.)
//By Victor Tchistiak - 2019
//
//This example demonstrates reading and removing paired devices stored on the ESP32 flash memory
//Sometimes you may find your ESP32 device could not connect to the remote device despite
//many successful connections earlier. This is most likely a result of client replacing your paired
//device info with new one from other device. The BT clients store connection info for paired devices,
//but it is limited to a few devices only. When new device pairs and number of stored devices is exceeded,
//one of the previously paired devices would be replaced with new one.
//The only remedy is to delete this saved bound device from your device flash memory
//and pair with the other device again.
//
#include "esp_bt_main.h"
#include "esp_bt_device.h"
#include"esp_gap_bt_api.h"
#include "esp_err.h"

#define REMOVE_BONDED_DEVICES 0 // <- Set to 0 to view all bonded devices addresses, set to 1 to remove

#define PAIR_MAX_DEVICES 20
uint8_t pairedDeviceBtAddr[PAIR_MAX_DEVICES][6];
char bda_str[18];

bool initBluetooth()
{
if(!btStart()) {
Serial.println("Failed to initialize controller");
return false;
}

if(esp_bluedroid_init() != ESP_OK) {
Serial.println("Failed to initialize bluedroid");
return false;
}

if(esp_bluedroid_enable() != ESP_OK) {
Serial.println("Failed to enable bluedroid");
return false;
}
return true;
}

char *bda2str(const uint8_t* bda, char *str, size_t size)
{
if (bda == NULL || str == NULL || size < 18) {
return NULL;
}
sprintf(str, "%02x:%02x:%02x:%02x:%02x:%02x",
bda[0], bda[1], bda[2], bda[3], bda[4], bda[5]);
return str;
}

void setup() {
Serial.begin(115200);

initBluetooth();
Serial.print("ESP32 bluetooth address: "); Serial.println(bda2str(esp_bt_dev_get_address(), bda_str, 18));
// Get the numbers of bonded/paired devices in the BT module
int count = esp_bt_gap_get_bond_device_num();
if(!count) {
Serial.println("No bonded device found.");
} else {
Serial.print("Bonded device count: "); Serial.println(count);
if(PAIR_MAX_DEVICES < count) {
count = PAIR_MAX_DEVICES;
Serial.print("Reset bonded device count: "); Serial.println(count);
}
esp_err_t tError = esp_bt_gap_get_bond_device_list(&count, pairedDeviceBtAddr);
if(ESP_OK == tError) {
for(int i = 0; i < count; i++) {
Serial.print("Found bonded device # "); Serial.print(i); Serial.print(" -> ");
Serial.println(bda2str(pairedDeviceBtAddr[i], bda_str, 18));
if(REMOVE_BONDED_DEVICES) {
esp_err_t tError = esp_bt_gap_remove_bond_device(pairedDeviceBtAddr[i]);
if(ESP_OK == tError) {
Serial.print("Removed bonded device # ");
} else {
Serial.print("Failed to remove bonded device # ");
}
Serial.println(i);
}
}
}
}
}

void loop() {}
Loading