Skip to content

feat(windows): use ConnectionPriority for preferred connection parameters - #473

Open
acouvreur wants to merge 1 commit into
feat/connection-priorityfrom
feat/connection-priority-windows
Open

feat(windows): use ConnectionPriority for preferred connection parameters#473
acouvreur wants to merge 1 commit into
feat/connection-priorityfrom
feat/connection-priority-windows

Conversation

@acouvreur

@acouvreur acouvreur commented Sep 2, 2026

Copy link
Copy Markdown
Member

This PR is on top of #472. The difference shows only the Windows change. It replaces #423.

What this does

It sends ConnectionParams.Priority to the three WinRT presets.

ConnectionPriority BluetoothLEPreferredConnectionParameters
Throughput ThroughputOptimized
Balanced Balanced
PowerSaving PowerOptimized

Windows ignores MinInterval, MaxInterval and Timeout, and the doc comment tells the user this. There is no method to obey them, because BluetoothLEPreferredConnectionParameters has no public constructor. An unset Priority keeps the open request.

The request is an object with a life cycle

Please look at this part with care. RequestPreferredConnectionParameters returns a BluetoothLEPreferredConnectionParametersRequest. This object has an IClosable interface, and Windows applies the request only while the object is open. Microsoft tells the user to restore the defaults with a close of the BluetoothLEDevice, or with a new request.

https://learn.microsoft.com/en-us/uwp/api/windows.devices.bluetooth.bluetoothledevice.requestpreferredconnectionparameters

PR #423 discarded the object with _, err = …. This causes a leak of a COM reference, and the garbage collector then controls the life of the request. This PR does the following instead.

  • The device keeps the request behind a pointer. Device is a value type, thus all copies must share the same object.
  • A new request closes and releases the previous request.
  • Disconnect closes the request while the device is still open.
  • If the device does not keep a request, the code closes it immediately.

The mutex is necessary because two goroutines can call RequestConnectionParams on copies of one Device.

The code reads the status

A success status shows only that the system accepted the request. DeviceNotAvailable and AccessDenied were not visible before. The code now returns an error with the name of the status.

The agreed parameters come later, in the ConnectionParametersChanged event. This PR does not read that event. A Device.ConnectionParams() function is a better interface for this, and the other platforms need the same function.

Requirements

These functions need Windows 11 build 22000 or later. On Windows 10 the activation factory does not exist. The error message gives the necessary build number.

This PR moves winrt-go to v0.0.0-20260513072510-45f10383b2b8 for the new bindings.

Tests

go build, go vet and go test are satisfactory. golangci-lint finds no new problems.

I did not test this on real hardware yet. Before you merge this PR, I want to make sure on a Windows 11 machine that a Throughput request changes the connection interval.

@acouvreur
acouvreur force-pushed the feat/connection-priority branch from 7d5e8ad to ed29352 Compare September 2, 2026 19:10
@acouvreur
acouvreur force-pushed the feat/connection-priority-windows branch from 388b58a to 512baa4 Compare September 2, 2026 19:10
@acouvreur acouvreur changed the title feat(windows): honour ConnectionPriority via preferred connection parameters feat(windows): use ConnectionPriority for preferred connection parameters Sep 2, 2026
…ters

Windows does not accept explicit connection parameters. It has only three
presets, and ConnectionPriority maps to them. Thus MinInterval, MaxInterval
and Timeout have no effect on this platform.

Windows applies a request only while the request object is open. If you
close the object, or release the BluetoothLEDevice, Windows restores the
system defaults. Therefore the device keeps the request, closes the previous
request when you make a new one, and closes the last request on Disconnect.
Device is a value type, thus the request is behind a pointer.

The code also reads the request status. A success status shows only that the
system accepted the request.

These functions need Windows 11 build 22000 or later.

https://learn.microsoft.com/en-us/uwp/api/windows.devices.bluetooth.bluetoothledevice.requestpreferredconnectionparameters
@acouvreur
acouvreur force-pushed the feat/connection-priority branch from ed29352 to 826dd9a Compare September 2, 2026 19:15
@acouvreur
acouvreur force-pushed the feat/connection-priority-windows branch from 512baa4 to 594f102 Compare September 2, 2026 19:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant