diff --git a/src/BluetoothSerial/BTAdress.h b/src/BluetoothSerial/BTAdress.h new file mode 100644 index 0000000..dbab3bb --- /dev/null +++ b/src/BluetoothSerial/BTAdress.h @@ -0,0 +1,26 @@ +#ifndef COMPONENTS_CPP_UTILS_BTADDRESS_H_ +#define COMPONENTS_CPP_UTILS_BTADDRESS_H_ +#include "sdkconfig.h" +#if defined(CONFIG_BT_ENABLED) +#include // ESP32 BT +#include + +/** + * @brief A %!BT device address + * + * Every %BT device has a unique address which can be used to identify it and from connection + */ +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_ */ \ No newline at end of file diff --git a/src/BluetoothSerial/BTAdvertisedDevice.h b/src/BluetoothSerial/BTAdvertisedDevice.h new file mode 100644 index 0000000..779c770 --- /dev/null +++ b/src/BluetoothSerial/BTAdvertisedDevice.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 \ No newline at end of file diff --git a/src/BluetoothSerial/BTScan.h b/src/BluetoothSerial/BTScan.h new file mode 100644 index 0000000..cc15c8c --- /dev/null +++ b/src/BluetoothSerial/BTScan.h @@ -0,0 +1,35 @@ +#ifndef __BTSCAN_H__ +#define __BTSCAN_H__ + +#include +#include +#include +#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 m_vectorAdvertisedDevices; +}; + +#endif \ No newline at end of file diff --git a/src/BluetoothSerial/BluetoothSerial.h b/src/BluetoothSerial/BluetoothSerial.h new file mode 100644 index 0000000..3bf3b20 --- /dev/null +++ b/src/BluetoothSerial/BluetoothSerial.h @@ -0,0 +1,76 @@ +#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 +#include +#include +#include "BTScan.h" + +typedef std::function BluetoothSerialDataCb; +typedef std::function ConfirmRequestCb; +typedef std::function AuthCompleteCb; +typedef std::function BTAdvertisedDeviceCb; + +class BluetoothSerial: public Stream +{ + public: + + BluetoothSerial(void); + ~BluetoothSerial(void); + + bool begin(String localName=String(), bool isMaster=false, uint16_t rxQueueSize = 512 * 4, 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; + + bool isCongested(); + private: + String local_name; + +}; + +#endif + +#endif \ No newline at end of file