Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 41c372c

Browse filesBrowse files
[2.0.0] BtClassic Discovery with info without connect (espressif#4811)
Hey guys, so I wanted to do a BtClassic Discovery without the need to call connect and to list all found devices on a display and continue work with that list. I wasn't capable to test the example code with my file structure, but I did use the discovery already in some different situations. However when I noted that the Bluedroid stack won't let me enforce an RfComm SPP connection to a GPS Device (Skytraxx 2 plus, I guess its interface is built so simple that it doesn't advertise its SPP over SDP), I will probably have to switch to BtStack (BlueKitchen) and stop on this side meanwhile
1 parent 223acb3 commit 41c372c
Copy full SHA for 41c372c

File tree

Expand file treeCollapse file tree

12 files changed

+609
-6
lines changed
Filter options
Expand file treeCollapse file tree

12 files changed

+609
-6
lines changed

‎CMakeLists.txt

Copy file name to clipboardExpand all lines: CMakeLists.txt
+3Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ set(LIBRARY_SRCS
4545
libraries/ArduinoOTA/src/ArduinoOTA.cpp
4646
libraries/AsyncUDP/src/AsyncUDP.cpp
4747
libraries/BluetoothSerial/src/BluetoothSerial.cpp
48+
libraries/BluetoothSerial/src/BTAddress.cpp
49+
libraries/BluetoothSerial/src/BTAdvertisedDeviceSet.cpp
50+
libraries/BluetoothSerial/src/BTScanResultsSet.cpp
4851
libraries/DNSServer/src/DNSServer.cpp
4952
libraries/EEPROM/src/EEPROM.cpp
5053
libraries/ESPmDNS/src/ESPmDNS.cpp

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

Copy file name to clipboardExpand all lines: libraries/BluetoothSerial/examples/bt_classic_device_discovery/.skip.esp32c3
Whitespace-only changes.

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

Copy file name to clipboardExpand all lines: libraries/BluetoothSerial/examples/bt_classic_device_discovery/.skip.esp32s2
Whitespace-only changes.
+52Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#include <BluetoothSerial.h>
2+
3+
#if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED)
4+
#error Bluetooth is not enabled! Please run `make menuconfig` to and enable it
5+
#endif
6+
7+
BluetoothSerial SerialBT;
8+
9+
10+
#define BT_DISCOVER_TIME 10000
11+
12+
13+
static bool btScanAsync = true;
14+
static bool btScanSync = true;
15+
16+
17+
void btAdvertisedDeviceFound(BTAdvertisedDevice* pDevice) {
18+
Serial.printf("Found a device asynchronously: %s\n", pDevice->toString().c_str());
19+
}
20+
21+
void setup() {
22+
Serial.begin(115200);
23+
SerialBT.begin("ESP32test"); //Bluetooth device name
24+
Serial.println("The device started, now you can pair it with bluetooth!");
25+
26+
27+
if (btScanAsync) {
28+
Serial.print("Starting discoverAsync...");
29+
if (SerialBT.discoverAsync(btAdvertisedDeviceFound)) {
30+
Serial.println("Findings will be reported in \"btAdvertisedDeviceFound\"");
31+
delay(10000);
32+
Serial.print("Stopping discoverAsync... ");
33+
SerialBT.discoverAsyncStop();
34+
Serial.println("stopped");
35+
} else {
36+
Serial.println("Error on discoverAsync f.e. not workin after a \"connect\"");
37+
}
38+
}
39+
40+
if (btScanSync) {
41+
Serial.println("Starting discover...");
42+
BTScanResults *pResults = SerialBT.discover(BT_DISCOVER_TIME);
43+
if (pResults)
44+
pResults->dump(&Serial);
45+
else
46+
Serial.println("Error on BT Scan, no result!");
47+
}
48+
}
49+
50+
void loop() {
51+
delay(100);
52+
}
+96Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
/*
2+
* BTAddress.cpp
3+
*
4+
* Created on: Jul 2, 2017
5+
* Author: kolban
6+
* Ported on: Feb 5, 2021
7+
* Author: Thomas M. (ArcticSnowSky)
8+
*/
9+
#include "sdkconfig.h"
10+
#if defined(CONFIG_BT_ENABLED)
11+
12+
#include "BTAddress.h"
13+
#include <string>
14+
#include <sstream>
15+
#include <iomanip>
16+
#include <string.h>
17+
#include <stdio.h>
18+
#include <malloc.h>
19+
#ifdef ARDUINO_ARCH_ESP32
20+
#include "esp32-hal-log.h"
21+
#endif
22+
23+
24+
/**
25+
* @brief Create an address from the native ESP32 representation.
26+
* @param [in] address The native representation.
27+
*/
28+
BTAddress::BTAddress(esp_bd_addr_t address) {
29+
memcpy(m_address, address, ESP_BD_ADDR_LEN);
30+
} // BTAddress
31+
32+
33+
/**
34+
* @brief Create an address from a hex string
35+
*
36+
* A hex string is of the format:
37+
* ```
38+
* 00:00:00:00:00:00
39+
* ```
40+
* which is 17 characters in length.
41+
*
42+
* @param [in] stringAddress The hex representation of the address.
43+
*/
44+
BTAddress::BTAddress(std::string stringAddress) {
45+
if (stringAddress.length() != 17) return;
46+
47+
int data[6];
48+
sscanf(stringAddress.c_str(), "%x:%x:%x:%x:%x:%x", &data[0], &data[1], &data[2], &data[3], &data[4], &data[5]);
49+
m_address[0] = (uint8_t) data[0];
50+
m_address[1] = (uint8_t) data[1];
51+
m_address[2] = (uint8_t) data[2];
52+
m_address[3] = (uint8_t) data[3];
53+
m_address[4] = (uint8_t) data[4];
54+
m_address[5] = (uint8_t) data[5];
55+
} // BTAddress
56+
57+
58+
/**
59+
* @brief Determine if this address equals another.
60+
* @param [in] otherAddress The other address to compare against.
61+
* @return True if the addresses are equal.
62+
*/
63+
bool BTAddress::equals(BTAddress otherAddress) {
64+
return memcmp(otherAddress.getNative(), m_address, 6) == 0;
65+
} // equals
66+
67+
68+
/**
69+
* @brief Return the native representation of the address.
70+
* @return The native representation of the address.
71+
*/
72+
esp_bd_addr_t *BTAddress::getNative() {
73+
return &m_address;
74+
} // getNative
75+
76+
77+
/**
78+
* @brief Convert a BT address to a string.
79+
*
80+
* A string representation of an address is in the format:
81+
*
82+
* ```
83+
* xx:xx:xx:xx:xx:xx
84+
* ```
85+
*
86+
* @return The string representation of the address.
87+
*/
88+
std::string BTAddress::toString() {
89+
auto size = 18;
90+
char *res = (char*)malloc(size);
91+
snprintf(res, size, "%02x:%02x:%02x:%02x:%02x:%02x", m_address[0], m_address[1], m_address[2], m_address[3], m_address[4], m_address[5]);
92+
std::string ret(res);
93+
free(res);
94+
return ret;
95+
} // toString
96+
#endif
+36Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* BTAddress.h
3+
*
4+
* Created on: Jul 2, 2017
5+
* Author: kolban
6+
* Ported on: Feb 5, 2021
7+
* Author: Thomas M. (ArcticSnowSky)
8+
*/
9+
10+
#ifndef COMPONENTS_CPP_UTILS_BTADDRESS_H_
11+
#define COMPONENTS_CPP_UTILS_BTADDRESS_H_
12+
#include "sdkconfig.h"
13+
#if defined(CONFIG_BT_ENABLED)
14+
#include <esp_gap_bt_api.h> // ESP32 BT
15+
#include <string>
16+
17+
18+
/**
19+
* @brief A %BT device address.
20+
*
21+
* Every %BT device has a unique address which can be used to identify it and form connections.
22+
*/
23+
class BTAddress {
24+
public:
25+
BTAddress(esp_bd_addr_t address);
26+
BTAddress(std::string stringAddress);
27+
bool equals(BTAddress otherAddress);
28+
esp_bd_addr_t* getNative();
29+
std::string toString();
30+
31+
private:
32+
esp_bd_addr_t m_address;
33+
};
34+
35+
#endif /* CONFIG_BT_ENABLED */
36+
#endif /* COMPONENTS_CPP_UTILS_BTADDRESS_H_ */
+65Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
* BTAdvertisedDevice.h
3+
*
4+
* Created on: Feb 5, 2021
5+
* Author: Thomas M. (ArcticSnowSky)
6+
*/
7+
8+
#ifndef __BTADVERTISEDDEVICE_H__
9+
#define __BTADVERTISEDDEVICE_H__
10+
11+
#include "BTAddress.h"
12+
13+
14+
class BTAdvertisedDevice {
15+
public:
16+
virtual ~BTAdvertisedDevice() = default;
17+
18+
virtual BTAddress getAddress();
19+
virtual uint32_t getCOD();
20+
virtual std::string getName();
21+
virtual int8_t getRSSI();
22+
23+
24+
virtual bool haveCOD();
25+
virtual bool haveName();
26+
virtual bool haveRSSI();
27+
28+
virtual std::string toString();
29+
};
30+
31+
class BTAdvertisedDeviceSet : public virtual BTAdvertisedDevice {
32+
public:
33+
BTAdvertisedDeviceSet();
34+
//~BTAdvertisedDeviceSet() = default;
35+
36+
37+
BTAddress getAddress();
38+
uint32_t getCOD();
39+
std::string getName();
40+
int8_t getRSSI();
41+
42+
43+
bool haveCOD();
44+
bool haveName();
45+
bool haveRSSI();
46+
47+
std::string toString();
48+
49+
void setAddress(BTAddress address);
50+
void setCOD(uint32_t cod);
51+
void setName(std::string name);
52+
void setRSSI(int8_t rssi);
53+
54+
bool m_haveCOD;
55+
bool m_haveName;
56+
bool m_haveRSSI;
57+
58+
59+
BTAddress m_address = BTAddress((uint8_t*)"\0\0\0\0\0\0");
60+
uint32_t m_cod;
61+
std::string m_name;
62+
int8_t m_rssi;
63+
};
64+
65+
#endif
+78Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/*
2+
* BTAdvertisedDeviceSet.cpp
3+
*
4+
* Created on: Feb 5, 2021
5+
* Author: Thomas M. (ArcticSnowSky)
6+
*/
7+
8+
#include "sdkconfig.h"
9+
#if defined(CONFIG_BT_ENABLED)
10+
11+
//#include <map>
12+
13+
#include "BTAdvertisedDevice.h"
14+
//#include "BTScan.h"
15+
16+
17+
BTAdvertisedDeviceSet::BTAdvertisedDeviceSet() {
18+
m_cod = 0;
19+
m_name = "";
20+
m_rssi = 0;
21+
22+
m_haveCOD = false;
23+
m_haveName = false;
24+
m_haveRSSI = false;
25+
} // BTAdvertisedDeviceSet
26+
27+
BTAddress BTAdvertisedDeviceSet::getAddress() { return m_address; }
28+
uint32_t BTAdvertisedDeviceSet::getCOD() { return m_cod; }
29+
std::string BTAdvertisedDeviceSet::getName() { return m_name; }
30+
int8_t BTAdvertisedDeviceSet::getRSSI() { return m_rssi; }
31+
32+
33+
bool BTAdvertisedDeviceSet::haveCOD() { return m_haveCOD; }
34+
bool BTAdvertisedDeviceSet::haveName() { return m_haveName; }
35+
bool BTAdvertisedDeviceSet::haveRSSI() { return m_haveRSSI; }
36+
37+
/**
38+
* @brief Create a string representation of this device.
39+
* @return A string representation of this device.
40+
*/
41+
std::string BTAdvertisedDeviceSet::toString() {
42+
std::string res = "Name: " + getName() + ", Address: " + getAddress().toString();
43+
if (haveCOD()) {
44+
char val[6];
45+
snprintf(val, sizeof(val), "%d", getCOD());
46+
res += ", cod: ";
47+
res += val;
48+
}
49+
if (haveRSSI()) {
50+
char val[4];
51+
snprintf(val, sizeof(val), "%d", getRSSI());
52+
res += ", rssi: ";
53+
res += val;
54+
}
55+
return res;
56+
} // toString
57+
58+
59+
void BTAdvertisedDeviceSet::setAddress(BTAddress address) {
60+
m_address = address;
61+
}
62+
63+
void BTAdvertisedDeviceSet::setCOD(uint32_t cod) {
64+
m_cod = cod;
65+
m_haveCOD = true;
66+
}
67+
68+
void BTAdvertisedDeviceSet::setName(std::string name) {
69+
m_name = name;
70+
m_haveName = true;
71+
}
72+
73+
void BTAdvertisedDeviceSet::setRSSI(int8_t rssi) {
74+
m_rssi = rssi;
75+
m_haveRSSI = true;
76+
}
77+
78+
#endif /* CONFIG_BT_ENABLED */
+42Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* BTScan.h
3+
*
4+
* Created on: Feb 5, 2021
5+
* Author: Thomas M. (ArcticSnowSky)
6+
*/
7+
8+
#ifndef __BTSCAN_H__
9+
#define __BTSCAN_H__
10+
11+
#include <map>
12+
#include <string>
13+
#include <Print.h>
14+
#include "BTAddress.h"
15+
#include "BTAdvertisedDevice.h"
16+
17+
class BTAdvertisedDevice;
18+
class BTAdvertisedDeviceSet;
19+
20+
21+
class BTScanResults {
22+
public:
23+
virtual ~BTScanResults() = default;
24+
25+
virtual void dump(Print *print = nullptr);
26+
virtual int getCount();
27+
virtual BTAdvertisedDevice* getDevice(uint32_t i);
28+
};
29+
30+
class BTScanResultsSet : public BTScanResults {
31+
public:
32+
void dump(Print *print = nullptr);
33+
int getCount();
34+
BTAdvertisedDevice* getDevice(uint32_t i);
35+
36+
bool add(BTAdvertisedDeviceSet advertisedDevice, bool unique = true);
37+
void clear();
38+
39+
std::map<std::string, BTAdvertisedDeviceSet> m_vectorAdvertisedDevices;
40+
};
41+
42+
#endif

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.