Skip to content

Commit 6d00baa

Browse files
authored
Merge pull request #1 from RobertoHE/Client_Upgrade
Update and simplify connect()
2 parents 4e52f70 + 586207d commit 6d00baa

File tree

1 file changed

+45
-54
lines changed

1 file changed

+45
-54
lines changed

src/hardware/BLEMIDI_Client_ESP32.h

Lines changed: 45 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ static uint32_t userOnPassKeyRequest()
7878
return passkey;
7979
};
8080

81-
/*
81+
/*
8282
###### BLE COMMUNICATION PARAMS ######
8383
*/
8484
/** Set connection parameters:
@@ -94,10 +94,10 @@ static uint32_t userOnPassKeyRequest()
9494
* 0 latency (Number of intervals allowed to skip),
9595
* TimeOut (unit: 10ms) 51 * 10ms = 510ms. Timeout should be minimum 100ms.
9696
*/
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
9999
#define BLEMIDI_CLIENT_COMM_LATENCY 0
100-
#define BLEMIDI_CLIENT_COMM_TIMEOUT 400 //4000ms
100+
#define BLEMIDI_CLIENT_COMM_TIMEOUT 200 //2000ms
101101

102102
/*
103103
#############################################
@@ -162,7 +162,7 @@ class AdvertisedDeviceCallbacks : public NimBLEAdvertisedDeviceCallbacks
162162
{
163163
/** Ready to connect now */
164164
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*/
166166
advDevice = *advertisedDevice;
167167
/** stop scan before connecting */
168168
NimBLEDevice::getScan()->stop();
@@ -259,7 +259,9 @@ class BLEMIDI_Client_ESP32
259259
void disconnected()
260260
{
261261
if (_bleMidiTransport->_disconnectedCallback)
262+
{
262263
_bleMidiTransport->_disconnectedCallback();
264+
}
263265
}
264266

265267
void notifyCB(NimBLERemoteCharacteristic *pRemoteCharacteristic, uint8_t *pData, size_t length, bool isNotify);
@@ -307,7 +309,7 @@ class MyClientCallbacks : public BLEClientCallbacks
307309
}
308310

309311
//Try reconnection or search a new one
310-
NimBLEDevice::getScan()->start(3, scanEndedCB);
312+
NimBLEDevice::getScan()->start(1, scanEndedCB);
311313
}
312314

313315
bool onConnParamsUpdateRequest(NimBLEClient *pClient, const ble_gap_upd_params *params)
@@ -419,7 +421,6 @@ void BLEMIDI_Client_ESP32::notifyCB(NimBLERemoteCharacteristic *pRemoteCharacter
419421
{
420422
if (this->_characteristic == pRemoteCharacteristic) //Redundant protection
421423
{
422-
if (uxQueueSpacesAvailable(mRxQueue) >= length) //Don't overflow the queue. If the queue is overflowed, comunication breaks out
423424
receive(pData, length);
424425
}
425426
}
@@ -439,13 +440,12 @@ void BLEMIDI_Client_ESP32::scan()
439440
pBLEScan->setActiveScan(true);
440441

441442
Serial.println("Scanning...");
442-
pBLEScan->start(3, scanEndedCB);
443+
pBLEScan->start(1, scanEndedCB);
443444
}
444445
};
445446

446447
bool BLEMIDI_Client_ESP32::connect()
447448
{
448-
Serial.println("Try Connection...");
449449
using namespace std::placeholders; //<- for bind funtion in callback notification
450450

451451
/** Check if we have a client we should reuse first
@@ -461,39 +461,38 @@ bool BLEMIDI_Client_ESP32::connect()
461461
{
462462
if (_characteristic->canNotify())
463463
{
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)))
472465
{
466+
//Re-connection SUCCESS
473467
return true;
474468
}
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();
482472
}
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;
483477
}
478+
/*If client does not match, delete previous client and create a new one*/
479+
NimBLEDevice::deleteClient(_client);
480+
_client = nullptr;
484481
}
485482

486483
if (NimBLEDevice::getClientListSize() >= NIMBLE_MAX_CONNECTIONS)
487484
{
488485
Serial.println("Max clients reached - no more connections available");
489486
return false;
490487
}
491-
488+
489+
// Create and setup a new client
492490
_client = BLEDevice::createClient();
493491

494492
_client->setClientCallbacks(new MyClientCallbacks(this), false);
495493

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+
497496
/** Set how long we are willing to wait for the connection to complete (seconds), default is 30. */
498497
_client->setConnectTimeout(15);
499498

@@ -502,14 +501,15 @@ bool BLEMIDI_Client_ESP32::connect()
502501
/** Created a client but failed to connect, don't need to keep it as it has no data */
503502
NimBLEDevice::deleteClient(_client);
504503
_client = nullptr;
505-
Serial.println("Failed to connect, deleted client");
504+
//Serial.println("Failed to connect, deleted client");
506505
return false;
507506
}
508507

509508
if (!_client->isConnected())
510509
{
511-
Serial.println("Failed to connect");
510+
//Serial.println("Failed to connect");
512511
_client->disconnect();
512+
NimBLEDevice::deleteClient(_client);
513513
_client = nullptr;
514514
return false;
515515
}
@@ -519,44 +519,35 @@ bool BLEMIDI_Client_ESP32::connect()
519519
Serial.print(" / ");
520520
Serial.println(_client->getPeerAddress().toString().c_str());
521521

522+
/*
522523
Serial.print("RSSI: ");
523524
Serial.println(_client->getRssi());
524-
525+
*/
526+
525527
/** Now we can read/write/subscribe the charateristics of the services we are interested in */
526528
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+
{
529531
_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+
{
533535
if (_characteristic->canNotify())
534536
{
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)))
536538
{
537-
Serial.println("Error - Subcribe error");
538-
/** Disconnect if subscribe failed */
539-
_client->disconnect();
540-
return false;
539+
//Connection SUCCESS
540+
return true;
541541
}
542542
}
543-
else
544-
{
545-
return false;
546-
}
547-
}
548-
else
549-
{
550-
return false;
551543
}
552544
}
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;
560551
};
561552

562553
/** Callback to process the results of the last scan or restart it */

0 commit comments

Comments
 (0)