From ba8f2f07686d4e62ef739ef4628d0dc3a75c922f Mon Sep 17 00:00:00 2001 From: Alexis Couvreur Date: Sat, 17 Oct 2026 16:34:06 -0400 Subject: [PATCH] windows: call the connect handler when disconnecting Disconnect removed the connection status listener before the link dropped, so the handler never heard about a disconnect this side asked for. The other platforms call it. The handler is now called through a state that reports each change once. Disconnect reports the link down after it removes the listener, and Connect reports it up once it is, in case the link came up before the listener was added. A disconnect after a remote one, or after a Connect that timed out, is not reported again. --- gap_windows.go | 28 ++++++++++++++++++++++------ gap_windows_test.go | 16 ++++++++++++++++ 2 files changed, 38 insertions(+), 6 deletions(-) diff --git a/gap_windows.go b/gap_windows.go index b95a75c9..b1088e77 100644 --- a/gap_windows.go +++ b/gap_windows.go @@ -6,6 +6,7 @@ import ( "fmt" "slices" "sync" + "sync/atomic" "syscall" "time" "unsafe" @@ -453,6 +454,7 @@ type Device struct { connectionStatusListener *foundation.TypedEventHandler connParams *connectionParamsState services *openServices + link *linkState } var errDeviceNotConnected = errors.New("bluetooth: device is not connected") @@ -503,6 +505,18 @@ func (s *openServices) close() { } } +// linkState calls the connect handler once for each change of the link. +type linkState struct { + connected atomic.Bool + handler func(device Device, connected bool) +} + +func (s *linkState) report(d Device, connected bool) { + if s.connected.CompareAndSwap(!connected, connected) && s.handler != nil { + s.handler(d, connected) + } +} + // Connect starts a connection attempt to the given peripheral device address. // // On Linux and Windows, the IsRandom part of the address is ignored. @@ -583,6 +597,7 @@ func (a *Adapter) Connect(address Address, params ConnectionParams) (Device, err session: newSession, connParams: &connectionParamsState{}, services: &openServices{}, + link: &linkState{handler: a.connectHandler}, } // https://learn.microsoft.com/es-es/uwp/api/windows.devices.bluetooth.bluetoothledevice.connectionstatuschanged?view=winrt-26100 @@ -606,9 +621,7 @@ func (a *Adapter) Connect(address Address, params ConnectionParams) (Device, err default: } } - if a.connectHandler != nil { - a.connectHandler(device, status == bluetooth.BluetoothConnectionStatusConnected) - } + device.link.report(device, status == bluetooth.BluetoothConnectionStatusConnected) }) token, err := device.device.AddConnectionStatusChanged(handler) @@ -631,6 +644,8 @@ func (a *Adapter) Connect(address Address, params ConnectionParams) (Device, err _ = device.Disconnect() return Device{}, err } + // The link can come up before the listener is added, which misses its event. + device.link.report(device, true) // WinRT has no priority at connection time. The connection stays if this // fails, as on the platforms that have no priority. @@ -673,12 +688,13 @@ func (d Device) Disconnect() error { d.connParams.close() } - if err := d.session.Close(); err != nil { + err := d.session.Close() + _ = d.device.RemoveConnectionStatusChanged(d.connectionStatusListenerToken) + d.link.report(d, false) + if err != nil { return err } - _ = d.device.RemoveConnectionStatusChanged(d.connectionStatusListenerToken) - if err := d.device.Close(); err != nil { return err } diff --git a/gap_windows_test.go b/gap_windows_test.go index dc7e4de4..8f5b7ed5 100644 --- a/gap_windows_test.go +++ b/gap_windows_test.go @@ -174,3 +174,19 @@ func TestWaitConnected(t *testing.T) { t.Errorf("never connected returned after %s, want the timeout", d) } } + +func TestLinkStateReportsChanges(t *testing.T) { + var got []bool + s := &linkState{handler: func(d Device, connected bool) { got = append(got, connected) }} + + s.report(Device{}, false) // a failed Connect never reported true + s.report(Device{}, true) + s.report(Device{}, true) // Connect after the status change + s.report(Device{}, false) + s.report(Device{}, false) // Disconnect after a remote disconnect + s.report(Device{}, true) // Windows reconnects while the session is open + + if want := []bool{true, false, true}; !reflect.DeepEqual(got, want) { + t.Errorf("handler got %v, want %v", got, want) + } +}