-
Notifications
You must be signed in to change notification settings - Fork 7.6k
Allow BluetoothSerial::connect() with specified channel and more options #6380
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
Changes from all commits
7b44ebb
4ffb53e
12d5e72
364e8e0
57b269b
8baf733
9d29684
cf5724c
f43f196
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} |
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,6 +24,7 @@ | |
#include <esp_gap_bt_api.h> | ||
#include <esp_spp_api.h> | ||
#include <functional> | ||
#include <map> | ||
#include "BTScan.h" | ||
|
||
typedef std::function<void(const uint8_t *buffer, size_t size)> BluetoothSerialDataCb; | ||
|
@@ -50,6 +51,7 @@ class BluetoothSerial: public Stream | |
size_t write(const uint8_t *buffer, size_t size); | ||
void flush(); | ||
void end(void); | ||
void setTimeout(int timeoutMS); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would like to ask why the timeout should be set globally, and not for each There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Because that is how it's done in Arduino for Streams |
||
void onData(BluetoothSerialDataCb cb); | ||
esp_err_t register_callback(esp_spp_cb_t * callback); | ||
|
||
|
@@ -60,9 +62,12 @@ class BluetoothSerial: public Stream | |
void enableSSP(); | ||
bool setPin(const char *pin); | ||
bool connect(String remoteName); | ||
bool connect(uint8_t remoteAddress[]); | ||
bool connect(uint8_t remoteAddress[], int channel=0, esp_spp_sec_t sec_mask=(ESP_SPP_SEC_ENCRYPT|ESP_SPP_SEC_AUTHENTICATE), esp_spp_role_t role=ESP_SPP_ROLE_MASTER); | ||
bool connect(const BTAddress &remoteAddress, int channel=0, esp_spp_sec_t sec_mask=(ESP_SPP_SEC_ENCRYPT|ESP_SPP_SEC_AUTHENTICATE), esp_spp_role_t role=ESP_SPP_ROLE_MASTER) { | ||
return connect(*remoteAddress.getNative(), channel, sec_mask); }; | ||
bool connect(); | ||
bool connected(int timeout=0); | ||
bool isClosed(); | ||
bool isReady(bool checkMaster=false, int timeout=0); | ||
bool disconnect(); | ||
bool unpairDevice(uint8_t remoteAddress[]); | ||
|
@@ -73,14 +78,16 @@ class BluetoothSerial: public Stream | |
void discoverClear(); | ||
BTScanResults* getScanResults(); | ||
|
||
std::map<int, std::string> getChannels(const BTAddress &remoteAddress); | ||
|
||
const int INQ_TIME = 1280; // Inquire Time unit 1280 ms | ||
const int MIN_INQ_TIME = (ESP_BT_GAP_MIN_INQ_LEN * INQ_TIME); | ||
const int MAX_INQ_TIME = (ESP_BT_GAP_MAX_INQ_LEN * INQ_TIME); | ||
|
||
operator bool() const; | ||
private: | ||
String local_name; | ||
|
||
int timeoutTicks=0; | ||
}; | ||
|
||
#endif | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add this test as well (it is in every SPP example in order to warn users that try to run it in any chip but ESP32).
#if !defined(CONFIG_BT_SPP_ENABLED)
#error Serial Bluetooth not available or not enabled. It is only available for the ESP32 chip.
#endif