Skip to content

Commit f9ad1e5

Browse files
authored
Transmition speed Improved WriteNoResponse
Now it sends data to server in Write Without Response mode. This increase the transmission speed and it get adapted to MIDI over BLE protocol. Some devices needs a first message with confirmation for clean security flags. After a connection or after an error, the first message is sended like Write (with response).
1 parent 6045d22 commit f9ad1e5

File tree

1 file changed

+32
-18
lines changed

1 file changed

+32
-18
lines changed

src/hardware/BLEMIDI_Client_ESP32.h

Lines changed: 32 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@
5858
* Uncomment what you need
5959
* These are the default values.
6060
*/
61-
//#define BLEMIDI_CLIENT_BOND
61+
#define BLEMIDI_CLIENT_BOND
6262
//#define BLEMIDI_CLIENT_MITM
6363
#define BLEMIDI_CLIENT_PAIR
6464

@@ -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
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
101101

102102
/*
103103
#############################################
@@ -191,6 +191,7 @@ class BLEMIDI_Client_ESP32
191191
BLEAdvertising *_advertising = nullptr;
192192
BLERemoteCharacteristic *_characteristic = nullptr;
193193
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.
194195

195196
BLEMIDI_Transport<class BLEMIDI_Client_ESP32> *_bleMidiTransport = nullptr;
196197

@@ -228,15 +229,26 @@ class BLEMIDI_Client_ESP32
228229
return;
229230
if (_characteristic == NULL)
230231
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;
232244
}
233245

234246
bool available(byte *pvBuffer);
235247

236248
void add(byte value)
237249
{
238250
// 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);
240252
}
241253

242254
protected:
@@ -254,6 +266,7 @@ class BLEMIDI_Client_ESP32
254266
{
255267
_bleMidiTransport->_connectedCallback();
256268
}
269+
firstTimeSend = true;
257270
}
258271

259272
void disconnected()
@@ -262,6 +275,7 @@ class BLEMIDI_Client_ESP32
262275
{
263276
_bleMidiTransport->_disconnectedCallback();
264277
}
278+
firstTimeSend = true;
265279
}
266280

267281
void notifyCB(NimBLERemoteCharacteristic *pRemoteCharacteristic, uint8_t *pData, size_t length, bool isNotify);
@@ -421,7 +435,7 @@ void BLEMIDI_Client_ESP32::notifyCB(NimBLERemoteCharacteristic *pRemoteCharacter
421435
{
422436
if (this->_characteristic == pRemoteCharacteristic) //Redundant protection
423437
{
424-
receive(pData, length);
438+
receive(pData, length);
425439
}
426440
}
427441

@@ -466,14 +480,14 @@ bool BLEMIDI_Client_ESP32::connect()
466480
//Re-connection SUCCESS
467481
return true;
468482
}
469-
}
483+
}
470484
/** Disconnect if subscribe failed */
471485
_client->disconnect();
472486
}
473487
/* If any connection problem exits, delete previous client and try again in the next attemp as new client*/
474488
NimBLEDevice::deleteClient(_client);
475489
_client = nullptr;
476-
return false;
490+
return false;
477491
}
478492
/*If client does not match, delete previous client and create a new one*/
479493
NimBLEDevice::deleteClient(_client);
@@ -485,14 +499,14 @@ bool BLEMIDI_Client_ESP32::connect()
485499
Serial.println("Max clients reached - no more connections available");
486500
return false;
487501
}
488-
502+
489503
// Create and setup a new client
490504
_client = BLEDevice::createClient();
491505

492506
_client->setClientCallbacks(new MyClientCallbacks(this), false);
493507

494508
_client->setConnectionParams(BLEMIDI_CLIENT_COMM_MIN_INTERVAL, BLEMIDI_CLIENT_COMM_MAX_INTERVAL, BLEMIDI_CLIENT_COMM_LATENCY, BLEMIDI_CLIENT_COMM_TIMEOUT);
495-
509+
496510
/** Set how long we are willing to wait for the connection to complete (seconds), default is 30. */
497511
_client->setConnectTimeout(15);
498512

@@ -523,15 +537,15 @@ bool BLEMIDI_Client_ESP32::connect()
523537
Serial.print("RSSI: ");
524538
Serial.println(_client->getRssi());
525539
*/
526-
540+
527541
/** Now we can read/write/subscribe the charateristics of the services we are interested in */
528542
pSvc = _client->getService(SERVICE_UUID);
529543
if (pSvc) /** make sure it's not null */
530-
{
544+
{
531545
_characteristic = pSvc->getCharacteristic(CHARACTERISTIC_UUID);
532-
546+
533547
if (_characteristic) /** make sure it's not null */
534-
{
548+
{
535549
if (_characteristic->canNotify())
536550
{
537551
if (_characteristic->subscribe(true, std::bind(&BLEMIDI_Client_ESP32::notifyCB, this, _1, _2, _3, _4)))
@@ -542,7 +556,7 @@ bool BLEMIDI_Client_ESP32::connect()
542556
}
543557
}
544558
}
545-
559+
546560
//If anything fails, disconnect and delete client
547561
_client->disconnect();
548562
NimBLEDevice::deleteClient(_client);

0 commit comments

Comments
 (0)