Syntax for BluetoothLEDevice::RequestPreferredConnectionParameters()

Edmond Hardin 1 Reputation point
2022-09-09T18:22:47.347+00:00

Using C++/WinRT, Win10, VS2019, SDK 10.0.22621.0, NuGet CppWinRT 2.0.220608.4

I'm trying to get the RequestPreferredConnectionParameters to work. At this point I am wondering if maybe I have the syntax wrong or maybe something else about it that I am not aware of. The MS docs for the function are here and the link to the various parameters are here.

The command line, as I have it, with pubDevice being the BLE device object, is:

BluetoothLEPreferredConnectionParametersRequest rcoConnect = pubDevice.RequestPreferredConnectionParameters(BluetoothLEPreferredConnectionParameters::ThroughputOptimized());  

Just to mention, I am able to run

auto statusTest = co_await pubDevice.RequestAccessAsync();  

before the RequestPreferredConnectionParameters without problems so, obviously, the device object is good and can be connected to.

What is happening is this. I have a function, OpenDevice(), that opens the device based on the address. If, after getting the device object, I issue the command above while still in the OpenDevice function, the code will not crash but it will immediately jump to the end of the OpenDevice() function bypassing all other lines of code below it and there will be no connection at all after that.

If I run the RequestPreferredConnectionParameters outside of the OpenDevice() function it errors out with a
An unhandled exception was encountered during a user callback and the line referenced is in base.h line 4942 if (result == impl::error_changed_state)

I had assumed that the callback refered to was the Rx Characteristic ValueChanged Callback that is set in OpenDevice(). So I tested by first revoking that callback with

pubRxCharacteristic.ValueChanged(etValueChangeToken);  

and then running the RequestPreferredConnectionParameters but I still got the An unhandled exception error.

The only other callback that I have is the BluetoothLEAdvertisementWatcher advert received callback but that was stopped after the device was found.

Can anyone verify that my syntax seems correct and/or have any clue as to what is causing my problems?

Universal Windows Platform (UWP)
C++
C++
A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
3,526 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Roy Li - MSFT 31,766 Reputation points Microsoft Vendor
    2022-09-12T07:41:49.877+00:00

    Hello,

    Welcome to Microsoft Q&A!

    Can anyone verify that my syntax seems correct and/or have any clue as to what is causing my problems?

    I noticed that you are running in Windows 10. Based on the document that you posted: BluetoothLEPreferredConnectionParameters Class and BluetoothLEDevice.RequestPreferredConnectionParameters Method, both of them require Windows 11 (introduced in 10.0.22000.0). This should be the reason why you are getting this behavior. Please try to run your project in a Windows 11 Device. Please let me know if you still have questions about this.

    Thank you.


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments