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) + } +}