forked from blynkkk/blynk-library
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBlynkSimpleEsp32_BLE.h
202 lines (157 loc) · 4.73 KB
/
BlynkSimpleEsp32_BLE.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/**
* @file BlynkSimpleEsp32_BLE.h
* @author Volodymyr Shymanskyy
* @license This project is released under the MIT License (MIT)
* @copyright Copyright (c) 2015 Volodymyr Shymanskyy
* @date Nov 2017
* @brief
*
*/
#ifndef BlynkSimpleEsp32_BLE_h
#define BlynkSimpleEsp32_BLE_h
#ifndef BLYNK_INFO_CONNECTION
#define BLYNK_INFO_CONNECTION "Esp32_BLE"
#endif
#define BLYNK_SEND_ATOMIC
#define BLYNK_SEND_CHUNK 20
//#define BLYNK_SEND_THROTTLE 20
#include <BlynkApiArduino.h>
#include <Blynk/BlynkProtocol.h>
#include <utility/BlynkFifo.h>
#include <BLEDevice.h>
#include <BLEServer.h>
#include <BLEUtils.h>
#include <BLE2902.h>
#define SERVICE_UUID "713D0000-503E-4C75-BA94-3148F18D941E"
#define CHARACTERISTIC_UUID_RX "713D0003-503E-4C75-BA94-3148F18D941E"
#define CHARACTERISTIC_UUID_TX "713D0002-503E-4C75-BA94-3148F18D941E"
class BlynkTransportEsp32_BLE :
public BLEServerCallbacks,
public BLECharacteristicCallbacks
{
public:
BlynkTransportEsp32_BLE()
: mConn (false)
, mName ("Blynk")
{}
void setDeviceName(const char* name) {
mName = name;
}
// IP redirect not available
void begin(char BLYNK_UNUSED *h, uint16_t BLYNK_UNUSED p) {}
void begin() {
// Create the BLE Device
BLEDevice::init(mName);
// Create the BLE Server
pServer = BLEDevice::createServer();
pServer->setCallbacks(this);
// Create the BLE Service
pService = pServer->createService(SERVICE_UUID);
// Create a BLE Characteristic
pCharacteristicTX = pService->createCharacteristic(
CHARACTERISTIC_UUID_TX,
BLECharacteristic::PROPERTY_NOTIFY
);
pCharacteristicTX->addDescriptor(new BLE2902());
pCharacteristicRX = pService->createCharacteristic(
CHARACTERISTIC_UUID_RX,
BLECharacteristic::PROPERTY_WRITE
);
pCharacteristicRX->setCallbacks(this);
// Start the service
pService->start();
// Start advertising
pServer->getAdvertising()->addServiceUUID(pService->getUUID());
pServer->getAdvertising()->start();
}
bool connect() {
mBuffRX.clear();
return mConn = true;
}
void disconnect() {
mConn = false;
}
bool connected() {
return mConn;
}
size_t read(void* buf, size_t len) {
millis_time_t start = BlynkMillis();
while (BlynkMillis() - start < BLYNK_TIMEOUT_MS) {
if (available() < len) {
delay(1);
} else {
break;
}
}
size_t res = mBuffRX.get((uint8_t*)buf, len);
return res;
}
size_t write(const void* buf, size_t len) {
pCharacteristicTX->setValue((uint8_t*)buf, len);
pCharacteristicTX->notify();
return len;
}
size_t available() {
size_t rxSize = mBuffRX.size();
return rxSize;
}
private:
void onConnect(BLEServer* pServer);
void onDisconnect(BLEServer* pServer);
void onWrite(BLECharacteristic *pCharacteristic) {
std::string rxValue = pCharacteristic->getValue();
if (rxValue.length() > 0) {
uint8_t* data = (uint8_t*)rxValue.data();
size_t len = rxValue.length();
BLYNK_DBG_DUMP(">> ", data, len);
mBuffRX.put(data, len);
}
}
private:
bool mConn;
const char* mName;
BLEServer *pServer;
BLEService *pService;
BLECharacteristic *pCharacteristicTX;
BLECharacteristic *pCharacteristicRX;
BlynkFifo<uint8_t, BLYNK_MAX_READBYTES*2> mBuffRX;
};
class BlynkEsp32_BLE
: public BlynkProtocol<BlynkTransportEsp32_BLE>
{
typedef BlynkProtocol<BlynkTransportEsp32_BLE> Base;
public:
BlynkEsp32_BLE(BlynkTransportEsp32_BLE& transp)
: Base(transp)
{}
void begin(const char* auth)
{
Base::begin(auth);
state = DISCONNECTED;
conn.begin();
}
void setDeviceName(const char* name) {
conn.setDeviceName(name);
}
};
#if !defined(NO_GLOBAL_INSTANCES) && !defined(NO_GLOBAL_BLYNK)
static BlynkTransportEsp32_BLE _blynkTransportBLE;
BlynkEsp32_BLE Blynk(_blynkTransportBLE);
#else
extern BlynkEsp32_BLE Blynk;
#endif
inline
void BlynkTransportEsp32_BLE::onConnect(BLEServer* pServer) {
BLYNK_LOG1(BLYNK_F("BLE connect"));
connect();
Blynk.startSession();
};
inline
void BlynkTransportEsp32_BLE::onDisconnect(BLEServer* pServer) {
BLYNK_LOG1(BLYNK_F("BLE disconnect"));
pServer->getAdvertising()->start();
Blynk.disconnect();
disconnect();
}
#include <BlynkWidgets.h>
#endif