forked from benkuper/FlowtoysConnectBridge
-
Notifications
You must be signed in to change notification settings - Fork 2
/
BLEManager.h
113 lines (86 loc) · 3.1 KB
/
BLEManager.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#include <BLEDevice.h>
#include <BLEServer.h>
#include <BLEUtils.h>
#include <BLE2902.h>
#define SERVICE_UUID "6E400001-B5A3-F393-E0A9-E50E24DCCA9E" // UART service UUID
#define CHARACTERISTIC_UUID_RX "6E400002-B5A3-F393-E0A9-E50E24DCCA9E"
#define CHARACTERISTIC_UUID_TX "6E400003-B5A3-F393-E0A9-E50E24DCCA9E"
class BLEManager
{
public:
class ServerCallback: public BLEServerCallbacks {
public:
ServerCallback(BLEManager * manager) : manager(manager) {}
BLEManager * manager;
void onConnect(BLEServer* pServer) {
manager->deviceConnected = true;
DBG("BLE is connected.");
};
void onDisconnect(BLEServer* pServer) {
manager->deviceConnected = false;
DBG("BLE is disconnected.");
}
};
class DataCallback: public BLECharacteristicCallbacks {
void onWrite(BLECharacteristic *pCharacteristic) {
String message = String(pCharacteristic->getValue().c_str());
DBG("[BLE] Received " + message);
SerialManager::instance->parseMessage(message);
}
};
BLEManager() {}
BLEServer *pServer = NULL;
BLECharacteristic * pTxCharacteristic;
bool deviceConnected = false;
bool oldDeviceConnected = false;
uint8_t txValue = 0;
bool isActivated;
void init()
{
// Create the BLE Device
isActivated = Config::instance->getBLEMode();
if(!isActivated)
{
DBG("BLE not activated in preferences, not initializing");
return;
}
String bleName ="FlowConnect "+Config::instance->getDeviceName();
BLEDevice::init(bleName.c_str());
// Create the BLE Server
pServer = BLEDevice::createServer();
pServer->setCallbacks(new ServerCallback(this));
// Create the BLE Service
BLEService *pService = pServer->createService(SERVICE_UUID);
// Create a BLE Characteristic
pTxCharacteristic = pService->createCharacteristic(
CHARACTERISTIC_UUID_TX,
BLECharacteristic::PROPERTY_NOTIFY
);
pTxCharacteristic->addDescriptor(new BLE2902());
BLECharacteristic * pRxCharacteristic = pService->createCharacteristic(
CHARACTERISTIC_UUID_RX,
BLECharacteristic::PROPERTY_WRITE
);
pRxCharacteristic->setCallbacks(new DataCallback());
// Start the service
pService->start();
// Start advertising
pServer->getAdvertising()->start();
DBG("BLE is init with name "+bleName);
}
void update()
{
if(!isActivated) return;
if (!deviceConnected && oldDeviceConnected) {
delay(500); // give the bluetooth stack the chance to get things ready
pServer->startAdvertising(); // restart advertising
Serial.println("start advertising");
oldDeviceConnected = deviceConnected;
}
// connecting
if (deviceConnected && !oldDeviceConnected) {
// do stuff here on connecting
oldDeviceConnected = deviceConnected;
}
}
};