-
Notifications
You must be signed in to change notification settings - Fork 7.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[2.0.0] BtClassic Discovery with info without connect (#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
- Loading branch information
1 parent
223acb3
commit 41c372c
Showing
12 changed files
with
609 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
Empty file.
52 changes: 52 additions & 0 deletions
52
...ries/BluetoothSerial/examples/bt_classic_device_discovery/bt_classic_device_discovery.ino
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
#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 | ||
|
||
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
/* | ||
* BTAddress.cpp | ||
* | ||
* Created on: Jul 2, 2017 | ||
* Author: kolban | ||
* Ported on: Feb 5, 2021 | ||
* Author: Thomas M. (ArcticSnowSky) | ||
*/ | ||
#include "sdkconfig.h" | ||
#if defined(CONFIG_BT_ENABLED) | ||
|
||
#include "BTAddress.h" | ||
#include <string> | ||
#include <sstream> | ||
#include <iomanip> | ||
#include <string.h> | ||
#include <stdio.h> | ||
#include <malloc.h> | ||
#ifdef ARDUINO_ARCH_ESP32 | ||
#include "esp32-hal-log.h" | ||
#endif | ||
|
||
|
||
/** | ||
* @brief Create an address from the native ESP32 representation. | ||
* @param [in] address The native representation. | ||
*/ | ||
BTAddress::BTAddress(esp_bd_addr_t address) { | ||
memcpy(m_address, address, ESP_BD_ADDR_LEN); | ||
} // BTAddress | ||
|
||
|
||
/** | ||
* @brief Create an address from a hex string | ||
* | ||
* A hex string is of the format: | ||
* ``` | ||
* 00:00:00:00:00:00 | ||
* ``` | ||
* which is 17 characters in length. | ||
* | ||
* @param [in] stringAddress The hex representation of the address. | ||
*/ | ||
BTAddress::BTAddress(std::string stringAddress) { | ||
if (stringAddress.length() != 17) return; | ||
|
||
int data[6]; | ||
sscanf(stringAddress.c_str(), "%x:%x:%x:%x:%x:%x", &data[0], &data[1], &data[2], &data[3], &data[4], &data[5]); | ||
m_address[0] = (uint8_t) data[0]; | ||
m_address[1] = (uint8_t) data[1]; | ||
m_address[2] = (uint8_t) data[2]; | ||
m_address[3] = (uint8_t) data[3]; | ||
m_address[4] = (uint8_t) data[4]; | ||
m_address[5] = (uint8_t) data[5]; | ||
} // BTAddress | ||
|
||
|
||
/** | ||
* @brief Determine if this address equals another. | ||
* @param [in] otherAddress The other address to compare against. | ||
* @return True if the addresses are equal. | ||
*/ | ||
bool BTAddress::equals(BTAddress otherAddress) { | ||
return memcmp(otherAddress.getNative(), m_address, 6) == 0; | ||
} // equals | ||
|
||
|
||
/** | ||
* @brief Return the native representation of the address. | ||
* @return The native representation of the address. | ||
*/ | ||
esp_bd_addr_t *BTAddress::getNative() { | ||
return &m_address; | ||
} // getNative | ||
|
||
|
||
/** | ||
* @brief Convert a BT address to a string. | ||
* | ||
* A string representation of an address is in the format: | ||
* | ||
* ``` | ||
* xx:xx:xx:xx:xx:xx | ||
* ``` | ||
* | ||
* @return The string representation of the address. | ||
*/ | ||
std::string BTAddress::toString() { | ||
auto size = 18; | ||
char *res = (char*)malloc(size); | ||
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]); | ||
std::string ret(res); | ||
free(res); | ||
return ret; | ||
} // toString | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/* | ||
* BTAddress.h | ||
* | ||
* Created on: Jul 2, 2017 | ||
* Author: kolban | ||
* Ported on: Feb 5, 2021 | ||
* Author: Thomas M. (ArcticSnowSky) | ||
*/ | ||
|
||
#ifndef COMPONENTS_CPP_UTILS_BTADDRESS_H_ | ||
#define COMPONENTS_CPP_UTILS_BTADDRESS_H_ | ||
#include "sdkconfig.h" | ||
#if defined(CONFIG_BT_ENABLED) | ||
#include <esp_gap_bt_api.h> // ESP32 BT | ||
#include <string> | ||
|
||
|
||
/** | ||
* @brief A %BT device address. | ||
* | ||
* Every %BT device has a unique address which can be used to identify it and form connections. | ||
*/ | ||
class BTAddress { | ||
public: | ||
BTAddress(esp_bd_addr_t address); | ||
BTAddress(std::string stringAddress); | ||
bool equals(BTAddress otherAddress); | ||
esp_bd_addr_t* getNative(); | ||
std::string toString(); | ||
|
||
private: | ||
esp_bd_addr_t m_address; | ||
}; | ||
|
||
#endif /* CONFIG_BT_ENABLED */ | ||
#endif /* COMPONENTS_CPP_UTILS_BTADDRESS_H_ */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
/* | ||
* BTAdvertisedDevice.h | ||
* | ||
* Created on: Feb 5, 2021 | ||
* Author: Thomas M. (ArcticSnowSky) | ||
*/ | ||
|
||
#ifndef __BTADVERTISEDDEVICE_H__ | ||
#define __BTADVERTISEDDEVICE_H__ | ||
|
||
#include "BTAddress.h" | ||
|
||
|
||
class BTAdvertisedDevice { | ||
public: | ||
virtual ~BTAdvertisedDevice() = default; | ||
|
||
virtual BTAddress getAddress(); | ||
virtual uint32_t getCOD(); | ||
virtual std::string getName(); | ||
virtual int8_t getRSSI(); | ||
|
||
|
||
virtual bool haveCOD(); | ||
virtual bool haveName(); | ||
virtual bool haveRSSI(); | ||
|
||
virtual std::string toString(); | ||
}; | ||
|
||
class BTAdvertisedDeviceSet : public virtual BTAdvertisedDevice { | ||
public: | ||
BTAdvertisedDeviceSet(); | ||
//~BTAdvertisedDeviceSet() = default; | ||
|
||
|
||
BTAddress getAddress(); | ||
uint32_t getCOD(); | ||
std::string getName(); | ||
int8_t getRSSI(); | ||
|
||
|
||
bool haveCOD(); | ||
bool haveName(); | ||
bool haveRSSI(); | ||
|
||
std::string toString(); | ||
|
||
void setAddress(BTAddress address); | ||
void setCOD(uint32_t cod); | ||
void setName(std::string name); | ||
void setRSSI(int8_t rssi); | ||
|
||
bool m_haveCOD; | ||
bool m_haveName; | ||
bool m_haveRSSI; | ||
|
||
|
||
BTAddress m_address = BTAddress((uint8_t*)"\0\0\0\0\0\0"); | ||
uint32_t m_cod; | ||
std::string m_name; | ||
int8_t m_rssi; | ||
}; | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
/* | ||
* BTAdvertisedDeviceSet.cpp | ||
* | ||
* Created on: Feb 5, 2021 | ||
* Author: Thomas M. (ArcticSnowSky) | ||
*/ | ||
|
||
#include "sdkconfig.h" | ||
#if defined(CONFIG_BT_ENABLED) | ||
|
||
//#include <map> | ||
|
||
#include "BTAdvertisedDevice.h" | ||
//#include "BTScan.h" | ||
|
||
|
||
BTAdvertisedDeviceSet::BTAdvertisedDeviceSet() { | ||
m_cod = 0; | ||
m_name = ""; | ||
m_rssi = 0; | ||
|
||
m_haveCOD = false; | ||
m_haveName = false; | ||
m_haveRSSI = false; | ||
} // BTAdvertisedDeviceSet | ||
|
||
BTAddress BTAdvertisedDeviceSet::getAddress() { return m_address; } | ||
uint32_t BTAdvertisedDeviceSet::getCOD() { return m_cod; } | ||
std::string BTAdvertisedDeviceSet::getName() { return m_name; } | ||
int8_t BTAdvertisedDeviceSet::getRSSI() { return m_rssi; } | ||
|
||
|
||
bool BTAdvertisedDeviceSet::haveCOD() { return m_haveCOD; } | ||
bool BTAdvertisedDeviceSet::haveName() { return m_haveName; } | ||
bool BTAdvertisedDeviceSet::haveRSSI() { return m_haveRSSI; } | ||
|
||
/** | ||
* @brief Create a string representation of this device. | ||
* @return A string representation of this device. | ||
*/ | ||
std::string BTAdvertisedDeviceSet::toString() { | ||
std::string res = "Name: " + getName() + ", Address: " + getAddress().toString(); | ||
if (haveCOD()) { | ||
char val[6]; | ||
snprintf(val, sizeof(val), "%d", getCOD()); | ||
res += ", cod: "; | ||
res += val; | ||
} | ||
if (haveRSSI()) { | ||
char val[4]; | ||
snprintf(val, sizeof(val), "%d", getRSSI()); | ||
res += ", rssi: "; | ||
res += val; | ||
} | ||
return res; | ||
} // toString | ||
|
||
|
||
void BTAdvertisedDeviceSet::setAddress(BTAddress address) { | ||
m_address = address; | ||
} | ||
|
||
void BTAdvertisedDeviceSet::setCOD(uint32_t cod) { | ||
m_cod = cod; | ||
m_haveCOD = true; | ||
} | ||
|
||
void BTAdvertisedDeviceSet::setName(std::string name) { | ||
m_name = name; | ||
m_haveName = true; | ||
} | ||
|
||
void BTAdvertisedDeviceSet::setRSSI(int8_t rssi) { | ||
m_rssi = rssi; | ||
m_haveRSSI = true; | ||
} | ||
|
||
#endif /* CONFIG_BT_ENABLED */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/* | ||
* BTScan.h | ||
* | ||
* Created on: Feb 5, 2021 | ||
* Author: Thomas M. (ArcticSnowSky) | ||
*/ | ||
|
||
#ifndef __BTSCAN_H__ | ||
#define __BTSCAN_H__ | ||
|
||
#include <map> | ||
#include <string> | ||
#include <Print.h> | ||
#include "BTAddress.h" | ||
#include "BTAdvertisedDevice.h" | ||
|
||
class BTAdvertisedDevice; | ||
class BTAdvertisedDeviceSet; | ||
|
||
|
||
class BTScanResults { | ||
public: | ||
virtual ~BTScanResults() = default; | ||
|
||
virtual void dump(Print *print = nullptr); | ||
virtual int getCount(); | ||
virtual BTAdvertisedDevice* getDevice(uint32_t i); | ||
}; | ||
|
||
class BTScanResultsSet : public BTScanResults { | ||
public: | ||
void dump(Print *print = nullptr); | ||
int getCount(); | ||
BTAdvertisedDevice* getDevice(uint32_t i); | ||
|
||
bool add(BTAdvertisedDeviceSet advertisedDevice, bool unique = true); | ||
void clear(); | ||
|
||
std::map<std::string, BTAdvertisedDeviceSet> m_vectorAdvertisedDevices; | ||
}; | ||
|
||
#endif |
Oops, something went wrong.