58
58
* Uncomment what you need
59
59
* These are the default values.
60
60
*/
61
- // #define BLEMIDI_CLIENT_BOND
61
+ #define BLEMIDI_CLIENT_BOND
62
62
// #define BLEMIDI_CLIENT_MITM
63
63
#define BLEMIDI_CLIENT_PAIR
64
64
@@ -78,7 +78,7 @@ static uint32_t userOnPassKeyRequest()
78
78
return passkey;
79
79
};
80
80
81
- /*
81
+ /*
82
82
###### BLE COMMUNICATION PARAMS ######
83
83
*/
84
84
/* * Set connection parameters:
@@ -94,10 +94,10 @@ static uint32_t userOnPassKeyRequest()
94
94
* 0 latency (Number of intervals allowed to skip),
95
95
* TimeOut (unit: 10ms) 51 * 10ms = 510ms. Timeout should be minimum 100ms.
96
96
*/
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
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
101
101
102
102
/*
103
103
#############################################
@@ -191,6 +191,7 @@ class BLEMIDI_Client_ESP32
191
191
BLEAdvertising *_advertising = nullptr ;
192
192
BLERemoteCharacteristic *_characteristic = nullptr ;
193
193
BLERemoteService *pSvc = nullptr ;
194
+ 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
195
195
196
BLEMIDI_Transport<class BLEMIDI_Client_ESP32 > *_bleMidiTransport = nullptr ;
196
197
@@ -228,15 +229,26 @@ class BLEMIDI_Client_ESP32
228
229
return ;
229
230
if (_characteristic == NULL )
230
231
return ;
231
- _characteristic->writeValue (data, length, true );
232
+
233
+ if (firstTimeSend)
234
+ {
235
+ _characteristic->writeValue (data, length, true );
236
+ firstTimeSend = false ;
237
+ return ;
238
+ }
239
+
240
+ if (!_characteristic->writeValue (data, length, !_characteristic->canWriteNoResponse ()))
241
+ firstTimeSend = true ;
242
+
243
+ return ;
232
244
}
233
245
234
246
bool available (byte *pvBuffer);
235
247
236
248
void add (byte value)
237
249
{
238
250
// called from BLE-MIDI, to add it to a buffer here
239
- xQueueSend (mRxQueue , &value, portMAX_DELAY/ 2 );
251
+ xQueueSend (mRxQueue , &value, portMAX_DELAY / 2 );
240
252
}
241
253
242
254
protected:
@@ -254,6 +266,7 @@ class BLEMIDI_Client_ESP32
254
266
{
255
267
_bleMidiTransport->_connectedCallback ();
256
268
}
269
+ firstTimeSend = true ;
257
270
}
258
271
259
272
void disconnected ()
@@ -262,6 +275,7 @@ class BLEMIDI_Client_ESP32
262
275
{
263
276
_bleMidiTransport->_disconnectedCallback ();
264
277
}
278
+ firstTimeSend = true ;
265
279
}
266
280
267
281
void notifyCB (NimBLERemoteCharacteristic *pRemoteCharacteristic, uint8_t *pData, size_t length, bool isNotify);
@@ -421,7 +435,7 @@ void BLEMIDI_Client_ESP32::notifyCB(NimBLERemoteCharacteristic *pRemoteCharacter
421
435
{
422
436
if (this ->_characteristic == pRemoteCharacteristic) // Redundant protection
423
437
{
424
- receive (pData, length);
438
+ receive (pData, length);
425
439
}
426
440
}
427
441
@@ -466,14 +480,14 @@ bool BLEMIDI_Client_ESP32::connect()
466
480
// Re-connection SUCCESS
467
481
return true ;
468
482
}
469
- }
483
+ }
470
484
/* * Disconnect if subscribe failed */
471
485
_client->disconnect ();
472
486
}
473
487
/* If any connection problem exits, delete previous client and try again in the next attemp as new client*/
474
488
NimBLEDevice::deleteClient (_client);
475
489
_client = nullptr ;
476
- return false ;
490
+ return false ;
477
491
}
478
492
/* If client does not match, delete previous client and create a new one*/
479
493
NimBLEDevice::deleteClient (_client);
@@ -485,14 +499,14 @@ bool BLEMIDI_Client_ESP32::connect()
485
499
Serial.println (" Max clients reached - no more connections available" );
486
500
return false ;
487
501
}
488
-
502
+
489
503
// Create and setup a new client
490
504
_client = BLEDevice::createClient ();
491
505
492
506
_client->setClientCallbacks (new MyClientCallbacks (this ), false );
493
507
494
508
_client->setConnectionParams (BLEMIDI_CLIENT_COMM_MIN_INTERVAL, BLEMIDI_CLIENT_COMM_MAX_INTERVAL, BLEMIDI_CLIENT_COMM_LATENCY, BLEMIDI_CLIENT_COMM_TIMEOUT);
495
-
509
+
496
510
/* * Set how long we are willing to wait for the connection to complete (seconds), default is 30. */
497
511
_client->setConnectTimeout (15 );
498
512
@@ -523,15 +537,15 @@ bool BLEMIDI_Client_ESP32::connect()
523
537
Serial.print("RSSI: ");
524
538
Serial.println(_client->getRssi());
525
539
*/
526
-
540
+
527
541
/* * Now we can read/write/subscribe the charateristics of the services we are interested in */
528
542
pSvc = _client->getService (SERVICE_UUID);
529
543
if (pSvc) /* * make sure it's not null */
530
- {
544
+ {
531
545
_characteristic = pSvc->getCharacteristic (CHARACTERISTIC_UUID);
532
-
546
+
533
547
if (_characteristic) /* * make sure it's not null */
534
- {
548
+ {
535
549
if (_characteristic->canNotify ())
536
550
{
537
551
if (_characteristic->subscribe (true , std::bind (&BLEMIDI_Client_ESP32::notifyCB, this , _1, _2, _3, _4)))
@@ -542,7 +556,7 @@ bool BLEMIDI_Client_ESP32::connect()
542
556
}
543
557
}
544
558
}
545
-
559
+
546
560
// If anything fails, disconnect and delete client
547
561
_client->disconnect ();
548
562
NimBLEDevice::deleteClient (_client);
0 commit comments