From 828bf8ec39f08ecb6a46a907fba12e82e3558ab8 Mon Sep 17 00:00:00 2001
From: Eric Albers <ealbers@syrdec.com>
Date: Thu, 1 Apr 2021 09:08:03 -0400
Subject: [PATCH 1/2] Add setMTU function to BLEClient.cpp/.h

---
 libraries/BLE/src/BLEClient.cpp | 33 +++++++++++++++++++++++++++++++++
 libraries/BLE/src/BLEClient.h   |  3 ++-
 2 files changed, 35 insertions(+), 1 deletion(-)

diff --git a/libraries/BLE/src/BLEClient.cpp b/libraries/BLE/src/BLEClient.cpp
index c056f255656..e729606c315 100644
--- a/libraries/BLE/src/BLEClient.cpp
+++ b/libraries/BLE/src/BLEClient.cpp
@@ -545,6 +545,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.
diff --git a/libraries/BLE/src/BLEClient.h b/libraries/BLE/src/BLEClient.h
index 75a288e4329..1eb619e9ac3 100644
--- a/libraries/BLE/src/BLEClient.h
+++ b/libraries/BLE/src/BLEClient.h
@@ -57,7 +57,8 @@ class BLEClient {
 	uint16_t                                   getConnId();
 	esp_gatt_if_t                              getGattcIf();
 	uint16_t								   getMTU();
-
+	bool 											 setMTU(uint16_t mtu);
+	
 uint16_t m_appId;
 private:
 	friend class BLEDevice;

From bbdc86cbcbe08e69f0eaa0818d4a97aa93569681 Mon Sep 17 00:00:00 2001
From: Eric Albers <ealbers@syrdec.com>
Date: Thu, 1 Apr 2021 09:32:41 -0400
Subject: [PATCH 2/2] add missing include

---
 libraries/BLE/src/BLEClient.cpp | 1 +
 1 file changed, 1 insertion(+)

diff --git a/libraries/BLE/src/BLEClient.cpp b/libraries/BLE/src/BLEClient.cpp
index e729606c315..8f41d73fe9d 100644
--- a/libraries/BLE/src/BLEClient.cpp
+++ b/libraries/BLE/src/BLEClient.cpp
@@ -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"