diff --git a/vscode/OmobiLEDdisplayBluetooth/include/BluetoothLeUartServer.h b/vscode/OmobiLEDdisplayBluetooth/include/BluetoothLeUartServer.h index 4a3b330..5421e4c 100644 --- a/vscode/OmobiLEDdisplayBluetooth/include/BluetoothLeUartServer.h +++ b/vscode/OmobiLEDdisplayBluetooth/include/BluetoothLeUartServer.h @@ -60,10 +60,11 @@ public: void sendData(String data); bool getDeviceConnected(); + bool disconnectCurrentDevice(); protected: // callbacks for BLEServer - void onConnect(BLEServer *pServer) override; + void onConnect(BLEServer *pServer, esp_ble_gatts_cb_param_t *param) override; void onDisconnect(BLEServer *pServer) override; // callback for BLECharacteristic @@ -75,6 +76,9 @@ private: const char *rxUUID; const char *txUUID; + + esp_gatt_perm_t permissions; + // BLE Objects BLEServer *bleServer; BLEService *bleService; @@ -82,7 +86,8 @@ private: BLECharacteristic *rxCharacteristic; // helpers - bool deviceConnected = false; + bool deviceConnected; + uint16_t deviceConnectionId; BluetoothLeUartServerCallbacks *callbacks; }; diff --git a/vscode/OmobiLEDdisplayBluetooth/include/OmobiLedDisplay.h b/vscode/OmobiLEDdisplayBluetooth/include/OmobiLedDisplay.h index 9e897ea..7c4f24b 100644 --- a/vscode/OmobiLEDdisplayBluetooth/include/OmobiLedDisplay.h +++ b/vscode/OmobiLEDdisplayBluetooth/include/OmobiLedDisplay.h @@ -15,6 +15,8 @@ public: // befriend for callbacks friend class BluetoothLeUartServer; + void loop(); + protected: // calbacks for BluetoothLeUartServerCallbacks void onDeviceConnectedChanged(bool deviceConnected) override; @@ -23,6 +25,8 @@ protected: private: enum OmobiDisplayCommand { + AuthorizeSessionCommand = 0, + KeepAliveCommand = 1, GetAllTextSetsCommand = 10, GetTextSetParameterCommand = 11, GetDisplayBrightnessCommand = 12, @@ -37,6 +41,9 @@ private: DisplayControllerError = 501 }; + unsigned long lastKeepAlive; + const int maximumKeepAliveDelay = 10000; + LedDisplayController *ledDisplayController; BluetoothLeUartServer *bleServer; }; diff --git a/vscode/OmobiLEDdisplayBluetooth/src/BluetoothLeUartServer.cpp b/vscode/OmobiLEDdisplayBluetooth/src/BluetoothLeUartServer.cpp index f384f36..0b1a5c3 100644 --- a/vscode/OmobiLEDdisplayBluetooth/src/BluetoothLeUartServer.cpp +++ b/vscode/OmobiLEDdisplayBluetooth/src/BluetoothLeUartServer.cpp @@ -7,6 +7,8 @@ BluetoothLeUartServer::BluetoothLeUartServer(String deviceName, const char uartS this->txUUID = txUUID; this->callbacks = nullptr; + this->deviceConnected = false; + this->deviceConnectionId = -1; // Create the BLE Device BLEDevice::init(deviceName.c_str()); // Give it a name @@ -54,9 +56,11 @@ void BluetoothLeUartServer::sendData(String data) txCharacteristic->notify(); } -void BluetoothLeUartServer::onConnect(BLEServer *pServer) +void BluetoothLeUartServer::onConnect(BLEServer* pServer, esp_ble_gatts_cb_param_t *param) { this->deviceConnected = true; + this->deviceConnectionId = param->connect.conn_id; + if (this->callbacks != nullptr) { this->callbacks->onDeviceConnectedChanged(this->deviceConnected); @@ -66,6 +70,8 @@ void BluetoothLeUartServer::onConnect(BLEServer *pServer) void BluetoothLeUartServer::onDisconnect(BLEServer *pServer) { this->deviceConnected = false; + this->deviceConnectionId = -1; + if (this->callbacks != nullptr) this->callbacks->onDeviceConnectedChanged(this->deviceConnected); } @@ -79,4 +85,12 @@ void BluetoothLeUartServer::onWrite(BLECharacteristic *rxCharacteristic) bool BluetoothLeUartServer::getDeviceConnected() { return this->deviceConnected; +} + +bool BluetoothLeUartServer::disconnectCurrentDevice() { + if(!this->getDeviceConnected()) + return false; + + this->bleServer->disconnect(this->deviceConnectionId); + return true; } \ No newline at end of file diff --git a/vscode/OmobiLEDdisplayBluetooth/src/OmobiLedDisplay.cpp b/vscode/OmobiLEDdisplayBluetooth/src/OmobiLedDisplay.cpp index d4aa2fa..979dbee 100644 --- a/vscode/OmobiLEDdisplayBluetooth/src/OmobiLedDisplay.cpp +++ b/vscode/OmobiLEDdisplayBluetooth/src/OmobiLedDisplay.cpp @@ -1,22 +1,34 @@ #include "OmobiLedDisplay.h" +#include "esp_gatts_api.h" OmobiLedDisplay::OmobiLedDisplay(String deviceName, Adafruit_NeoMatrix *ledDisplayMatrix) { + this->lastKeepAlive = -1; + // init led display controller this->ledDisplayController = new LedDisplayController(ledDisplayMatrix); this->ledDisplayController->setTextSetParameter(0, LedDisplayController::ActiveParameter, "true"); // init ble server - this->bleServer = new BluetoothLeUartServer(deviceName, "6e400001-b5a3-f393-e0a9-e50e24dcca9e", "6e400002-b5a3-f393-e0a9-e50e24dcca9e", "6e400003-b5a3-f393-e0a9-e50e24dcca9e"); + this->bleServer = new BluetoothLeUartServer(deviceName, "92fecb20-1406-426a-afa5-cd5c1f306462", "92fecb21-1406-426a-afa5-cd5c1f306462", "92fecb22-1406-426a-afa5-cd5c1f306462"); this->bleServer->setCallbacks(this); } +void OmobiLedDisplay::loop() { + if(millis() - lastKeepAlive > this->maximumKeepAliveDelay) + this->bleServer->disconnectCurrentDevice(); +} + void OmobiLedDisplay::onDeviceConnectedChanged(bool deviceConnected) { - if (deviceConnected) + if (deviceConnected) { Serial.println("Device connected"); - else + this->lastKeepAlive = millis(); + } + else { Serial.println("Device disconnected"); + this->lastKeepAlive = -1; + } } void OmobiLedDisplay::onDataReceived(String dataString) @@ -42,6 +54,13 @@ void OmobiLedDisplay::onDataReceived(String dataString) switch (requestHeader) { + case KeepAliveCommand: + { + replyStatus = Success; + this->lastKeepAlive = millis(); + break; + } + case GetAllTextSetsCommand: { // cycle through all text sets diff --git a/vscode/OmobiLEDdisplayBluetooth/src/main.cpp b/vscode/OmobiLEDdisplayBluetooth/src/main.cpp index 93c673b..ba685d7 100644 --- a/vscode/OmobiLEDdisplayBluetooth/src/main.cpp +++ b/vscode/OmobiLEDdisplayBluetooth/src/main.cpp @@ -27,5 +27,6 @@ void setup() void loop() { // nothing to do in loop - delay(1000); + display->loop(); + delay(1); } \ No newline at end of file