[Server][Client] Add function to set data length.

Enables the use of BLE data length extension to improve data transfer rates.
This commit is contained in:
Author: Mr-Mime 2021-09-12 19:09:02 -06:00 committed by h2zero
parent ccea428b9e
commit 5925782a65
4 changed files with 41 additions and 2 deletions

View file

@ -436,6 +436,24 @@ void NimBLEClient::updateConnParams(uint16_t minInterval, uint16_t maxInterval,
} // updateConnParams } // updateConnParams
/**
* @brief Request an update of the data packet length.
* * Can only be used after a connection has been established.
* @details Sends a data length update request to the server the client is connected to.
* The Data Length Extension (DLE) allows to increase the Data Channel Payload from 27 bytes to up to 251 bytes.
* The server needs to support the Bluetooth 4.2 specifications, to be capable of DLE.
* @param [in] tx_octets The preferred number of payload octets to use (Range 0x001B-0x00FB).
*/
void NimBLEClient::setDataLen(uint16_t tx_octets) {
uint16_t tx_time = (tx_octets + 14) * 8;
int rc = ble_gap_set_data_len(m_conn_id, tx_octets, tx_time);
if(rc != 0) {
NIMBLE_LOGE(LOG_TAG, "Set data length error: %d, %s", rc, NimBLEUtils::returnCodeToString(rc));
}
} // setDataLen
/** /**
* @brief Get detailed information about the current peer connection. * @brief Get detailed information about the current peer connection.
*/ */

View file

@ -68,6 +68,7 @@ public:
uint16_t scanInterval=16, uint16_t scanWindow=16); uint16_t scanInterval=16, uint16_t scanWindow=16);
void updateConnParams(uint16_t minInterval, uint16_t maxInterval, void updateConnParams(uint16_t minInterval, uint16_t maxInterval,
uint16_t latency, uint16_t timeout); uint16_t latency, uint16_t timeout);
void setDataLen(uint16_t tx_octets);
void discoverAttributes(); void discoverAttributes();
NimBLEConnInfo getConnInfo(); NimBLEConnInfo getConnInfo();

View file

@ -735,7 +735,7 @@ void NimBLEServer::startAdvertising() {
*/ */
void NimBLEServer::stopAdvertising() { void NimBLEServer::stopAdvertising() {
NimBLEDevice::stopAdvertising(); NimBLEDevice::stopAdvertising();
} // startAdvertising } // stopAdvertising
/** /**
@ -773,7 +773,26 @@ void NimBLEServer::updateConnParams(uint16_t conn_handle,
if(rc != 0) { if(rc != 0) {
NIMBLE_LOGE(LOG_TAG, "Update params error: %d, %s", rc, NimBLEUtils::returnCodeToString(rc)); NIMBLE_LOGE(LOG_TAG, "Update params error: %d, %s", rc, NimBLEUtils::returnCodeToString(rc));
} }
}// updateConnParams } // updateConnParams
/**
* @brief Request an update of the data packet length.
* * Can only be used after a connection has been established.
* @details Sends a data length update request to the peer.
* The Data Length Extension (DLE) allows to increase the Data Channel Payload from 27 bytes to up to 251 bytes.
* The peer needs to support the Bluetooth 4.2 specifications, to be capable of DLE.
* @param [in] conn_handle The connection handle of the peer to send the request to.
* @param [in] tx_octets The preferred number of payload octets to use (Range 0x001B-0x00FB).
*/
void NimBLEServer::setDataLen(uint16_t conn_handle, uint16_t tx_octets) {
uint16_t tx_time = (tx_octets + 14) * 8;
int rc = ble_gap_set_data_len(conn_handle, tx_octets, tx_time);
if(rc != 0) {
NIMBLE_LOGE(LOG_TAG, "Set data length error: %d, %s", rc, NimBLEUtils::returnCodeToString(rc));
}
} // setDataLen
bool NimBLEServer::setIndicateWait(uint16_t conn_handle) { bool NimBLEServer::setIndicateWait(uint16_t conn_handle) {

View file

@ -59,6 +59,7 @@ public:
void updateConnParams(uint16_t conn_handle, void updateConnParams(uint16_t conn_handle,
uint16_t minInterval, uint16_t maxInterval, uint16_t minInterval, uint16_t maxInterval,
uint16_t latency, uint16_t timeout); uint16_t latency, uint16_t timeout);
void setDataLen(uint16_t conn_handle, uint16_t tx_octets);
uint16_t getPeerMTU(uint16_t conn_id); uint16_t getPeerMTU(uint16_t conn_id);
std::vector<uint16_t> getPeerDevices(); std::vector<uint16_t> getPeerDevices();
NimBLEConnInfo getPeerInfo(size_t index); NimBLEConnInfo getPeerInfo(size_t index);