30
30
static volatile BTState bluetoothState = BT_OFF;
31
31
32
32
#ifdef COMPILE_BT
33
- BTSerialInterface *bluetoothSerial;
34
33
BTSerialInterface *bluetoothSerialSpp;
35
34
BTSerialInterface *bluetoothSerialBle;
36
35
@@ -46,7 +45,7 @@ void bluetoothCallback(esp_spp_cb_event_t event, esp_spp_cb_param_t *param)
46
45
{
47
46
systemPrintln (" BT client Connected" );
48
47
bluetoothState = BT_CONNECTED;
49
- // LED is controlled by tickerBluetoothLedUpdate()
48
+ // LED is controlled by tickerBluetoothLedUpdate()
50
49
}
51
50
52
51
if (event == ESP_SPP_CLOSE_EVT)
@@ -95,8 +94,10 @@ int bluetoothRead(uint8_t *buffer, int length)
95
94
96
95
return (bytesRead);
97
96
}
98
- else
99
- return bluetoothSerial->readBytes (buffer, length);
97
+ else if (settings.bluetoothRadioType == BLUETOOTH_RADIO_SPP)
98
+ return bluetoothSerialSpp->readBytes (buffer, length);
99
+ else if (settings.bluetoothRadioType == BLUETOOTH_RADIO_BLE)
100
+ return bluetoothSerialBle->readBytes (buffer, length);
100
101
101
102
return 0 ;
102
103
@@ -117,8 +118,12 @@ uint8_t bluetoothRead()
117
118
118
119
return (bluetoothSerialSpp->read ());
119
120
}
120
- else
121
- return bluetoothSerial->read ();
121
+ else if (settings.bluetoothRadioType == BLUETOOTH_RADIO_SPP)
122
+ return bluetoothSerialSpp->read ();
123
+ else if (settings.bluetoothRadioType == BLUETOOTH_RADIO_BLE)
124
+ return bluetoothSerialBle->read ();
125
+
126
+ return 0 ;
122
127
#else // COMPILE_BT
123
128
return 0 ;
124
129
#endif // COMPILE_BT
@@ -136,8 +141,12 @@ bool bluetoothRxDataAvailable()
136
141
137
142
return (bluetoothSerialSpp->available ());
138
143
}
139
- else
140
- return bluetoothSerial->available ();
144
+ else if (settings.bluetoothRadioType == BLUETOOTH_RADIO_SPP)
145
+ return bluetoothSerialSpp->available ();
146
+ else if (settings.bluetoothRadioType == BLUETOOTH_RADIO_BLE)
147
+ return bluetoothSerialBle->available ();
148
+
149
+ return (0 );
141
150
#else // COMPILE_BT
142
151
return false ;
143
152
#endif // COMPILE_BT
@@ -147,8 +156,6 @@ bool bluetoothRxDataAvailable()
147
156
int bluetoothWrite (const uint8_t *buffer, int length)
148
157
{
149
158
#ifdef COMPILE_BT
150
- // BLE write does not handle 0 length requests correctly
151
-
152
159
if (settings.bluetoothRadioType == BLUETOOTH_RADIO_SPP_AND_BLE)
153
160
{
154
161
// Write to both interfaces
@@ -162,13 +169,19 @@ int bluetoothWrite(const uint8_t *buffer, int length)
162
169
return (bleWrite);
163
170
return (sppWrite);
164
171
}
165
- else
172
+ else if (settings.bluetoothRadioType == BLUETOOTH_RADIO_SPP)
173
+ {
174
+ return bluetoothSerialSpp->write (buffer, length);
175
+ }
176
+ else if (settings.bluetoothRadioType == BLUETOOTH_RADIO_BLE)
166
177
{
178
+ // BLE write does not handle 0 length requests correctly
167
179
if (length > 0 )
168
- return bluetoothSerial->write (buffer, length);
169
- else
170
- return 0 ;
180
+ return bluetoothSerialBle->write (buffer, length);
181
+ return 0 ;
171
182
}
183
+
184
+ return (0 );
172
185
#else // COMPILE_BT
173
186
return 0 ;
174
187
#endif // COMPILE_BT
@@ -191,10 +204,16 @@ int bluetoothWrite(uint8_t value)
191
204
return (bleWrite);
192
205
return (sppWrite);
193
206
}
194
- else
207
+ else if (settings. bluetoothRadioType == BLUETOOTH_RADIO_SPP)
195
208
{
196
- return bluetoothSerial ->write (value);
209
+ return bluetoothSerialSpp ->write (value);
197
210
}
211
+ else if (settings.bluetoothRadioType == BLUETOOTH_RADIO_BLE)
212
+ {
213
+ return bluetoothSerialBle->write (value);
214
+ }
215
+
216
+ return (0 );
198
217
#else // COMPILE_BT
199
218
return 0 ;
200
219
#endif // COMPILE_BT
@@ -209,8 +228,10 @@ void bluetoothFlush()
209
228
bluetoothSerialBle->flush ();
210
229
bluetoothSerialSpp->flush ();
211
230
}
212
- else
213
- bluetoothSerial->flush ();
231
+ else if (settings.bluetoothRadioType == BLUETOOTH_RADIO_SPP)
232
+ bluetoothSerialSpp->flush ();
233
+ else if (settings.bluetoothRadioType == BLUETOOTH_RADIO_BLE)
234
+ bluetoothSerialBle->flush ();
214
235
#else // COMPILE_BT
215
236
return ;
216
237
#endif // COMPILE_BT
@@ -221,6 +242,9 @@ void bluetoothFlush()
221
242
// This allows multiple units to be on at same time
222
243
void bluetoothStart ()
223
244
{
245
+ if (settings.bluetoothRadioType == BLUETOOTH_RADIO_OFF)
246
+ return ;
247
+
224
248
#ifdef COMPILE_BT
225
249
if (!online.bluetooth )
226
250
{
@@ -269,9 +293,9 @@ void bluetoothStart()
269
293
bluetoothSerialBle = new BTLESerial ();
270
294
}
271
295
else if (settings.bluetoothRadioType == BLUETOOTH_RADIO_SPP)
272
- bluetoothSerial = new BTClassicSerial ();
296
+ bluetoothSerialSpp = new BTClassicSerial ();
273
297
else if (settings.bluetoothRadioType == BLUETOOTH_RADIO_BLE)
274
- bluetoothSerial = new BTLESerial ();
298
+ bluetoothSerialBle = new BTLESerial ();
275
299
276
300
// Not yet implemented
277
301
// if (pinBluetoothTaskHandle == nullptr)
@@ -290,18 +314,26 @@ void bluetoothStart()
290
314
bool beginSuccess = true ;
291
315
if (settings.bluetoothRadioType == BLUETOOTH_RADIO_SPP_AND_BLE)
292
316
{
293
- beginSuccess &=
294
- bluetoothSerialBle-> begin ( deviceName, false , false , settings.sppRxQueueSize ,
295
- settings.sppTxQueueSize ); // localName, isMaster, disableBLE, rxBufferSize, txBufferSize
296
- beginSuccess &=
297
- bluetoothSerialSpp-> begin ( deviceName, false , false , settings.sppRxQueueSize ,
298
- settings.sppTxQueueSize ); // localName, isMaster, disableBLE, rxBufferSize, txBufferSize
317
+ beginSuccess &= bluetoothSerialBle-> begin (
318
+ deviceName, false , false , settings.sppRxQueueSize ,
319
+ settings.sppTxQueueSize ); // localName, isMaster, disableBLE, rxBufferSize, txBufferSize
320
+ beginSuccess &= bluetoothSerialSpp-> begin (
321
+ deviceName, false , false , settings.sppRxQueueSize ,
322
+ settings.sppTxQueueSize ); // localName, isMaster, disableBLE, rxBufferSize, txBufferSize
299
323
}
300
- else
324
+ else if (settings. bluetoothRadioType == BLUETOOTH_RADIO_SPP)
301
325
{
302
- beginSuccess &=
303
- bluetoothSerial->begin (deviceName, false , true , settings.sppRxQueueSize ,
304
- settings.sppTxQueueSize ); // localName, isMaster, disableBLE, rxBufferSize, txBufferSize
326
+ // Disable BLE
327
+ beginSuccess &= bluetoothSerialSpp->begin (
328
+ deviceName, false , true , settings.sppRxQueueSize ,
329
+ settings.sppTxQueueSize ); // localName, isMaster, disableBLE, rxBufferSize, txBufferSize
330
+ }
331
+ else if (settings.bluetoothRadioType == BLUETOOTH_RADIO_BLE)
332
+ {
333
+ // Don't disable BLE
334
+ beginSuccess &= bluetoothSerialBle->begin (
335
+ deviceName, false , false , settings.sppRxQueueSize ,
336
+ settings.sppTxQueueSize ); // localName, isMaster, disableBLE, rxBufferSize, txBufferSize
305
337
}
306
338
307
339
if (beginSuccess == false )
@@ -315,8 +347,8 @@ void bluetoothStart()
315
347
// https://github.com/espressif/esp-idf/issues/1541
316
348
// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
317
349
/*
318
- // Note: Since version 3.0.0 this library does not support legacy pairing (using fixed PIN consisting of 4 digits).
319
- esp_bt_sp_param_t param_type = ESP_BT_SP_IOCAP_MODE;
350
+ // Note: Since version 3.0.0 this library does not support legacy pairing (using fixed PIN consisting of 4
351
+ digits). esp_bt_sp_param_t param_type = ESP_BT_SP_IOCAP_MODE;
320
352
321
353
esp_bt_io_cap_t iocap = ESP_BT_IO_CAP_NONE; // Requires pin 1234 on old BT dongle, No prompt on new BT dongle
322
354
// esp_bt_io_cap_t iocap = ESP_BT_IO_CAP_OUT; //Works but prompts for either pin (old) or 'Does this 6 pin
@@ -342,10 +374,15 @@ void bluetoothStart()
342
374
bluetoothSerialSpp->register_callback (bluetoothCallback); // Controls BT state and LED
343
375
bluetoothSerialSpp->setTimeout (250 );
344
376
}
345
- else
377
+ else if (settings.bluetoothRadioType == BLUETOOTH_RADIO_SPP)
378
+ {
379
+ bluetoothSerialSpp->register_callback (bluetoothCallback); // Controls BT state and LED
380
+ bluetoothSerialSpp->setTimeout (250 );
381
+ }
382
+ else if (settings.bluetoothRadioType == BLUETOOTH_RADIO_BLE)
346
383
{
347
- bluetoothSerial ->register_callback (bluetoothCallback); // Controls BT state and LED
348
- bluetoothSerial ->setTimeout (250 );
384
+ bluetoothSerialBle ->register_callback (bluetoothCallback); // Controls BT state and LED
385
+ bluetoothSerialBle ->setTimeout (250 );
349
386
}
350
387
351
388
if (settings.bluetoothRadioType == BLUETOOTH_RADIO_SPP_AND_BLE)
@@ -390,7 +427,7 @@ void bluetoothStart()
390
427
// }
391
428
392
429
// This function stops BT so that it can be restarted later
393
- // It also releases as much system resources as possible so that WiFi/caster is more stable
430
+ // It also releases as many system resources as possible so that WiFi/caster is more stable
394
431
void bluetoothStop ()
395
432
{
396
433
#ifdef COMPILE_BT
@@ -399,22 +436,28 @@ void bluetoothStop()
399
436
if (settings.bluetoothRadioType == BLUETOOTH_RADIO_SPP_AND_BLE)
400
437
{
401
438
bluetoothSerialBle->register_callback (nullptr );
402
- bluetoothSerialBle->flush (); // Complete any transfers
439
+ bluetoothSerialBle->flush (); // Complete any transfers
403
440
bluetoothSerialBle->disconnect (); // Drop any clients
404
- bluetoothSerialBle->end (); // Release resources
441
+ bluetoothSerialBle->end (); // Release resources
405
442
406
443
bluetoothSerialSpp->register_callback (nullptr );
407
- bluetoothSerialSpp->flush (); // Complete any transfers
444
+ bluetoothSerialSpp->flush (); // Complete any transfers
408
445
bluetoothSerialSpp->disconnect (); // Drop any clients
409
- bluetoothSerialSpp->end (); // Release resources
446
+ bluetoothSerialSpp->end (); // Release resources
410
447
}
411
- else
448
+ else if (settings. bluetoothRadioType == BLUETOOTH_RADIO_SPP)
412
449
{
413
- bluetoothSerial->register_callback (nullptr );
414
- bluetoothSerial->flush (); // Complete any transfers
415
- bluetoothSerial->disconnect (); // Drop any clients
416
- bluetoothSerial->end (); // bluetoothSerial->end() will release significant RAM (~100k!) but a
417
- // bluetoothSerial->start will crash.
450
+ bluetoothSerialSpp->register_callback (nullptr );
451
+ bluetoothSerialSpp->flush (); // Complete any transfers
452
+ bluetoothSerialSpp->disconnect (); // Drop any clients
453
+ bluetoothSerialSpp->end (); // Release resources
454
+ }
455
+ else if (settings.bluetoothRadioType == BLUETOOTH_RADIO_BLE)
456
+ {
457
+ bluetoothSerialBle->register_callback (nullptr );
458
+ bluetoothSerialBle->flush (); // Complete any transfers
459
+ bluetoothSerialBle->disconnect (); // Drop any clients
460
+ bluetoothSerialBle->end (); // Release resources
418
461
}
419
462
420
463
log_d (" Bluetooth turned off" );
@@ -473,9 +516,14 @@ void bluetoothTest(bool runTest)
473
516
snprintf (macAddress, sizeof (macAddress), " %02X%02X" , btMACAddress[4 ], btMACAddress[5 ]);
474
517
systemPrint (" Bluetooth " );
475
518
if (settings.bluetoothRadioType == BLUETOOTH_RADIO_SPP_AND_BLE)
476
- systemPrint (" Low Energy " );
519
+ systemPrint (" SPP and Low Energy " );
520
+ else if (settings.bluetoothRadioType == BLUETOOTH_RADIO_SPP)
521
+ systemPrint (" SPP " );
477
522
else if (settings.bluetoothRadioType == BLUETOOTH_RADIO_BLE)
478
523
systemPrint (" Low Energy " );
524
+ else if (settings.bluetoothRadioType == BLUETOOTH_RADIO_OFF)
525
+ systemPrint (" Off " );
526
+
479
527
systemPrint (" (" );
480
528
systemPrint (macAddress);
481
529
systemPrint (" ): " );
0 commit comments