Prvni ulozeni z chegewara githubu
This commit is contained in:
@ -0,0 +1,114 @@
|
||||
/**
|
||||
* Bluetooth Classic Example
|
||||
* Scan for devices - asyncronously, print device as soon as found
|
||||
* query devices for SPP - SDP profile
|
||||
* connect to first device offering a SPP connection
|
||||
*
|
||||
* Example python server:
|
||||
* source: https://gist.github.com/ukBaz/217875c83c2535d22a16ba38fc8f2a91
|
||||
*
|
||||
* Tested with Raspberry Pi onboard Wifi/BT, USB BT 4.0 dongles, USB BT 1.1 dongles,
|
||||
* 202202: does NOT work with USB BT 2.0 dongles when esp32 aduino lib is compiled with SSP support!
|
||||
* see https://github.com/espressif/esp-idf/issues/8394
|
||||
*
|
||||
* use ESP_SPP_SEC_ENCRYPT|ESP_SPP_SEC_AUTHENTICATE in connect() if remote side requests 'RequireAuthentication': dbus.Boolean(True),
|
||||
* use ESP_SPP_SEC_NONE or ESP_SPP_SEC_ENCRYPT|ESP_SPP_SEC_AUTHENTICATE in connect() if remote side has Authentication: False
|
||||
*/
|
||||
|
||||
#include <map>
|
||||
#include <BluetoothSerial.h>
|
||||
|
||||
#if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED)
|
||||
#error Bluetooth is not enabled! Please run `make menuconfig` to and enable it
|
||||
#endif
|
||||
|
||||
#if !defined(CONFIG_BT_SPP_ENABLED)
|
||||
#error Serial Bluetooth not available or not enabled. It is only available for the ESP32 chip.
|
||||
#endif
|
||||
|
||||
BluetoothSerial SerialBT;
|
||||
|
||||
|
||||
#define BT_DISCOVER_TIME 10000
|
||||
esp_spp_sec_t sec_mask=ESP_SPP_SEC_NONE; // or ESP_SPP_SEC_ENCRYPT|ESP_SPP_SEC_AUTHENTICATE to request pincode confirmation
|
||||
esp_spp_role_t role=ESP_SPP_ROLE_SLAVE; // or ESP_SPP_ROLE_MASTER
|
||||
|
||||
// std::map<BTAddress, BTAdvertisedDeviceSet> btDeviceList;
|
||||
|
||||
void setup() {
|
||||
Serial.begin(115200);
|
||||
if(! SerialBT.begin("ESP32test", true) ) {
|
||||
Serial.println("========== serialBT failed!");
|
||||
abort();
|
||||
}
|
||||
// SerialBT.setPin("1234"); // doesn't seem to change anything
|
||||
// SerialBT.enableSSP(); // doesn't seem to change anything
|
||||
|
||||
|
||||
Serial.println("Starting discoverAsync...");
|
||||
BTScanResults* btDeviceList = SerialBT.getScanResults(); // maybe accessing from different threads!
|
||||
if (SerialBT.discoverAsync([](BTAdvertisedDevice* pDevice) {
|
||||
// BTAdvertisedDeviceSet*set = reinterpret_cast<BTAdvertisedDeviceSet*>(pDevice);
|
||||
// btDeviceList[pDevice->getAddress()] = * set;
|
||||
Serial.printf(">>>>>>>>>>>Found a new device asynchronously: %s\n", pDevice->toString().c_str());
|
||||
} )
|
||||
) {
|
||||
delay(BT_DISCOVER_TIME);
|
||||
Serial.print("Stopping discoverAsync... ");
|
||||
SerialBT.discoverAsyncStop();
|
||||
Serial.println("discoverAsync stopped");
|
||||
delay(5000);
|
||||
if(btDeviceList->getCount() > 0) {
|
||||
BTAddress addr;
|
||||
int channel=0;
|
||||
Serial.println("Found devices:");
|
||||
for (int i=0; i < btDeviceList->getCount(); i++) {
|
||||
BTAdvertisedDevice *device=btDeviceList->getDevice(i);
|
||||
Serial.printf(" ----- %s %s %d\n", device->getAddress().toString().c_str(), device->getName().c_str(), device->getRSSI());
|
||||
std::map<int,std::string> channels=SerialBT.getChannels(device->getAddress());
|
||||
Serial.printf("scanned for services, found %d\n", channels.size());
|
||||
for(auto const &entry : channels) {
|
||||
Serial.printf(" channel %d (%s)\n", entry.first, entry.second.c_str());
|
||||
}
|
||||
if(channels.size() > 0) {
|
||||
addr = device->getAddress();
|
||||
channel=channels.begin()->first;
|
||||
}
|
||||
}
|
||||
if(addr) {
|
||||
Serial.printf("connecting to %s - %d\n", addr.toString().c_str(), channel);
|
||||
SerialBT.connect(addr, channel, sec_mask, role);
|
||||
}
|
||||
} else {
|
||||
Serial.println("Didn't find any devices");
|
||||
}
|
||||
} else {
|
||||
Serial.println("Error on discoverAsync f.e. not workin after a \"connect\"");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
String sendData="Hi from esp32!\n";
|
||||
|
||||
void loop() {
|
||||
if(! SerialBT.isClosed() && SerialBT.connected()) {
|
||||
if( SerialBT.write((const uint8_t*) sendData.c_str(),sendData.length()) != sendData.length()) {
|
||||
Serial.println("tx: error");
|
||||
} else {
|
||||
Serial.printf("tx: %s",sendData.c_str());
|
||||
}
|
||||
if(SerialBT.available()) {
|
||||
Serial.print("rx: ");
|
||||
while(SerialBT.available()) {
|
||||
int c=SerialBT.read();
|
||||
if(c >= 0) {
|
||||
Serial.print((char) c);
|
||||
}
|
||||
}
|
||||
Serial.println();
|
||||
}
|
||||
} else {
|
||||
Serial.println("not connected");
|
||||
}
|
||||
delay(1000);
|
||||
}
|
@ -0,0 +1,33 @@
|
||||
//This example code is in the Public Domain (or CC0 licensed, at your option.)
|
||||
//By Evandro Copercini - 2018
|
||||
//
|
||||
//This example creates a bridge between Serial and Classical Bluetooth (SPP)
|
||||
//and also demonstrate that SerialBT have the same functionalities of a normal Serial
|
||||
|
||||
#include "BluetoothSerial.h"
|
||||
|
||||
#if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED)
|
||||
#error Bluetooth is not enabled! Please run `make menuconfig` to and enable it
|
||||
#endif
|
||||
|
||||
#if !defined(CONFIG_BT_SPP_ENABLED)
|
||||
#error Serial Bluetooth not available or not enabled. It is only available for the ESP32 chip.
|
||||
#endif
|
||||
|
||||
BluetoothSerial SerialBT;
|
||||
|
||||
void setup() {
|
||||
Serial.begin(115200);
|
||||
SerialBT.begin("ESP32test"); //Bluetooth device name
|
||||
Serial.println("The device started, now you can pair it with bluetooth!");
|
||||
}
|
||||
|
||||
void loop() {
|
||||
if (Serial.available()) {
|
||||
SerialBT.write(Serial.read());
|
||||
}
|
||||
if (SerialBT.available()) {
|
||||
Serial.write(SerialBT.read());
|
||||
}
|
||||
delay(20);
|
||||
}
|
@ -0,0 +1,60 @@
|
||||
//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"
|
||||
|
||||
#if !defined(CONFIG_BT_SPP_ENABLED)
|
||||
#error Serial Bluetooth not available or not enabled. It is only available for the ESP32 chip.
|
||||
#endif
|
||||
|
||||
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";
|
||||
const 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);
|
||||
}
|
@ -0,0 +1,79 @@
|
||||
//This example code is in the Public Domain (or CC0 licensed, at your option.)
|
||||
//By Richard Li - 2020
|
||||
//
|
||||
//This example creates a bridge between Serial and Classical Bluetooth (SPP with authentication)
|
||||
//and also demonstrate that SerialBT have the same functionalities of a normal Serial
|
||||
|
||||
#include "BluetoothSerial.h"
|
||||
|
||||
#if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED)
|
||||
#error Bluetooth is not enabled! Please run `make menuconfig` to and enable it
|
||||
#endif
|
||||
|
||||
#if !defined(CONFIG_BT_SPP_ENABLED)
|
||||
#error Serial Bluetooth not available or not enabled. It is only available for the ESP32 chip.
|
||||
#endif
|
||||
|
||||
BluetoothSerial SerialBT;
|
||||
boolean confirmRequestPending = true;
|
||||
|
||||
void BTConfirmRequestCallback(uint32_t numVal)
|
||||
{
|
||||
confirmRequestPending = true;
|
||||
Serial.println(numVal);
|
||||
}
|
||||
|
||||
void BTAuthCompleteCallback(boolean success)
|
||||
{
|
||||
confirmRequestPending = false;
|
||||
if (success)
|
||||
{
|
||||
Serial.println("Pairing success!!");
|
||||
}
|
||||
else
|
||||
{
|
||||
Serial.println("Pairing failed, rejected by user!!");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void setup()
|
||||
{
|
||||
Serial.begin(115200);
|
||||
SerialBT.enableSSP();
|
||||
SerialBT.onConfirmRequest(BTConfirmRequestCallback);
|
||||
SerialBT.onAuthComplete(BTAuthCompleteCallback);
|
||||
SerialBT.begin("ESP32test"); //Bluetooth device name
|
||||
Serial.println("The device started, now you can pair it with bluetooth!");
|
||||
}
|
||||
|
||||
void loop()
|
||||
{
|
||||
if (confirmRequestPending)
|
||||
{
|
||||
if (Serial.available())
|
||||
{
|
||||
int dat = Serial.read();
|
||||
if (dat == 'Y' || dat == 'y')
|
||||
{
|
||||
SerialBT.confirmReply(true);
|
||||
}
|
||||
else
|
||||
{
|
||||
SerialBT.confirmReply(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (Serial.available())
|
||||
{
|
||||
SerialBT.write(Serial.read());
|
||||
}
|
||||
if (SerialBT.available())
|
||||
{
|
||||
Serial.write(SerialBT.read());
|
||||
}
|
||||
delay(20);
|
||||
}
|
||||
}
|
@ -0,0 +1,56 @@
|
||||
#include <BluetoothSerial.h>
|
||||
|
||||
#if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED)
|
||||
#error Bluetooth is not enabled! Please run `make menuconfig` to and enable it
|
||||
#endif
|
||||
|
||||
#if !defined(CONFIG_BT_SPP_ENABLED)
|
||||
#error Serial Bluetooth not available or not enabled. It is only available for the ESP32 chip.
|
||||
#endif
|
||||
|
||||
BluetoothSerial SerialBT;
|
||||
|
||||
|
||||
#define BT_DISCOVER_TIME 10000
|
||||
|
||||
|
||||
static bool btScanAsync = true;
|
||||
static bool btScanSync = true;
|
||||
|
||||
|
||||
void btAdvertisedDeviceFound(BTAdvertisedDevice* pDevice) {
|
||||
Serial.printf("Found a device asynchronously: %s\n", pDevice->toString().c_str());
|
||||
}
|
||||
|
||||
void setup() {
|
||||
Serial.begin(115200);
|
||||
SerialBT.begin("ESP32test"); //Bluetooth device name
|
||||
Serial.println("The device started, now you can pair it with bluetooth!");
|
||||
|
||||
|
||||
if (btScanAsync) {
|
||||
Serial.print("Starting discoverAsync...");
|
||||
if (SerialBT.discoverAsync(btAdvertisedDeviceFound)) {
|
||||
Serial.println("Findings will be reported in \"btAdvertisedDeviceFound\"");
|
||||
delay(10000);
|
||||
Serial.print("Stopping discoverAsync... ");
|
||||
SerialBT.discoverAsyncStop();
|
||||
Serial.println("stopped");
|
||||
} else {
|
||||
Serial.println("Error on discoverAsync f.e. not workin after a \"connect\"");
|
||||
}
|
||||
}
|
||||
|
||||
if (btScanSync) {
|
||||
Serial.println("Starting discover...");
|
||||
BTScanResults *pResults = SerialBT.discover(BT_DISCOVER_TIME);
|
||||
if (pResults)
|
||||
pResults->dump(&Serial);
|
||||
else
|
||||
Serial.println("Error on BT Scan, no result!");
|
||||
}
|
||||
}
|
||||
|
||||
void loop() {
|
||||
delay(100);
|
||||
}
|
@ -0,0 +1,91 @@
|
||||
//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"
|
||||
|
||||
#if !defined(CONFIG_BT_SPP_ENABLED)
|
||||
#error Serial Bluetooth not available or not enabled. It is only available for the ESP32 chip.
|
||||
#endif
|
||||
|
||||
#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() {}
|
Reference in New Issue
Block a user