@@ -33,15 +33,18 @@ struct DefaultSettingsClient : public BLEMIDI_NAMESPACE::DefaultSettings
33
33
/*
34
34
##### BLE DEVICE NAME #####
35
35
*/
36
+
36
37
/* *
37
38
* Set name of ble device (not affect to connection with server)
38
39
* max 16 characters
39
40
*/
40
- static constexpr char *name = " BleMidiClient" ;
41
-
41
+ static constexpr char *name = " BleMidiClient" ;
42
+
43
+
42
44
/*
43
45
###### TX POWER #####
44
46
*/
47
+
45
48
/* *
46
49
* Set power transmision
47
50
*
@@ -56,9 +59,11 @@ struct DefaultSettingsClient : public BLEMIDI_NAMESPACE::DefaultSettings
56
59
*/
57
60
static const esp_power_level_t clientTXPwr = ESP_PWR_LVL_P9;
58
61
62
+
59
63
/*
60
64
###### SECURITY #####
61
65
*/
66
+
62
67
/* * Set the IO capabilities of the device, each option will trigger a different pairing method.
63
68
* BLE_HS_IO_KEYBOARD_ONLY - Passkey pairing
64
69
* BLE_HS_IO_DISPLAY_YESNO - Numeric comparison pairing
@@ -83,9 +88,11 @@ struct DefaultSettingsClient : public BLEMIDI_NAMESPACE::DefaultSettings
83
88
*/
84
89
static constexpr PasskeyRequestCallback userOnPassKeyRequest = defautlPasskeyRequest;
85
90
91
+
86
92
/*
87
93
###### BLE COMMUNICATION PARAMS ######
88
94
*/
95
+
89
96
/* * Set connection parameters:
90
97
* If you only use one connection, put recomended BLE server param communication
91
98
* (you may scan it ussing "nRF Connect" app or other similar apps).
@@ -104,6 +111,7 @@ struct DefaultSettingsClient : public BLEMIDI_NAMESPACE::DefaultSettings
104
111
static const uint16_t commLatency = 0 ; //
105
112
static const uint16_t commTimeOut = 200 ; // 2000ms
106
113
114
+
107
115
/*
108
116
###### BLE FORCE NEW CONNECTION ######
109
117
*/
@@ -118,7 +126,6 @@ struct DefaultSettingsClient : public BLEMIDI_NAMESPACE::DefaultSettings
118
126
static const bool forceNewConnection = false ;
119
127
};
120
128
121
-
122
129
/* * Define a class to handle the callbacks when advertisments are received */
123
130
class AdvertisedDeviceCallbacks : public NimBLEAdvertisedDeviceCallbacks
124
131
{
@@ -275,15 +282,15 @@ class MyClientCallbacks : public BLEClientCallbacks
275
282
276
283
uint32_t onPassKeyRequest ()
277
284
{
278
- // if (nullptr != _Settings::userOnPassKeyRequest)
279
- return _Settings::userOnPassKeyRequest ();
280
- // return 0;
285
+ // if (nullptr != _Settings::userOnPassKeyRequest)
286
+ return _Settings::userOnPassKeyRequest ();
287
+ // return 0;
281
288
};
282
289
283
290
void onConnect (BLEClient *pClient)
284
291
{
285
292
DEBUGCLIENT (" ##Connected##" );
286
- // pClient->updateConnParams(_Settings::commMinInterval, _Settings::commMaxInterval, _Settings::commLatency, _Settings::commTimeOut);
293
+ // pClient->updateConnParams(_Settings::commMinInterval, _Settings::commMaxInterval, _Settings::commLatency, _Settings::commTimeOut);
287
294
vTaskDelay (1 );
288
295
if (_bluetoothEsp32)
289
296
_bluetoothEsp32->connected ();
@@ -299,18 +306,18 @@ class MyClientCallbacks : public BLEClientCallbacks
299
306
_bluetoothEsp32->disconnected ();
300
307
}
301
308
302
- if (_Settings::forceNewConnection)
303
- {
304
- // Renew Client
305
- NimBLEDevice::deleteClient (pClient);
306
- pClient = nullptr ;
307
- }
309
+ if (_Settings::forceNewConnection)
310
+ {
311
+ // Renew Client
312
+ NimBLEDevice::deleteClient (pClient);
313
+ pClient = nullptr ;
314
+ }
308
315
309
316
// Try reconnection or search a new one
310
317
NimBLEDevice::getScan ()->start (1 , scanEndedCB);
311
318
}
312
319
313
- bool onConnParamsUpdateRequest (NimBLEClient *pClient, const ble_gap_upd_params *params)
320
+ bool onConnParamsUpdateRequest (NimBLEClient *pClient, const ble_gap_upd_params *params)
314
321
{
315
322
if (params->itvl_min < _Settings::commMinInterval)
316
323
{ /* * 1.25ms units */
@@ -346,24 +353,23 @@ bool BLEMIDI_Client_ESP32<_Settings>::begin(const char *deviceName, BLEMIDI_Tran
346
353
_bleMidiTransport = bleMidiTransport;
347
354
348
355
std::string strDeviceName (deviceName);
349
- if (strDeviceName == " " ) // Connect to the first midi server found
356
+ // Connect to the first midi server found
357
+ if (strDeviceName == " " )
350
358
{
351
359
myAdvCB.specificTarget = false ;
352
360
myAdvCB.nameTarget = " " ;
353
361
}
354
- else // Connect to a specific name or address
362
+ // Connect to a specific name or address
363
+ else
355
364
{
356
365
myAdvCB.specificTarget = true ;
357
366
myAdvCB.nameTarget = strDeviceName;
358
367
}
359
368
360
- static char array[16 ] = " patata" ;
361
-
369
+ static char array[16 ];
362
370
memcpy (array, _Settings::name, 16 );
363
371
strDeviceName = array;
364
-
365
372
DEBUGCLIENT (strDeviceName.c_str ());
366
-
367
373
NimBLEDevice::init (strDeviceName);
368
374
369
375
// To communicate between the 2 cores.
@@ -390,7 +396,8 @@ bool BLEMIDI_Client_ESP32<_Settings>::available(byte *pvBuffer)
390
396
return false ;
391
397
}
392
398
393
- if (_client == nullptr || !_client->isConnected ()) // Try to connect/reconnect
399
+ // Try to connect/reconnect
400
+ if (_client == nullptr || !_client->isConnected ())
394
401
{
395
402
if (myAdvCB.doConnect )
396
403
{
@@ -445,42 +452,41 @@ bool BLEMIDI_Client_ESP32<_Settings>::connect()
445
452
{
446
453
using namespace std ::placeholders; // <- for bind funtion in callback notification
447
454
448
- // Retry to connecto to last one
449
- if (!_Settings::forceNewConnection)
450
- {
451
- /* * Check if we have a client we should reuse first
452
- * Special case when we already know this device
453
- * This saves considerable time and power.
454
- */
455
-
456
- if (_client)
455
+ // Retry to connecto to last one
456
+ if (!_Settings::forceNewConnection)
457
457
{
458
- if (_client == NimBLEDevice::getClientByPeerAddress (myAdvCB.advDevice .getAddress ()))
458
+ /* * Check if we have a client we should reuse first
459
+ * Special case when we already know this device
460
+ * This saves considerable time and power.
461
+ */
462
+
463
+ if (_client)
459
464
{
460
- if (_client-> connect (& myAdvCB.advDevice , false ))
465
+ if (_client == NimBLEDevice::getClientByPeerAddress ( myAdvCB.advDevice . getAddress () ))
461
466
{
462
- if (_characteristic-> canNotify ( ))
467
+ if (_client-> connect (&myAdvCB. advDevice , false ))
463
468
{
464
- if (_characteristic->subscribe ( true , std::bind (&BLEMIDI_Client_ESP32::notifyCB, this , _1, _2, _3, _4) ))
469
+ if (_characteristic->canNotify ( ))
465
470
{
466
- // Re-connection SUCCESS
467
- return true ;
471
+ if (_characteristic->subscribe (true , std::bind (&BLEMIDI_Client_ESP32::notifyCB, this , _1, _2, _3, _4)))
472
+ {
473
+ // Re-connection SUCCESS
474
+ return true ;
475
+ }
468
476
}
477
+ /* * Disconnect if subscribe failed */
478
+ _client->disconnect ();
469
479
}
470
- /* * Disconnect if subscribe failed */
471
- _client->disconnect ();
480
+ /* If any connection problem exits, delete previous client and try again in the next attemp as new client*/
481
+ NimBLEDevice::deleteClient (_client);
482
+ _client = nullptr ;
483
+ return false ;
472
484
}
473
- /* If any connection problem exits , delete previous client and try again in the next attemp as new client */
485
+ /* If client does not match , delete previous client and create a new one */
474
486
NimBLEDevice::deleteClient (_client);
475
487
_client = nullptr ;
476
- return false ;
477
488
}
478
- /* If client does not match, delete previous client and create a new one*/
479
- NimBLEDevice::deleteClient (_client);
480
- _client = nullptr ;
481
489
}
482
- }
483
-
484
490
485
491
if (NimBLEDevice::getClientListSize () >= NIMBLE_MAX_CONNECTIONS)
486
492
{
@@ -493,7 +499,7 @@ if (!_Settings::forceNewConnection)
493
499
494
500
_client->setClientCallbacks (new MyClientCallbacks<_Settings>(this ), false );
495
501
496
- _client->setConnectionParams (_Settings::commMinInterval,_Settings::commMaxInterval, _Settings::commLatency, _Settings::commTimeOut);
502
+ _client->setConnectionParams (_Settings::commMinInterval, _Settings::commMaxInterval, _Settings::commLatency, _Settings::commTimeOut);
497
503
498
504
/* * Set how long we are willing to wait for the connection to complete (seconds), default is 30. */
499
505
_client->setConnectTimeout (15 );
@@ -517,7 +523,7 @@ if (!_Settings::forceNewConnection)
517
523
}
518
524
519
525
DEBUGCLIENT (" Connected to: " + myAdvCB.advDevice .getName ().c_str () + " / " + _client->getPeerAddress ().toString ().c_str ());
520
-
526
+
521
527
DEBUGCLIENT (" RSSI: " );
522
528
DEBUGCLIENT (_client->getRssi ());
523
529
0 commit comments