Skip to content

Commit cd4f1c2

Browse files
Soraya Ormazabaldeadprogram
authored andcommitted
Add WinRT handler for Bluetooth LE connection status changes
This PR registers a `ConnectionStatusChanged` WinRT event handler on `BluetoothLEDevice` to detect abrupt disconnections. The handler resolves the current connection state and forwards it to `a.connectHandler`. The event token and handler are stored to allow proper unregistration during device teardown.
1 parent c5ddcf8 commit cd4f1c2

File tree

1 file changed

+40
-8
lines changed

1 file changed

+40
-8
lines changed

gap_windows.go

Lines changed: 40 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -289,8 +289,10 @@ type Device struct {
289289

290290
Address Address // the MAC address of the device
291291

292-
device *bluetooth.BluetoothLEDevice
293-
session *genericattributeprofile.GattSession
292+
device *bluetooth.BluetoothLEDevice
293+
session *genericattributeprofile.GattSession
294+
connectionStatusListenerToken foundation.EventRegistrationToken
295+
connectionStatusListener *foundation.TypedEventHandler
294296
}
295297

296298
// Connect starts a connection attempt to the given peripheral device address.
@@ -367,8 +369,36 @@ func (a *Adapter) Connect(address Address, params ConnectionParams) (Device, err
367369
session: newSession,
368370
}
369371

370-
if a.connectHandler != nil {
371-
a.connectHandler(device, true)
372+
// https://learn.microsoft.com/es-es/uwp/api/windows.devices.bluetooth.bluetoothledevice.connectionstatuschanged?view=winrt-26100
373+
// TypedEventHandler<BluetoothLEDevice,object>
374+
connectionStatusChangedGUID := winrt.ParameterizedInstanceGUID(
375+
foundation.GUIDTypedEventHandler,
376+
bluetooth.SignatureBluetoothLEDevice,
377+
"cinterface(IInspectable)", // object
378+
)
379+
380+
handler := foundation.NewTypedEventHandler(ole.NewGUID(connectionStatusChangedGUID), func(instance *foundation.TypedEventHandler, sender, arg unsafe.Pointer) {
381+
status, err := bleDevice.GetConnectionStatus()
382+
if err != nil {
383+
return
384+
}
385+
if status == bluetooth.BluetoothConnectionStatusDisconnected {
386+
device.Disconnect()
387+
}
388+
389+
if a.connectHandler != nil {
390+
a.connectHandler(device, status == bluetooth.BluetoothConnectionStatusConnected)
391+
}
392+
})
393+
394+
token, err := device.device.AddConnectionStatusChanged(handler)
395+
396+
device.connectionStatusListenerToken = token
397+
device.connectionStatusListener = handler
398+
399+
if err != nil {
400+
_ = handler.Release()
401+
return device, err
372402
}
373403

374404
return device, nil
@@ -379,20 +409,22 @@ func (a *Adapter) Connect(address Address, params ConnectionParams) (Device, err
379409
func (d Device) Disconnect() error {
380410
defer d.device.Release()
381411
defer d.session.Release()
412+
if d.connectionStatusListener != nil {
413+
defer d.connectionStatusListener.Release()
414+
}
382415

383416
d.cancel()
384417

385418
if err := d.session.Close(); err != nil {
386419
return err
387420
}
421+
422+
_ = d.device.RemoveConnectionStatusChanged(d.connectionStatusListenerToken)
423+
388424
if err := d.device.Close(); err != nil {
389425
return err
390426
}
391427

392-
if DefaultAdapter.connectHandler != nil {
393-
DefaultAdapter.connectHandler(d, false)
394-
}
395-
396428
return nil
397429
}
398430

0 commit comments

Comments
 (0)