Skip to content

Commit 7a4e706

Browse files
authored
Add setMTU function to BLEClient.cpp/.h (espressif#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.
1 parent f3dca15 commit 7a4e706

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

libraries/BLE/src/BLEClient.cpp

+34
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <esp_bt_main.h>
1111
#include <esp_gap_ble_api.h>
1212
#include <esp_gattc_api.h>
13+
#include <esp_gatt_common_api.h>// ESP32 BLE
1314
#include "BLEClient.h"
1415
#include "BLEUtils.h"
1516
#include "BLEService.h"
@@ -545,6 +546,39 @@ uint16_t BLEClient::getMTU() {
545546
return m_mtu;
546547
}
547548

549+
550+
/**
551+
@brief Set the local and remote MTU size.
552+
Should be called once after client connects if MTU size needs to be changed.
553+
@return bool indicating if MTU was successfully set locally and on remote.
554+
*/
555+
bool BLEClient::setMTU(uint16_t mtu)
556+
{
557+
esp_err_t err = esp_ble_gatt_set_local_mtu(mtu); //First must set local MTU value.
558+
if (err == ESP_OK)
559+
{
560+
err = esp_ble_gattc_send_mtu_req(m_gattc_if,m_conn_id); //Once local is set successfully set remote size
561+
if (err!=ESP_OK)
562+
{
563+
log_e("Error setting send MTU request MTU: %d err=%d", mtu,err);
564+
return false;
565+
}
566+
}
567+
else
568+
{
569+
log_e("can't set local mtu value: %d", mtu);
570+
return false;
571+
}
572+
log_v("<< setLocalMTU");
573+
574+
m_mtu = mtu; //successfully changed
575+
576+
return true;
577+
}
578+
579+
580+
581+
548582
/**
549583
* @brief Return a string representation of this client.
550584
* @return A string representation of this client.

libraries/BLE/src/BLEClient.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,8 @@ class BLEClient {
5757
uint16_t getConnId();
5858
esp_gatt_if_t getGattcIf();
5959
uint16_t getMTU();
60-
60+
bool setMTU(uint16_t mtu);
61+
6162
uint16_t m_appId;
6263
private:
6364
friend class BLEDevice;

0 commit comments

Comments
 (0)