Files
firmware/RF/BluetoothDispatcher.hpp

42 lines
1.3 KiB
C++

#include "Arduino.h"
#include "BluetoothSerial.h"
#include "HardwareSerial.h"
#include "Stream.h"
/* Check the ESP configuration */
#if not defined(CONFIG_BT_ENABLED) || not defined(CONFIG_BLUEDROID_ENABLED)
#error Bluetooth is not enabled! Please run `make menuconfig` to and enable it
#endif
#if not defined(CONFIG_BT_SPP_ENABLED)
#error Serial Port Profile for Bluetooth is not available or not enabled. It is only available for the ESP32 chip.
#endif
#if not defined(CONFIG_BT_SSP_ENABLED)
#error Simple Secure Pairing for Bluetooth is not available or not enabled.
#endif
/* ************************* */
typedef std::function<void(esp_bd_addr_t bt_addr)> DeviceConnectedCb;
class BluetoothDispatcher {
public:
BluetoothDispatcher(BluetoothSerial *controller, const char *device_name = "Helicopter", HardwareSerial *serial = &Serial);
bool initialize();
void tick();
~BluetoothDispatcher();
private:
void _confirmRequestCallback(uint16_t pin);
void _authCompleteCallback(boolean success);
void _deviceConnectedCallback(esp_bd_addr_t bt_addr);
const char *_device_name = nullptr;
bool _confirmRequestDone = false;
BluetoothSerial *_controller = nullptr;
HardwareSerial *_serial = nullptr;
static constexpr int _buffer_size = 256;
String _buffer;
};