Repository navigation
feat: add SetConnectionStateHandler with a disconnect reason - #518
Merged
Merged
Conversation
acouvreur
requested review from
deadprogram
and removed request for
deadprogram
October 17, 2026 20:13
acouvreur
marked this pull request as ready for review
October 17, 2026 20:31
SetConnectionStateHandler takes a handler that gets a ConnectionState. It holds whether the device is connected and, when it disconnected, the reason. Nordic and HCI adapters report the HCI reason code as a DisconnectReason. macOS passes the CoreBluetooth error. The reason is nil when this side ended the connection, and on Windows, Linux and the web, which give none. SetConnectHandler keeps its signature and is deprecated, so existing programs still build. go fix rewrites its callers, and a nil handler stays a no-op.
acouvreur
force-pushed
the
connection-state
branch
from
October 27, 2026 17:38
f18fd02 to
dc51810
Compare
Member
|
Thanks, this looks good.
|
DisconnectReason and its codes now live in ble with aliases in the root package, like AttributeProtocolError.
deadprogram
approved these changes
Oct 27, 2026
deadprogram
left a comment
Member
There was a problem hiding this comment.
Looking good now @acouvreur thanks for keeping backwards compat now squash/merging.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Replaces #444. It follows the suggestion on #443 to pass a struct instead of a new argument.
This adds
SetConnectionStateHandler, whose handler gets aConnectionStateinstead of a bool.SetConnectHandlerkeeps its signature, so existing programs still build. It is deprecated and marked//go:fix inline, sogo fix ./...rewrites callers to the new method. The rewrite needs Go 1.26 or later, wherego fixruns the inline analyzer. Older Go versions still build the code but do not rewrite it.The nil check keeps
SetConnectHandler(nil)a no-op, which some programs use to clear the handler.BLEAdaptergetsSetConnectionStateHandlerand keepsSetConnectHandler.Errsays why the device disconnected. It is nil when the device connected, when this side ended the connection, or when the platform gives no reason.Erron disconnectDisconnectReason, the HCI code ofBLE_GAP_EVT_DISCONNECTEDDisconnectReason, from the Disconnection Complete eventdidDisconnectPeripheralA caller can check the code with
errors.As.Why this shape
DNSDoneInfoandWroteRequestInfo. grpc passesSubConnStateto its state listener, with the state and aConnectionErrorthat is nil unless the connection failed.http.Server.ConnState func(net.Conn, ConnState), AndroidonConnectionStateChange(gatt, status, newState)and Nordic'sonDeviceDisconnected(device, reason).Erris nil when this side ended the connection, as in CoreBluetoothdidDisconnectPeripheral. Sostate.Err != nilmeans the same on every platform. Nordic and HCI report reason 0x16 in that case, which becomes nil.REASON_TIMEOUTandREASON_TERMINATE_PEER_USER, or Kable'sState.Disconnected(status).DisconnectReasonfollowsAttributeProtocolError. It lives inble/with an alias in the root package, and has the codes from Core Specification v5.4, Vol 1, Part F, Table 1.1.//go:fix inlineletsgo fixmove them over.Next steps
On Linux, BlueZ 5.83 added a
Disconnected(reason, message)signal toorg.bluez.Device1(bluez@8b08f90). A follow-up can use it when it is there.Tests
go testandgo veton Windowsgo vetand the changed examples for Linux and js/wasmgo fixon a program that callsSetConnectHandlerwith a func literal, a named func, a variable and nilConnected: truewith a nilErrHow to verify
The reason path is not tested on hardware yet.
On nRF52
tinygo flash -target=pca10056-s140v7 -monitor ./examples/advertisement.reason: HCI error 0x13 (Remote User Terminated Connection).reason: HCI error 0x08 (Connection Timeout).On macOS, connect to a peripheral from a program with a connection state handler, then power off the peripheral. Expect a non-nil
Err. CallDisconnectinstead and expect a nilErr.