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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user