Repository navigation
Conversation
The connect handler now takes a ConnectionState instead of a bool. 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.
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
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.
The connect handler changes from
func(device Device, connected bool)tofunc(device Device, state ConnectionState).This breaks every program that sets a connect handler. The fix in each one is to use
state.Connectedinstead ofconnected.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, with the codes from Core Specification v5.4, Vol 1, Part F, Table 1.1.Follow-ups
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/wasmConnected: truewith a nilErrHow to verify
The reason path is not tested on hardware yet.
nRF52:
tinygo flash -target=pca10056-s140v7 -monitor ./examples/advertisement.reason: HCI error 0x13 (Remote User Terminated Connection).reason: HCI error 0x08 (Connection Timeout).macOS: connect to a peripheral from a program with a connect handler, then power off the peripheral. Expect a non-nil
Err. CallDisconnectinstead and expect a nilErr.