@@ -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
97
+ #define BLEMIDI_CLIENT_COMM_MIN_INTERVAL 6 // 7.5ms
98
+ #define BLEMIDI_CLIENT_COMM_MAX_INTERVAL 35 // 40ms
99
99
#define BLEMIDI_CLIENT_COMM_LATENCY 0
100
- #define BLEMIDI_CLIENT_COMM_TIMEOUT 400 // 4000ms
100
+ #define BLEMIDI_CLIENT_COMM_TIMEOUT 200 // 2000ms
101
101
102
102
/*
103
103
#############################################
@@ -162,7 +162,7 @@ class AdvertisedDeviceCallbacks : public NimBLEAdvertisedDeviceCallbacks
162
162
{
163
163
/* * Ready to connect now */
164
164
doConnect = true ;
165
- /* * Save the device reference in a public variable the client can use*/
165
+ /* * Save the device reference in a public variable that the client can use*/
166
166
advDevice = *advertisedDevice;
167
167
/* * stop scan before connecting */
168
168
NimBLEDevice::getScan ()->stop ();
@@ -259,7 +259,9 @@ class BLEMIDI_Client_ESP32
259
259
void disconnected ()
260
260
{
261
261
if (_bleMidiTransport->_disconnectedCallback )
262
+ {
262
263
_bleMidiTransport->_disconnectedCallback ();
264
+ }
263
265
}
264
266
265
267
void notifyCB (NimBLERemoteCharacteristic *pRemoteCharacteristic, uint8_t *pData, size_t length, bool isNotify);
@@ -307,7 +309,7 @@ class MyClientCallbacks : public BLEClientCallbacks
307
309
}
308
310
309
311
// Try reconnection or search a new one
310
- NimBLEDevice::getScan ()->start (3 , scanEndedCB);
312
+ NimBLEDevice::getScan ()->start (1 , scanEndedCB);
311
313
}
312
314
313
315
bool onConnParamsUpdateRequest (NimBLEClient *pClient, const ble_gap_upd_params *params)
@@ -419,7 +421,6 @@ void BLEMIDI_Client_ESP32::notifyCB(NimBLERemoteCharacteristic *pRemoteCharacter
419
421
{
420
422
if (this ->_characteristic == pRemoteCharacteristic) // Redundant protection
421
423
{
422
- if (uxQueueSpacesAvailable (mRxQueue ) >= length) // Don't overflow the queue. If the queue is overflowed, comunication breaks out
423
424
receive (pData, length);
424
425
}
425
426
}
@@ -439,13 +440,12 @@ void BLEMIDI_Client_ESP32::scan()
439
440
pBLEScan->setActiveScan (true );
440
441
441
442
Serial.println (" Scanning..." );
442
- pBLEScan->start (3 , scanEndedCB);
443
+ pBLEScan->start (1 , scanEndedCB);
443
444
}
444
445
};
445
446
446
447
bool BLEMIDI_Client_ESP32::connect ()
447
448
{
448
- Serial.println (" Try Connection..." );
449
449
using namespace std ::placeholders; // <- for bind funtion in callback notification
450
450
451
451
/* * Check if we have a client we should reuse first
@@ -461,39 +461,38 @@ bool BLEMIDI_Client_ESP32::connect()
461
461
{
462
462
if (_characteristic->canNotify ())
463
463
{
464
- if (!_characteristic->subscribe (true , std::bind (&BLEMIDI_Client_ESP32::notifyCB, this , _1, _2, _3, _4)))
465
- {
466
- Serial.println (" Error - Not subscribe" );
467
- /* * Disconnect if subscribe failed */
468
- _client->disconnect ();
469
- return false ;
470
- }
471
- else
464
+ if (_characteristic->subscribe (true , std::bind (&BLEMIDI_Client_ESP32::notifyCB, this , _1, _2, _3, _4)))
472
465
{
466
+ // Re-connection SUCCESS
473
467
return true ;
474
468
}
475
- }
476
- }
477
- else
478
- {
479
- Serial.println (" Error. Reconnect failed" );
480
- _client = nullptr ;
481
- return false ;
469
+ }
470
+ /* * Disconnect if subscribe failed */
471
+ _client->disconnect ();
482
472
}
473
+ /* If any connection problem exits, delete previous client and try again in the next attemp as new client*/
474
+ NimBLEDevice::deleteClient (_client);
475
+ _client = nullptr ;
476
+ return false ;
483
477
}
478
+ /* If client does not match, delete previous client and create a new one*/
479
+ NimBLEDevice::deleteClient (_client);
480
+ _client = nullptr ;
484
481
}
485
482
486
483
if (NimBLEDevice::getClientListSize () >= NIMBLE_MAX_CONNECTIONS)
487
484
{
488
485
Serial.println (" Max clients reached - no more connections available" );
489
486
return false ;
490
487
}
491
-
488
+
489
+ // Create and setup a new client
492
490
_client = BLEDevice::createClient ();
493
491
494
492
_client->setClientCallbacks (new MyClientCallbacks (this ), false );
495
493
496
- _client->setConnectionParams (BLEMIDI_CLIENT_COMM_MIN_INTERVAL, BLEMIDI_CLIENT_COMM_MAX_INTERVAL + 10 , BLEMIDI_CLIENT_COMM_LATENCY + 1 , BLEMIDI_CLIENT_COMM_TIMEOUT + 10 );
494
+ _client->setConnectionParams (BLEMIDI_CLIENT_COMM_MIN_INTERVAL, BLEMIDI_CLIENT_COMM_MAX_INTERVAL, BLEMIDI_CLIENT_COMM_LATENCY, BLEMIDI_CLIENT_COMM_TIMEOUT);
495
+
497
496
/* * Set how long we are willing to wait for the connection to complete (seconds), default is 30. */
498
497
_client->setConnectTimeout (15 );
499
498
@@ -502,14 +501,15 @@ bool BLEMIDI_Client_ESP32::connect()
502
501
/* * Created a client but failed to connect, don't need to keep it as it has no data */
503
502
NimBLEDevice::deleteClient (_client);
504
503
_client = nullptr ;
505
- Serial.println (" Failed to connect, deleted client" );
504
+ // Serial.println("Failed to connect, deleted client");
506
505
return false ;
507
506
}
508
507
509
508
if (!_client->isConnected ())
510
509
{
511
- Serial.println (" Failed to connect" );
510
+ // Serial.println("Failed to connect");
512
511
_client->disconnect ();
512
+ NimBLEDevice::deleteClient (_client);
513
513
_client = nullptr ;
514
514
return false ;
515
515
}
@@ -519,44 +519,35 @@ bool BLEMIDI_Client_ESP32::connect()
519
519
Serial.print (" / " );
520
520
Serial.println (_client->getPeerAddress ().toString ().c_str ());
521
521
522
+ /*
522
523
Serial.print("RSSI: ");
523
524
Serial.println(_client->getRssi());
524
-
525
+ */
526
+
525
527
/* * Now we can read/write/subscribe the charateristics of the services we are interested in */
526
528
pSvc = _client->getService (SERVICE_UUID);
527
- if (pSvc)
528
- { /* * make sure it's not null */
529
+ if (pSvc) /* * make sure it's not null */
530
+ {
529
531
_characteristic = pSvc->getCharacteristic (CHARACTERISTIC_UUID);
530
-
531
- if (_characteristic)
532
- { /* * make sure it's not null */
532
+
533
+ if (_characteristic) /* * make sure it's not null */
534
+ {
533
535
if (_characteristic->canNotify ())
534
536
{
535
- if (! _characteristic->subscribe (true , std::bind (&BLEMIDI_Client_ESP32::notifyCB, this , _1, _2, _3, _4)))
537
+ if (_characteristic->subscribe (true , std::bind (&BLEMIDI_Client_ESP32::notifyCB, this , _1, _2, _3, _4)))
536
538
{
537
- Serial.println (" Error - Subcribe error" );
538
- /* * Disconnect if subscribe failed */
539
- _client->disconnect ();
540
- return false ;
539
+ // Connection SUCCESS
540
+ return true ;
541
541
}
542
542
}
543
- else
544
- {
545
- return false ;
546
- }
547
- }
548
- else
549
- {
550
- return false ;
551
543
}
552
544
}
553
- else
554
- {
555
- Serial.println (" Error. MIDI service not found." );
556
- return false ;
557
- }
558
-
559
- return true ;
545
+
546
+ // If anything fails, disconnect and delete client
547
+ _client->disconnect ();
548
+ NimBLEDevice::deleteClient (_client);
549
+ _client = nullptr ;
550
+ return false ;
560
551
};
561
552
562
553
/* * Callback to process the results of the last scan or restart it */
0 commit comments