Add setMTU function to BLEClient.cpp/.h (#4999)

The current implementation has a getMTU function which returns the mtu sent in a message.

This function allows you to set the MTU value on the connected device, it first sets the MTU locally by calling esp_ble_gatt_set_local_mtu. It then calls esp_ble_gattc_send_mtu_req to have the connected device also change its MTU size.
This commit is contained in:
Eric Albers 2021-04-15 09:07:45 -04:00 committed by GitHub
parent f3dca15a6f
commit 7a4e7066f9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 36 additions and 1 deletions

View File

@ -10,6 +10,7 @@
#include <esp_bt_main.h>
#include <esp_gap_ble_api.h>
#include <esp_gattc_api.h>
#include <esp_gatt_common_api.h>// ESP32 BLE
#include "BLEClient.h"
#include "BLEUtils.h"
#include "BLEService.h"
@ -545,6 +546,39 @@ uint16_t BLEClient::getMTU() {
return m_mtu;
}
/**
@brief Set the local and remote MTU size.
Should be called once after client connects if MTU size needs to be changed.
@return bool indicating if MTU was successfully set locally and on remote.
*/
bool BLEClient::setMTU(uint16_t mtu)
{
esp_err_t err = esp_ble_gatt_set_local_mtu(mtu); //First must set local MTU value.
if (err == ESP_OK)
{
err = esp_ble_gattc_send_mtu_req(m_gattc_if,m_conn_id); //Once local is set successfully set remote size
if (err!=ESP_OK)
{
log_e("Error setting send MTU request MTU: %d err=%d", mtu,err);
return false;
}
}
else
{
log_e("can't set local mtu value: %d", mtu);
return false;
}
log_v("<< setLocalMTU");
m_mtu = mtu; //successfully changed
return true;
}
/**
* @brief Return a string representation of this client.
* @return A string representation of this client.

View File

@ -57,6 +57,7 @@ public:
uint16_t getConnId();
esp_gatt_if_t getGattcIf();
uint16_t getMTU();
bool setMTU(uint16_t mtu);
uint16_t m_appId;
private: