Skip to content

fix(win): maintain the GATT session to stabilize MTU negotiation#88

Open
stoprocent wants to merge 1 commit into
mainfrom
claude/win-maintain-connection
Open

fix(win): maintain the GATT session to stabilize MTU negotiation#88
stoprocent wants to merge 1 commit into
mainfrom
claude/win-maintain-connection

Conversation

@stoprocent

Copy link
Copy Markdown
Owner

Summary

Sets GattSession.MaintainConnection(true) right after the session is created in OnGattSessionCreated, so Windows establishes the physical BLE connection up front instead of lazily on the first GATT operation.

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 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() (gattSession->Close()), so this does not keep the link open after an intentional disconnect.

Verified against Microsoft's documentation

  • GattSession.MaintainConnection — read/write Boolean, default false; when true "the system waits indefinitely for a connection, and it will connect when the device is available."
  • Bluetooth GATT Client guide — "To initiate a connection, you must set GattSession.MaintainConnection to true, or call an uncached service discovery method … or perform a read/write operation." This confirms the lazy-connect behavior the change addresses.

Why this PR exists

This is the same change as #82 by @ranbochen — full credit to them (preserved as co-author on the commit). That PR is a first-time-contributor fork PR, and for some reason its CI never ran: the workflow runs queued at open time were gated for approval and have since aged out, leaving 0 checks and no way to re-trigger them from that PR. Re-applying the change on a branch in this repository lets the build/test/lint workflows run normally and go through the standard review gates.

Supersedes #82.

🤖 Generated with Claude Code

https://claude.ai/code/session_013BTp5XgwGPsnpgo8V1vdn7


Generated by Claude Code

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants