forked from blynkkk/blynk-library
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBlynkSimpleEsp32_BT.h
239 lines (192 loc) · 5.53 KB
/
BlynkSimpleEsp32_BT.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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
#ifndef BlynkSimpleEsp32_BT_h
#define BlynkSimpleEsp32_BT_h
#ifndef BLYNK_INFO_CONNECTION
#define BLYNK_INFO_CONNECTION "ESP32_BT"
#endif
#define BLYNK_SEND_ATOMIC
#define BLYNK_SEND_CHUNK 40
#include "sdkconfig.h"
#if defined(CONFIG_BT_ENABLED) && defined(CONFIG_BLUEDROID_ENABLED)
#ifdef ARDUINO_ARCH_ESP32
#include "esp32-hal-log.h"
#endif
#include "esp_bt.h"
#include "esp_bt_main.h"
#include "esp_gap_bt_api.h"
#include "esp_bt_device.h"
#include "esp_spp_api.h"
#include <BlynkApiArduino.h>
#include <Blynk/BlynkProtocol.h>
#include <utility/BlynkFifo.h>
class BlynkTransportEsp32_BT
{
public:
BlynkTransportEsp32_BT()
: 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() {
instance = this;
if (!btStarted() && !btStart()) {
BLYNK_LOG1(BLYNK_F("btStart failed"));
return;
}
esp_bluedroid_status_t bt_state = esp_bluedroid_get_status();
if (bt_state == ESP_BLUEDROID_STATUS_UNINITIALIZED) {
if (esp_bluedroid_init()) {
BLYNK_LOG1(BLYNK_F("esp_bluedroid_init failed"));
return;
}
}
if (bt_state != ESP_BLUEDROID_STATUS_ENABLED) {
if (esp_bluedroid_enable()) {
BLYNK_LOG1(BLYNK_F("esp_bluedroid_enable failed"));
return;
}
}
if (esp_spp_register_callback(esp_spp_cb) != ESP_OK) {
BLYNK_LOG1(BLYNK_F("esp_spp_register_callback failed"));
return;
}
if (esp_spp_init(ESP_SPP_MODE_CB) != ESP_OK) {
BLYNK_LOG1(BLYNK_F("esp_spp_init failed"));
return;
}
if (esp_bredr_tx_power_set(ESP_PWR_LVL_N2, ESP_PWR_LVL_P7) != ESP_OK)
{
BLYNK_LOG1(BLYNK_F("esp_bredr_tx_power_set failed"));
};
if (esp_bt_dev_set_device_name(mName) != ESP_OK)
{
BLYNK_LOG1(BLYNK_F("esp_bt_dev_set_device_name failed"));
}
}
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) {
if (!spp_handle) {
return 0;
}
esp_err_t err = esp_spp_write(spp_handle, len, (uint8_t *)buf);
return (err == ESP_OK) ? len : 0;
}
size_t available() {
size_t rxSize = mBuffRX.size();
return rxSize;
}
static
void putData(uint8_t* data, uint16_t len) {
if (instance)
{
// BLYNK_DBG_DUMP(">> ", data, len);
instance->mBuffRX.put(data, len);
}
}
private:
static BlynkTransportEsp32_BT* instance;
static uint32_t spp_handle;
static void onConnect();
static void onDisconnect();
bool mConn;
const char* mName;
BlynkFifo<uint8_t, BLYNK_MAX_READBYTES * 2> mBuffRX;
static void esp_spp_cb(esp_spp_cb_event_t event, esp_spp_cb_param_t *param)
{
switch (event)
{
case ESP_SPP_INIT_EVT: // Once the SPP callback has been registered, ESP_SPP_INIT_EVT is triggered
#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(4, 0, 0)
esp_bt_gap_set_scan_mode(ESP_BT_CONNECTABLE, ESP_BT_GENERAL_DISCOVERABLE);
#else
esp_bt_gap_set_scan_mode(ESP_BT_SCAN_MODE_CONNECTABLE_DISCOVERABLE);
#endif
esp_spp_start_srv(ESP_SPP_SEC_NONE, ESP_SPP_ROLE_SLAVE, 0, "SPP_SERVER");
break;
case ESP_SPP_CLOSE_EVT:// After the SPP disconnection, ESP_SPP_CLOSE_EVT is triggered.
spp_handle = 0;
onDisconnect();
break;
case ESP_SPP_DATA_IND_EVT:// Data received
if (param->data_ind.len > 0)
{
instance->putData((uint8_t*)param->data_ind.data, param->data_ind.len);
}
else
{
BLYNK_LOG1(BLYNK_F("ESP_SPP_DATA_IND_EVT ERROR"));
}
break;
case ESP_SPP_CONG_EVT: // SPP connection congestion status changed
BLYNK_LOG1(BLYNK_F("ESP_SPP_CONG_EVT"));
break;
case ESP_SPP_SRV_OPEN_EVT://Server connection open
spp_handle = param->open.handle;
onConnect();
break;
default:
break;
}
}
};
class BlynkEsp32_BT
: public BlynkProtocol<BlynkTransportEsp32_BT>
{
typedef BlynkProtocol<BlynkTransportEsp32_BT> Base;
public:
BlynkEsp32_BT(BlynkTransportEsp32_BT& transp)
: Base(transp)
{}
void begin(const char* auth)
{
Base::begin(auth);
state = DISCONNECTED;
conn.begin();
}
void setDeviceName(const char* name) {
conn.setDeviceName(name);
}
};
BlynkTransportEsp32_BT* BlynkTransportEsp32_BT::instance = NULL;
uint32_t BlynkTransportEsp32_BT::spp_handle = 0;
#if !defined(NO_GLOBAL_INSTANCES) && !defined(NO_GLOBAL_BLYNK)
static BlynkTransportEsp32_BT _blynkTransport;
BlynkEsp32_BT Blynk(_blynkTransport);
#else
extern BlynkEsp32_BT Blynk;
#endif
void BlynkTransportEsp32_BT::onConnect() {
BLYNK_LOG1(BLYNK_F("BT connect"));
Blynk.startSession();
};
void BlynkTransportEsp32_BT::onDisconnect() {
BLYNK_LOG1(BLYNK_F("BT disconnect"));
Blynk.disconnect();
}
#include <BlynkWidgets.h>
#endif
#endif