Template
Add new bluetooth lib code
This commit is contained in:
@@ -0,0 +1,88 @@
|
||||
#include "sdkconfig.h"
|
||||
#if defined(CONFIG_BT_ENABLED) && defined(CONFIG_BLUEDROID_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
|
||||
@@ -0,0 +1,27 @@
|
||||
#ifndef COMPONENTS_CPP_UTILS_BTADDRESS_H_
|
||||
#define COMPONENTS_CPP_UTILS_BTADDRESS_H_
|
||||
#include "sdkconfig.h"
|
||||
#if defined(CONFIG_BT_ENABLED) && defined(CONFIG_BLUEDROID_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_ */
|
||||
@@ -0,0 +1,58 @@
|
||||
#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
|
||||
@@ -0,0 +1,71 @@
|
||||
#include "sdkconfig.h"
|
||||
#if defined(CONFIG_BT_ENABLED) && defined(CONFIG_BLUEDROID_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[6];
|
||||
snprintf(val, sizeof(val), "%d", (int8_t)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 */
|
||||
@@ -0,0 +1,35 @@
|
||||
#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
|
||||
@@ -0,0 +1,88 @@
|
||||
#include "sdkconfig.h"
|
||||
#if defined(CONFIG_BT_ENABLED) && defined(CONFIG_BLUEDROID_ENABLED)
|
||||
|
||||
|
||||
#include <esp_err.h>
|
||||
|
||||
#include "BTAdvertisedDevice.h"
|
||||
#include "BTScan.h"
|
||||
//#include "GeneralUtils.h"
|
||||
#include "esp32-hal-log.h"
|
||||
|
||||
|
||||
class BTAdvertisedDevice;
|
||||
|
||||
/**
|
||||
* @brief Dump the scan results to the log.
|
||||
*/
|
||||
void BTScanResultsSet::dump(Print *print) {
|
||||
int cnt = getCount();
|
||||
if (print == nullptr) {
|
||||
log_v(">> Dump scan results : %d", cnt);
|
||||
for (int i=0; i < cnt; i++) {
|
||||
BTAdvertisedDevice* dev = getDevice(i);
|
||||
if (dev)
|
||||
log_d("- %d: %s\n", i+1, dev->toString().c_str());
|
||||
else
|
||||
log_d("- %d is null\n", i+1);
|
||||
}
|
||||
log_v("-- dump finished --");
|
||||
} else {
|
||||
print->printf(">> Dump scan results: %d\n", cnt);
|
||||
for (int i=0; i < cnt; i++) {
|
||||
BTAdvertisedDevice* dev = getDevice(i);
|
||||
if (dev)
|
||||
print->printf("- %d: %s\n", i+1, dev->toString().c_str());
|
||||
else
|
||||
print->printf("- %d is null\n", i+1);
|
||||
}
|
||||
print->println("-- Dump finished --");
|
||||
}
|
||||
} // dump
|
||||
|
||||
|
||||
/**
|
||||
* @brief Return the count of devices found in the last scan.
|
||||
* @return The number of devices found in the last scan.
|
||||
*/
|
||||
int BTScanResultsSet::getCount() {
|
||||
return m_vectorAdvertisedDevices.size();
|
||||
} // getCount
|
||||
|
||||
|
||||
/**
|
||||
* @brief Return the specified device at the given index.
|
||||
* The index should be between 0 and getCount()-1.
|
||||
* @param [in] i The index of the device.
|
||||
* @return The device at the specified index.
|
||||
*/
|
||||
BTAdvertisedDevice* BTScanResultsSet::getDevice(uint32_t i) {
|
||||
if (i < 0)
|
||||
return nullptr;
|
||||
|
||||
uint32_t x = 0;
|
||||
BTAdvertisedDeviceSet* pDev = &m_vectorAdvertisedDevices.begin()->second;
|
||||
for (auto it = m_vectorAdvertisedDevices.begin(); it != m_vectorAdvertisedDevices.end(); it++) {
|
||||
pDev = &it->second;
|
||||
if (x==i) break;
|
||||
x++;
|
||||
}
|
||||
return x==i ? pDev : nullptr;
|
||||
}
|
||||
|
||||
void BTScanResultsSet::clear() {
|
||||
//for(auto _dev : m_vectorAdvertisedDevices)
|
||||
// delete _dev.second;
|
||||
m_vectorAdvertisedDevices.clear();
|
||||
}
|
||||
|
||||
bool BTScanResultsSet::add(BTAdvertisedDeviceSet advertisedDevice, bool unique) {
|
||||
std::string key = advertisedDevice.getAddress().toString();
|
||||
if (!unique || m_vectorAdvertisedDevices.count(key) == 0) {
|
||||
m_vectorAdvertisedDevices.insert(std::pair<std::string, BTAdvertisedDeviceSet>(key, advertisedDevice));
|
||||
return true;
|
||||
} else
|
||||
return false;
|
||||
}
|
||||
|
||||
#endif
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,75 @@
|
||||
#ifndef _BLUETOOTH_SERIAL_H_
|
||||
#define _BLUETOOTH_SERIAL_H_
|
||||
|
||||
#include "sdkconfig.h"
|
||||
|
||||
#if defined(CONFIG_BT_ENABLED) && defined(CONFIG_BLUEDROID_ENABLED)
|
||||
|
||||
#include "Arduino.h"
|
||||
#include "Stream.h"
|
||||
#include <esp_gap_bt_api.h>
|
||||
#include <esp_spp_api.h>
|
||||
#include <functional>
|
||||
#include "BTScan.h"
|
||||
|
||||
typedef std::function<void(const uint8_t *buffer, size_t size)> BluetoothSerialDataCb;
|
||||
typedef std::function<void(uint32_t num_val)> ConfirmRequestCb;
|
||||
typedef std::function<void(boolean success)> AuthCompleteCb;
|
||||
typedef std::function<void(BTAdvertisedDevice* pAdvertisedDevice)> BTAdvertisedDeviceCb;
|
||||
|
||||
class BluetoothSerial: public Stream
|
||||
{
|
||||
public:
|
||||
|
||||
BluetoothSerial(void);
|
||||
~BluetoothSerial(void);
|
||||
|
||||
bool begin(String localName=String(), bool isMaster=false, uint16_t rxQueueSize = 512 * 2, uint16_t txQueueSize = 512);
|
||||
bool begin(unsigned long baud){//compatibility
|
||||
return begin();
|
||||
}
|
||||
int available(void);
|
||||
int peek(void);
|
||||
bool hasClient(void);
|
||||
int read(void);
|
||||
size_t write(uint8_t c);
|
||||
size_t write(const uint8_t *buffer, size_t size);
|
||||
void flush();
|
||||
void end(void);
|
||||
void onData(BluetoothSerialDataCb cb);
|
||||
esp_err_t register_callback(esp_spp_cb_t * callback);
|
||||
|
||||
void onConfirmRequest(ConfirmRequestCb cb);
|
||||
void onAuthComplete(AuthCompleteCb cb);
|
||||
void confirmReply(boolean confirm);
|
||||
|
||||
void enableSSP();
|
||||
bool setPin(const char *pin);
|
||||
bool connect(String remoteName);
|
||||
bool connect(uint8_t remoteAddress[]);
|
||||
bool connect();
|
||||
bool connected(int timeout=0);
|
||||
bool isReady(bool checkMaster=false, int timeout=0);
|
||||
bool disconnect();
|
||||
bool unpairDevice(uint8_t remoteAddress[]);
|
||||
|
||||
BTScanResults* discover(int timeout=0x30*1280);
|
||||
bool discoverAsync(BTAdvertisedDeviceCb cb, int timeout=0x30*1280);
|
||||
void discoverAsyncStop();
|
||||
void discoverClear();
|
||||
BTScanResults* getScanResults();
|
||||
|
||||
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;
|
||||
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user