Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 57b269b

Browse files
committedMar 5, 2022
BluetoothSerial add discover and connect with channel number example
1 parent 364e8e0 commit 57b269b

File tree

3 files changed

+110
-0
lines changed

3 files changed

+110
-0
lines changed
 

‎libraries/BluetoothSerial/examples/DiscoverConnect/.skip.esp32c3

Whitespace-only changes.

‎libraries/BluetoothSerial/examples/DiscoverConnect/.skip.esp32s2

Whitespace-only changes.
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
/**
2+
* Bluetooth Classic Example
3+
* Scan for devices - asyncronously, print device as soon as found
4+
* query devices for SPP - SDP profile
5+
* connect to first device offering a SPP connection
6+
*
7+
* Example python server:
8+
* source: https://gist.github.com/ukBaz/217875c83c2535d22a16ba38fc8f2a91
9+
*
10+
* Tested with Raspberry Pi onboard Wifi/BT, USB BT 4.0 dongles, USB BT 1.1 dongles,
11+
* 202202: does NOT work with USB BT 2.0 dongles when esp32 aduino lib is compiled with SSP support!
12+
* see https://github.com/espressif/esp-idf/issues/8394
13+
*
14+
* use ESP_SPP_SEC_ENCRYPT|ESP_SPP_SEC_AUTHENTICATE in connect() if remote side requests 'RequireAuthentication': dbus.Boolean(True),
15+
* use ESP_SPP_SEC_NONE or ESP_SPP_SEC_ENCRYPT|ESP_SPP_SEC_AUTHENTICATE in connect() if remote side has Authentication: False
16+
*/
17+
18+
#include <map>
19+
#include <BluetoothSerial.h>
20+
21+
#if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED)
22+
#error Bluetooth is not enabled! Please run `make menuconfig` to and enable it
23+
#endif
24+
25+
BluetoothSerial SerialBT;
26+
27+
28+
#define BT_DISCOVER_TIME 10000
29+
esp_spp_sec_t sec_mask=ESP_SPP_SEC_NONE; // or ESP_SPP_SEC_ENCRYPT|ESP_SPP_SEC_AUTHENTICATE to request pincode confirmation
30+
esp_spp_role_t role=ESP_SPP_ROLE_SLAVE; // or ESP_SPP_ROLE_MASTER
31+
32+
// std::map<BTAddress, BTAdvertisedDeviceSet> btDeviceList;
33+
34+
void setup() {
35+
Serial.begin(115200);
36+
if(! SerialBT.begin("ESP32test", true) ) {
37+
Serial.println("========== serialBT failed!");
38+
abort();
39+
}
40+
// SerialBT.setPin("1234"); // doesn't seem to change anything
41+
// SerialBT.enableSSP(); // doesn't seem to change anything
42+
43+
44+
Serial.println("Starting discoverAsync...");
45+
BTScanResults* btDeviceList = SerialBT.getScanResults(); // maybe accessing from different threads!
46+
if (SerialBT.discoverAsync([](BTAdvertisedDevice* pDevice) {
47+
// BTAdvertisedDeviceSet*set = reinterpret_cast<BTAdvertisedDeviceSet*>(pDevice);
48+
// btDeviceList[pDevice->getAddress()] = * set;
49+
Serial.printf(">>>>>>>>>>>Found a new device asynchronously: %s\n", pDevice->toString().c_str());
50+
} )
51+
) {
52+
delay(BT_DISCOVER_TIME);
53+
Serial.print("Stopping discoverAsync... ");
54+
SerialBT.discoverAsyncStop();
55+
Serial.println("discoverAsync stopped");
56+
delay(5000);
57+
if(btDeviceList->getCount() > 0) {
58+
BTAddress addr;
59+
int channel=0;
60+
Serial.println("Found devices:");
61+
for (int i=0; i < btDeviceList->getCount(); i++) {
62+
BTAdvertisedDevice *device=btDeviceList->getDevice(i);
63+
Serial.printf(" ----- %s %s %d\n", device->getAddress().toString().c_str(), device->getName().c_str(), device->getRSSI());
64+
std::map<int,std::string> channels=SerialBT.getChannels(device->getAddress());
65+
Serial.printf("scanned for services, found %d\n", channels.size());
66+
for(auto const &entry : channels) {
67+
Serial.printf(" channel %d (%s)\n", entry.first, entry.second.c_str());
68+
}
69+
if(channels.size() > 0) {
70+
addr = device->getAddress();
71+
channel=channels.begin()->first;
72+
}
73+
}
74+
if(addr) {
75+
Serial.printf("connecting to %s - %d\n", addr.toString().c_str(), channel);
76+
SerialBT.connect(addr, channel, sec_mask, role);
77+
}
78+
} else {
79+
Serial.println("Didn't find any devices");
80+
}
81+
} else {
82+
Serial.println("Error on discoverAsync f.e. not workin after a \"connect\"");
83+
}
84+
}
85+
86+
87+
String sendData="Hi from esp32!\n";
88+
89+
void loop() {
90+
if(! SerialBT.isClosed() && SerialBT.connected()) {
91+
if( SerialBT.write((const uint8_t*) sendData.c_str(),sendData.length()) != sendData.length()) {
92+
Serial.println("tx: error");
93+
} else {
94+
Serial.printf("tx: %s",sendData.c_str());
95+
}
96+
if(SerialBT.available()) {
97+
Serial.print("rx: ");
98+
while(SerialBT.available()) {
99+
int c=SerialBT.read();
100+
if(c >= 0) {
101+
Serial.print((char) c);
102+
}
103+
}
104+
Serial.println();
105+
}
106+
} else {
107+
Serial.println("not connected");
108+
}
109+
delay(1000);
110+
}

0 commit comments

Comments
 (0)
Please sign in to comment.