2020-10-13 17:01:29 +02:00
|
|
|
#ifndef BLUETOOTH_LE_UART_SERVER
|
|
|
|
#define BLUETOOTH_LE_UART_SERVER
|
|
|
|
|
|
|
|
#include <BLEDevice.h>
|
|
|
|
#include <BLEServer.h>
|
|
|
|
#include <BLEUtils.h>
|
|
|
|
#include <BLE2902.h>
|
|
|
|
#include <Arduino.h>
|
|
|
|
|
|
|
|
class BluetoothLeUartServerCallbacks;
|
|
|
|
|
2020-10-14 23:54:54 +02:00
|
|
|
/*!
|
|
|
|
* \brief The BluetoothLeUartServer class can be used to connect to another Bluetooth LE device (like an Android phone) effordlessly.
|
|
|
|
*
|
|
|
|
* Example use
|
|
|
|
* \code
|
|
|
|
* #include <Arduino.h>
|
|
|
|
* #include "OmobiLedDisplay.h"
|
|
|
|
*
|
|
|
|
* BluetoothLeUartServer* server;
|
|
|
|
*
|
|
|
|
* class MyCallbacks : public BluetoothLeUartServerCallbacks
|
|
|
|
* {
|
|
|
|
* virtual void onDeviceConnectedChanged(bool deviceConnected)
|
|
|
|
* {
|
|
|
|
* Serial.println("Device connected changed");
|
|
|
|
* };
|
|
|
|
* virtual void onDataReceived(String data)
|
|
|
|
* {
|
|
|
|
* Serial.println("Got some data: " + data);
|
|
|
|
* };
|
|
|
|
* };
|
|
|
|
*
|
|
|
|
* void setup()
|
|
|
|
* {
|
|
|
|
* server = new BluetoothLeUartServer("OmobiLedDisplay1", "6e400001-b5a3-f393-e0a9-e50e24dcca9e", "6e400002-b5a3-f393-e0a9-e50e24dcca9e", "6e400003-b5a3-f393-e0a9-e50e24dcca9e");
|
|
|
|
* server->setCallbacks(new MyCallbacks());
|
|
|
|
* }
|
|
|
|
*
|
|
|
|
* void loop()
|
|
|
|
* {
|
|
|
|
* if(server->getDeviceConnected())
|
|
|
|
* server->sendData("PING");
|
|
|
|
* delay(1000);
|
|
|
|
* }
|
|
|
|
* \endcode
|
|
|
|
*/
|
2020-10-13 17:01:29 +02:00
|
|
|
class BluetoothLeUartServer : protected BLEServerCallbacks, protected BLECharacteristicCallbacks
|
|
|
|
{
|
|
|
|
|
|
|
|
public:
|
|
|
|
explicit BluetoothLeUartServer(String deviceName, const char uartServiceUUID[36], const char rxUUID[36], const char txUUID[36]);
|
|
|
|
|
|
|
|
// befriend for callbacks
|
|
|
|
friend class BLEServer;
|
|
|
|
friend class BLECharacteristic;
|
|
|
|
|
|
|
|
// public functions
|
|
|
|
void setCallbacks(BluetoothLeUartServerCallbacks *callbacks);
|
|
|
|
void sendData(String data);
|
|
|
|
|
|
|
|
bool getDeviceConnected();
|
2020-10-15 16:58:06 +02:00
|
|
|
bool disconnectCurrentDevice();
|
2020-10-13 17:01:29 +02:00
|
|
|
|
2020-10-17 01:08:43 +02:00
|
|
|
String getDeviceAddress();
|
|
|
|
|
2020-10-13 17:01:29 +02:00
|
|
|
protected:
|
|
|
|
// callbacks for BLEServer
|
2020-10-15 16:58:06 +02:00
|
|
|
void onConnect(BLEServer *pServer, esp_ble_gatts_cb_param_t *param) override;
|
2020-10-13 17:01:29 +02:00
|
|
|
void onDisconnect(BLEServer *pServer) override;
|
|
|
|
|
|
|
|
// callback for BLECharacteristic
|
|
|
|
void onWrite(BLECharacteristic *rxCharacteristic) override;
|
|
|
|
|
|
|
|
private:
|
2020-10-14 23:54:54 +02:00
|
|
|
// service and characteristic UUIDs
|
2020-10-13 17:01:29 +02:00
|
|
|
const char *uartServiceUUID;
|
|
|
|
const char *rxUUID;
|
|
|
|
const char *txUUID;
|
|
|
|
|
2020-10-15 16:58:06 +02:00
|
|
|
|
|
|
|
esp_gatt_perm_t permissions;
|
|
|
|
|
2020-10-14 23:54:54 +02:00
|
|
|
// BLE Objects
|
2020-10-13 17:01:29 +02:00
|
|
|
BLEServer *bleServer;
|
|
|
|
BLEService *bleService;
|
|
|
|
BLECharacteristic *txCharacteristic;
|
|
|
|
BLECharacteristic *rxCharacteristic;
|
2020-10-14 23:54:54 +02:00
|
|
|
|
|
|
|
// helpers
|
2020-10-15 16:58:06 +02:00
|
|
|
bool deviceConnected;
|
|
|
|
uint16_t deviceConnectionId;
|
2020-10-13 17:01:29 +02:00
|
|
|
BluetoothLeUartServerCallbacks *callbacks;
|
|
|
|
};
|
|
|
|
|
|
|
|
class BluetoothLeUartServerCallbacks
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
virtual void onDeviceConnectedChanged(bool deviceConnected);
|
|
|
|
virtual void onDataReceived(String data);
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif // BLUETOOTH_LE_UART_SERVER
|