37
37
#define BLEMIDI_CLIENT_DEFAULT_NAME " BLEMIDI-CLIENT"
38
38
#endif // Not modify
39
39
40
+ /*
41
+ ###### TX POWER #####
42
+ */
43
+ /* *
44
+ * Set power transmision
45
+ *
46
+ * ESP_PWR_LVL_N12 // Corresponding to -12dbm Minimum
47
+ * ESP_PWR_LVL_N9 // Corresponding to -9dbm
48
+ * ESP_PWR_LVL_N6 // Corresponding to -6dbm
49
+ * ESP_PWR_LVL_N3 // Corresponding to -3dbm
50
+ * ESP_PWR_LVL_N0 // Corresponding to 0dbm
51
+ * ESP_PWR_LVL_P3 // Corresponding to +3dbm
52
+ * ESP_PWR_LVL_P6 // Corresponding to +6dbm
53
+ * ESP_PWR_LVL_P9 // Corresponding to +9dbm Maximum
54
+ */
55
+
56
+ #define BLEMIDI_TX_PWR ESP_PWR_LVL_P9
57
+
40
58
/*
41
59
###### SECURITY #####
42
60
*/
58
76
* Uncomment what you need
59
77
* These are the default values.
60
78
*/
61
- // #define BLEMIDI_CLIENT_BOND
79
+ #define BLEMIDI_CLIENT_BOND
62
80
// #define BLEMIDI_CLIENT_MITM
63
81
#define BLEMIDI_CLIENT_PAIR
64
82
@@ -78,7 +96,7 @@ static uint32_t userOnPassKeyRequest()
78
96
return passkey;
79
97
};
80
98
81
- /*
99
+ /*
82
100
###### BLE COMMUNICATION PARAMS ######
83
101
*/
84
102
/* * Set connection parameters:
@@ -94,10 +112,10 @@ static uint32_t userOnPassKeyRequest()
94
112
* 0 latency (Number of intervals allowed to skip),
95
113
* TimeOut (unit: 10ms) 51 * 10ms = 510ms. Timeout should be minimum 100ms.
96
114
*/
97
- #define BLEMIDI_CLIENT_COMM_MIN_INTERVAL 6 // 7.5ms
98
- #define BLEMIDI_CLIENT_COMM_MAX_INTERVAL 35 // 40ms
99
- #define BLEMIDI_CLIENT_COMM_LATENCY 0
100
- #define BLEMIDI_CLIENT_COMM_TIMEOUT 200 // 2000ms
115
+ #define BLEMIDI_CLIENT_COMM_MIN_INTERVAL 6 // 7.5ms
116
+ #define BLEMIDI_CLIENT_COMM_MAX_INTERVAL 35 // 40ms
117
+ #define BLEMIDI_CLIENT_COMM_LATENCY 0 //
118
+ #define BLEMIDI_CLIENT_COMM_TIMEOUT 200 // 2000ms
101
119
102
120
/*
103
121
#############################################
@@ -191,6 +209,7 @@ class BLEMIDI_Client_ESP32
191
209
BLEAdvertising *_advertising = nullptr ;
192
210
BLERemoteCharacteristic *_characteristic = nullptr ;
193
211
BLERemoteService *pSvc = nullptr ;
212
+ bool firstTimeSend = true ; // First writeValue get sends like Write with reponse for clean security flags. After first time, all messages are send like WriteNoResponse for increase transmision speed.
194
213
195
214
BLEMIDI_Transport<class BLEMIDI_Client_ESP32 > *_bleMidiTransport = nullptr ;
196
215
@@ -228,15 +247,26 @@ class BLEMIDI_Client_ESP32
228
247
return ;
229
248
if (_characteristic == NULL )
230
249
return ;
231
- _characteristic->writeValue (data, length, true );
250
+
251
+ if (firstTimeSend)
252
+ {
253
+ _characteristic->writeValue (data, length, true );
254
+ firstTimeSend = false ;
255
+ return ;
256
+ }
257
+
258
+ if (!_characteristic->writeValue (data, length, !_characteristic->canWriteNoResponse ()))
259
+ firstTimeSend = true ;
260
+
261
+ return ;
232
262
}
233
263
234
264
bool available (byte *pvBuffer);
235
265
236
266
void add (byte value)
237
267
{
238
268
// called from BLE-MIDI, to add it to a buffer here
239
- xQueueSend (mRxQueue , &value, portMAX_DELAY/ 2 );
269
+ xQueueSend (mRxQueue , &value, portMAX_DELAY / 2 );
240
270
}
241
271
242
272
protected:
@@ -254,6 +284,7 @@ class BLEMIDI_Client_ESP32
254
284
{
255
285
_bleMidiTransport->_connectedCallback ();
256
286
}
287
+ firstTimeSend = true ;
257
288
}
258
289
259
290
void disconnected ()
@@ -262,6 +293,7 @@ class BLEMIDI_Client_ESP32
262
293
{
263
294
_bleMidiTransport->_disconnectedCallback ();
264
295
}
296
+ firstTimeSend = true ;
265
297
}
266
298
267
299
void notifyCB (NimBLERemoteCharacteristic *pRemoteCharacteristic, uint8_t *pData, size_t length, bool isNotify);
@@ -290,7 +322,7 @@ class MyClientCallbacks : public BLEClientCallbacks
290
322
void onConnect (BLEClient *pClient)
291
323
{
292
324
// Serial.println("##Connected##");
293
- pClient->updateConnParams (BLEMIDI_CLIENT_COMM_MIN_INTERVAL, BLEMIDI_CLIENT_COMM_MAX_INTERVAL, BLEMIDI_CLIENT_COMM_LATENCY, BLEMIDI_CLIENT_COMM_TIMEOUT);
325
+ // pClient->updateConnParams(BLEMIDI_CLIENT_COMM_MIN_INTERVAL, BLEMIDI_CLIENT_COMM_MAX_INTERVAL, BLEMIDI_CLIENT_COMM_LATENCY, BLEMIDI_CLIENT_COMM_TIMEOUT);
294
326
vTaskDelay (1 );
295
327
if (_bluetoothEsp32)
296
328
{
@@ -326,10 +358,11 @@ class MyClientCallbacks : public BLEClientCallbacks
326
358
{ /* * Number of intervals allowed to skip */
327
359
return false ;
328
360
}
329
- else if (params->supervision_timeout > BLEMIDI_CLIENT_COMM_TIMEOUT + 10 )
361
+ else if (params->supervision_timeout > BLEMIDI_CLIENT_COMM_TIMEOUT)
330
362
{ /* * 10ms units */
331
363
return false ;
332
364
}
365
+ pClient->updateConnParams (params->itvl_min , params->itvl_max , params->latency , params->supervision_timeout );
333
366
334
367
return true ;
335
368
};
@@ -380,7 +413,7 @@ bool BLEMIDI_Client_ESP32::begin(const char *deviceName, BLEMIDI_Transport<class
380
413
NimBLEDevice::setSecurityAuth (BLEMIDI_CLIENT_SECURITY_AUTH);
381
414
382
415
/* * Optional: set the transmit power, default is 3db */
383
- NimBLEDevice::setPower (ESP_PWR_LVL_P9 ); /* * +9db */
416
+ NimBLEDevice::setPower (BLEMIDI_TX_PWR ); /* * +9db */
384
417
385
418
myAdvCB.enableConnection = true ;
386
419
scan ();
@@ -421,7 +454,7 @@ void BLEMIDI_Client_ESP32::notifyCB(NimBLERemoteCharacteristic *pRemoteCharacter
421
454
{
422
455
if (this ->_characteristic == pRemoteCharacteristic) // Redundant protection
423
456
{
424
- receive (pData, length);
457
+ receive (pData, length);
425
458
}
426
459
}
427
460
@@ -466,14 +499,14 @@ bool BLEMIDI_Client_ESP32::connect()
466
499
// Re-connection SUCCESS
467
500
return true ;
468
501
}
469
- }
502
+ }
470
503
/* * Disconnect if subscribe failed */
471
504
_client->disconnect ();
472
505
}
473
506
/* If any connection problem exits, delete previous client and try again in the next attemp as new client*/
474
507
NimBLEDevice::deleteClient (_client);
475
508
_client = nullptr ;
476
- return false ;
509
+ return false ;
477
510
}
478
511
/* If client does not match, delete previous client and create a new one*/
479
512
NimBLEDevice::deleteClient (_client);
@@ -485,14 +518,14 @@ bool BLEMIDI_Client_ESP32::connect()
485
518
Serial.println (" Max clients reached - no more connections available" );
486
519
return false ;
487
520
}
488
-
521
+
489
522
// Create and setup a new client
490
523
_client = BLEDevice::createClient ();
491
524
492
525
_client->setClientCallbacks (new MyClientCallbacks (this ), false );
493
526
494
527
_client->setConnectionParams (BLEMIDI_CLIENT_COMM_MIN_INTERVAL, BLEMIDI_CLIENT_COMM_MAX_INTERVAL, BLEMIDI_CLIENT_COMM_LATENCY, BLEMIDI_CLIENT_COMM_TIMEOUT);
495
-
528
+
496
529
/* * Set how long we are willing to wait for the connection to complete (seconds), default is 30. */
497
530
_client->setConnectTimeout (15 );
498
531
@@ -523,15 +556,15 @@ bool BLEMIDI_Client_ESP32::connect()
523
556
Serial.print("RSSI: ");
524
557
Serial.println(_client->getRssi());
525
558
*/
526
-
559
+
527
560
/* * Now we can read/write/subscribe the charateristics of the services we are interested in */
528
561
pSvc = _client->getService (SERVICE_UUID);
529
562
if (pSvc) /* * make sure it's not null */
530
- {
563
+ {
531
564
_characteristic = pSvc->getCharacteristic (CHARACTERISTIC_UUID);
532
-
565
+
533
566
if (_characteristic) /* * make sure it's not null */
534
- {
567
+ {
535
568
if (_characteristic->canNotify ())
536
569
{
537
570
if (_characteristic->subscribe (true , std::bind (&BLEMIDI_Client_ESP32::notifyCB, this , _1, _2, _3, _4)))
@@ -542,7 +575,7 @@ bool BLEMIDI_Client_ESP32::connect()
542
575
}
543
576
}
544
577
}
545
-
578
+
546
579
// If anything fails, disconnect and delete client
547
580
_client->disconnect ();
548
581
NimBLEDevice::deleteClient (_client);
0 commit comments