Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 22 additions & 6 deletions gap_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"slices"
"sync"
"sync/atomic"
"syscall"
"time"
"unsafe"
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -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.
Expand Down Expand Up @@ -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
}
Expand Down
16 changes: 16 additions & 0 deletions gap_windows_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Loading