Skip to content

Commit 927b3ac

Browse files
authored
Merge pull request #36 from RobertoHE/master
Improve transmissions in client mode (thank you @RobertoHE)
2 parents 6045d22 + b3df11a commit 927b3ac

File tree

1 file changed

+54
-21
lines changed

1 file changed

+54
-21
lines changed

src/hardware/BLEMIDI_Client_ESP32.h

Lines changed: 54 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,24 @@
3737
#define BLEMIDI_CLIENT_DEFAULT_NAME "BLEMIDI-CLIENT"
3838
#endif //Not modify
3939

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+
4058
/*
4159
###### SECURITY #####
4260
*/
@@ -58,7 +76,7 @@
5876
* Uncomment what you need
5977
* These are the default values.
6078
*/
61-
//#define BLEMIDI_CLIENT_BOND
79+
#define BLEMIDI_CLIENT_BOND
6280
//#define BLEMIDI_CLIENT_MITM
6381
#define BLEMIDI_CLIENT_PAIR
6482

@@ -78,7 +96,7 @@ static uint32_t userOnPassKeyRequest()
7896
return passkey;
7997
};
8098

81-
/*
99+
/*
82100
###### BLE COMMUNICATION PARAMS ######
83101
*/
84102
/** Set connection parameters:
@@ -94,10 +112,10 @@ static uint32_t userOnPassKeyRequest()
94112
* 0 latency (Number of intervals allowed to skip),
95113
* TimeOut (unit: 10ms) 51 * 10ms = 510ms. Timeout should be minimum 100ms.
96114
*/
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
101119

102120
/*
103121
#############################################
@@ -191,6 +209,7 @@ class BLEMIDI_Client_ESP32
191209
BLEAdvertising *_advertising = nullptr;
192210
BLERemoteCharacteristic *_characteristic = nullptr;
193211
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.
194213

195214
BLEMIDI_Transport<class BLEMIDI_Client_ESP32> *_bleMidiTransport = nullptr;
196215

@@ -228,15 +247,26 @@ class BLEMIDI_Client_ESP32
228247
return;
229248
if (_characteristic == NULL)
230249
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;
232262
}
233263

234264
bool available(byte *pvBuffer);
235265

236266
void add(byte value)
237267
{
238268
// 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);
240270
}
241271

242272
protected:
@@ -254,6 +284,7 @@ class BLEMIDI_Client_ESP32
254284
{
255285
_bleMidiTransport->_connectedCallback();
256286
}
287+
firstTimeSend = true;
257288
}
258289

259290
void disconnected()
@@ -262,6 +293,7 @@ class BLEMIDI_Client_ESP32
262293
{
263294
_bleMidiTransport->_disconnectedCallback();
264295
}
296+
firstTimeSend = true;
265297
}
266298

267299
void notifyCB(NimBLERemoteCharacteristic *pRemoteCharacteristic, uint8_t *pData, size_t length, bool isNotify);
@@ -290,7 +322,7 @@ class MyClientCallbacks : public BLEClientCallbacks
290322
void onConnect(BLEClient *pClient)
291323
{
292324
//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);
294326
vTaskDelay(1);
295327
if (_bluetoothEsp32)
296328
{
@@ -326,10 +358,11 @@ class MyClientCallbacks : public BLEClientCallbacks
326358
{ /** Number of intervals allowed to skip */
327359
return false;
328360
}
329-
else if (params->supervision_timeout > BLEMIDI_CLIENT_COMM_TIMEOUT + 10)
361+
else if (params->supervision_timeout > BLEMIDI_CLIENT_COMM_TIMEOUT)
330362
{ /** 10ms units */
331363
return false;
332364
}
365+
pClient->updateConnParams(params->itvl_min, params->itvl_max, params->latency, params->supervision_timeout);
333366

334367
return true;
335368
};
@@ -380,7 +413,7 @@ bool BLEMIDI_Client_ESP32::begin(const char *deviceName, BLEMIDI_Transport<class
380413
NimBLEDevice::setSecurityAuth(BLEMIDI_CLIENT_SECURITY_AUTH);
381414

382415
/** Optional: set the transmit power, default is 3db */
383-
NimBLEDevice::setPower(ESP_PWR_LVL_P9); /** +9db */
416+
NimBLEDevice::setPower(BLEMIDI_TX_PWR); /** +9db */
384417

385418
myAdvCB.enableConnection = true;
386419
scan();
@@ -421,7 +454,7 @@ void BLEMIDI_Client_ESP32::notifyCB(NimBLERemoteCharacteristic *pRemoteCharacter
421454
{
422455
if (this->_characteristic == pRemoteCharacteristic) //Redundant protection
423456
{
424-
receive(pData, length);
457+
receive(pData, length);
425458
}
426459
}
427460

@@ -466,14 +499,14 @@ bool BLEMIDI_Client_ESP32::connect()
466499
//Re-connection SUCCESS
467500
return true;
468501
}
469-
}
502+
}
470503
/** Disconnect if subscribe failed */
471504
_client->disconnect();
472505
}
473506
/* If any connection problem exits, delete previous client and try again in the next attemp as new client*/
474507
NimBLEDevice::deleteClient(_client);
475508
_client = nullptr;
476-
return false;
509+
return false;
477510
}
478511
/*If client does not match, delete previous client and create a new one*/
479512
NimBLEDevice::deleteClient(_client);
@@ -485,14 +518,14 @@ bool BLEMIDI_Client_ESP32::connect()
485518
Serial.println("Max clients reached - no more connections available");
486519
return false;
487520
}
488-
521+
489522
// Create and setup a new client
490523
_client = BLEDevice::createClient();
491524

492525
_client->setClientCallbacks(new MyClientCallbacks(this), false);
493526

494527
_client->setConnectionParams(BLEMIDI_CLIENT_COMM_MIN_INTERVAL, BLEMIDI_CLIENT_COMM_MAX_INTERVAL, BLEMIDI_CLIENT_COMM_LATENCY, BLEMIDI_CLIENT_COMM_TIMEOUT);
495-
528+
496529
/** Set how long we are willing to wait for the connection to complete (seconds), default is 30. */
497530
_client->setConnectTimeout(15);
498531

@@ -523,15 +556,15 @@ bool BLEMIDI_Client_ESP32::connect()
523556
Serial.print("RSSI: ");
524557
Serial.println(_client->getRssi());
525558
*/
526-
559+
527560
/** Now we can read/write/subscribe the charateristics of the services we are interested in */
528561
pSvc = _client->getService(SERVICE_UUID);
529562
if (pSvc) /** make sure it's not null */
530-
{
563+
{
531564
_characteristic = pSvc->getCharacteristic(CHARACTERISTIC_UUID);
532-
565+
533566
if (_characteristic) /** make sure it's not null */
534-
{
567+
{
535568
if (_characteristic->canNotify())
536569
{
537570
if (_characteristic->subscribe(true, std::bind(&BLEMIDI_Client_ESP32::notifyCB, this, _1, _2, _3, _4)))
@@ -542,7 +575,7 @@ bool BLEMIDI_Client_ESP32::connect()
542575
}
543576
}
544577
}
545-
578+
546579
//If anything fails, disconnect and delete client
547580
_client->disconnect();
548581
NimBLEDevice::deleteClient(_client);

0 commit comments

Comments
 (0)