arduino-esp32/libraries/BluetoothSerial/examples/SerialToSerialBTM/SerialToSerialBTM.ino

57 lines
1.9 KiB
Arduino
Raw Normal View History

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
2019-10-01 16:34:46 +02:00
//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);
}