From 6a591e8bab8ba6a396d226c2622f8da0f1724a8c Mon Sep 17 00:00:00 2001 From: Claude Date: Wed, 17 Jun 2026 19:39:27 +0000 Subject: [PATCH] fix(win): maintain the GATT session to stabilize MTU negotiation Set GattSession.MaintainConnection(true) after creating the session so Windows establishes the physical BLE connection up front. On Windows, BluetoothLEDevice::FromBluetoothAddressAsync returns before the link is established. Without MaintainConnection the first GATT operation (GetGattServicesAsync) triggers the connection, but by then the MTU is still at the default 23. If the peripheral negotiates a larger MTU during service discovery, the race can exhaust the peripheral's ATT buffer pool. MaintainConnection defaults to false and the session is released in PeripheralWinrt::Disconnect(), so the link is not kept open after an intentional disconnect. This carries the same change as #82 by @ranbochen, re-applied on a branch in this repository so CI can run on it. Co-authored-by: ranbochen <5681057+ranbochen@users.noreply.github.com> https://claude.ai/code/session_013BTp5XgwGPsnpgo8V1vdn7 --- lib/win/src/ble_manager.cc | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/lib/win/src/ble_manager.cc b/lib/win/src/ble_manager.cc index dc01c375..5b7fd268 100644 --- a/lib/win/src/ble_manager.cc +++ b/lib/win/src/ble_manager.cc @@ -362,6 +362,16 @@ void BLEManager::OnGattSessionCreated(IAsyncOperation asyncOp, Asyn peripheral.gattSession = session; auto token = session.MaxPduSizeChanged(onPduSizeChanged); peripheral.maxPduSizeChangedToken = token; + + // Proactively maintain the GATT session so Windows establishes the + // physical connection up front. FromBluetoothAddressAsync returns + // before the link is up; without this the first GATT operation + // triggers the connection while the MTU is still the default 23, + // racing the peripheral's MTU exchange during service discovery. + // MaintainConnection defaults to false; the session is released in + // PeripheralWinrt::Disconnect(), so this does not keep the link open + // after an intentional disconnect. + session.MaintainConnection(true); } else {