BluetoothDispatcher was added & reformatting
This commit is contained in:
73
RF/BluetoothDispatcher.cpp
Normal file
73
RF/BluetoothDispatcher.cpp
Normal file
@@ -0,0 +1,73 @@
|
||||
#include "BluetoothDispatcher.hpp"
|
||||
|
||||
static DeviceConnectedCb deviceConnectedCallback = nullptr;
|
||||
static void deviceConnectedStaticCallback(esp_spp_cb_event_t event, esp_spp_cb_param_t *param);
|
||||
|
||||
BluetoothDispatcher::BluetoothDispatcher(BluetoothSerial *controller, const char *device_name, HardwareSerial *serial) {
|
||||
_device_name = device_name;
|
||||
_controller = controller;
|
||||
_serial = serial;
|
||||
}
|
||||
|
||||
bool BluetoothDispatcher::initialize() {
|
||||
_controller->enableSSP();
|
||||
_controller->onConfirmRequest([this](uint16_t pin) {
|
||||
_confirmRequestCallback(pin);
|
||||
});
|
||||
_controller->onAuthComplete([this](boolean success) {
|
||||
_authCompleteCallback(success);
|
||||
});
|
||||
_controller->register_callback(deviceConnectedStaticCallback);
|
||||
deviceConnectedCallback = [this](esp_bd_addr_t bt_addr) {
|
||||
_deviceConnectedCallback(bt_addr);
|
||||
};
|
||||
_controller->setTimeout(2);
|
||||
return _controller->begin(_device_name);
|
||||
}
|
||||
|
||||
void BluetoothDispatcher::tick() {
|
||||
while (_controller->available() and _buffer.length() <= _buffer_size) {
|
||||
_buffer += (char)_controller->read();
|
||||
}
|
||||
if (_buffer.endsWith("\r")) {
|
||||
_serial->println(_buffer.substring(0, _buffer.lastIndexOf("\r")));
|
||||
_buffer.clear();
|
||||
_controller->write((uint8_t *)"Hello, world!", strlen("Hello, world!"));
|
||||
}
|
||||
if (_buffer.length() > _buffer_size) {
|
||||
_buffer.clear();
|
||||
}
|
||||
}
|
||||
|
||||
BluetoothDispatcher::~BluetoothDispatcher() {
|
||||
_controller->end();
|
||||
delete _controller;
|
||||
}
|
||||
|
||||
void BluetoothDispatcher::_confirmRequestCallback(uint16_t pin) {
|
||||
_confirmRequestDone = true;
|
||||
_serial->print("The PIN is: ");
|
||||
_serial->println(pin);
|
||||
_controller->confirmReply(true);
|
||||
}
|
||||
|
||||
void BluetoothDispatcher::_authCompleteCallback(boolean success) {
|
||||
if (success) {
|
||||
_confirmRequestDone = true;
|
||||
_serial->println("Pairing success!");
|
||||
} else {
|
||||
_serial->println("Pairing failed, rejected by user!");
|
||||
}
|
||||
}
|
||||
|
||||
void BluetoothDispatcher::_deviceConnectedCallback(esp_bd_addr_t bt_addr) {
|
||||
_serial->print("New connection opened: ");
|
||||
_serial->println(BTAddress(bt_addr).toString(true));
|
||||
}
|
||||
|
||||
|
||||
static void deviceConnectedStaticCallback(esp_spp_cb_event_t event, esp_spp_cb_param_t *param) {
|
||||
if (event == ESP_SPP_SRV_OPEN_EVT and param->srv_open.status == ESP_SPP_SUCCESS and deviceConnectedCallback) {
|
||||
deviceConnectedCallback(param->srv_open.rem_bda);
|
||||
}
|
||||
}
|
||||
42
RF/BluetoothDispatcher.hpp
Normal file
42
RF/BluetoothDispatcher.hpp
Normal file
@@ -0,0 +1,42 @@
|
||||
#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;
|
||||
};
|
||||
Reference in New Issue
Block a user