繁体   English   中英

BluetoothLEDevice::RequestPreferredConnectionParameters() 的语法

[英]Syntax for BluetoothLEDevice::RequestPreferredConnectionParameters()

使用 C++/WinRT、Win10、VS2019、SDK 10.0.22621.0、NuGet CppWinRT 2.0.220608.4

我正在尝试让RequestPreferredConnectionParameters工作。 在这一点上,我想知道我的语法是否错误,或者可能是我不知道的其他问题。 function 的 MS 文档在这里,各种参数的链接在这里

正如我所拥有的,pubDevice 是 BLE 设备 object 的命令行是:

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

顺便提一下,我可以跑

auto statusTest = co_await pubDevice.RequestAccessAsync();

之前RequestPreferredConnectionParameters没有问题所以,显然,设备 object 是好的,可以连接到。

正在发生的事情是这样的。 我有一个 function, OpenDevice(),它根据地址打开设备。 如果,在获得设备 object 后,我在 OpenDevice function 中发出上述命令,代码不会崩溃,但会立即跳转到 OpenDevice() ZC1C425268E68385D1AB5074C17 的末尾并绕过它下面的所有其他代码行之后就完全没有联系了。

如果我在 OpenDevice() function 之外运行RequestPreferredConnectionParameters ,则会出错,并An unhandled exception was encountered during a user callback并且引用的行位于base.h line 4942 if (result == impl::error_changed_state)

我假设所引用的回调是在 OpenDevice() 中设置的 Rx 特性 ValueChanged 回调。 所以我首先通过撤销回调来测试

pubRxCharacteristic.ValueChanged(etValueChangeToken);

然后运行RequestPreferredConnectionParameters但我仍然得到An unhandled exception错误。

我唯一拥有的其他回调是BluetoothLEAdvertisementWatcher advert 收到的回调,但在找到设备后已停止。

任何人都可以验证我的语法是否正确和/或对导致我的问题的原因有任何线索吗?

编辑以在控制台应用程序中显示更多代码------------
@IInspectable
再次记录:
使用 C++/WinRT、Win10、VS2019 - 控制台应用程序、SDK 10.0.22621.0、NuGet CppWinRT 2.0.220608.4

pch.h 文件中的相关内容包括:

// 2022/9/10 -- for WHCAR and apparently GUID
#include <Windows.h>
#include <tchar.h>

#include <winrt\Windows.Foundation.h>
#include <winrt\Windows.Storage.Streams.h>
#include <winrt\Windows.Devices.Bluetooth.h>
#include <winrt\Windows.Devices.Bluetooth.Advertisement.h>
#include <winrt\Windows.Devices.Bluetooth.GenericAttributeProfile.h>
// 2022/9/10
#include <winrt\Windows.Devices.Enumeration.h>
#include <winrt/Windows.Foundation.Collections.h>

Main.cpp 顶部的相关名称空间:

using namespace winrt;
using namespace Windows::Foundation;
using namespace winrt::Windows::Foundation;
using namespace Windows::Storage::Streams;
using namespace Windows::Devices::Bluetooth;
using namespace Windows::Foundation::Collections;
using namespace Windows::Devices::Bluetooth::Advertisement;
using namespace Windows::Devices::Bluetooth::GenericAttributeProfile;
// 2022/9/10 for RequestConnectionAsync
using namespace Windows::Devices::Enumeration;

我假设要监视和查找设备的代码在这里不相关。 不用说找到设备并将地址传递给 OpenDevice 以创建设备 object。

这是 OpenDevice 的顶部:

IAsyncAction OpenDevice(unsigned long long deviceAddress)
{
    auto device = co_await BluetoothLEDevice::FromBluetoothAddressAsync(deviceAddress);

    // 2022/9/10 test code
    auto statusTest = co_await device.RequestAccessAsync();
    // Allowed, DeniedBySystem, Unspecified
    if (statusTest != DeviceAccessStatus::Allowed) {            
        std::cout << "Access to device is not allowed...." << std::endl;
    }
    else {
        std::cout << "Access to device is allowed...." << std::endl;
    }

    // Next line ends without error but immediately goes to the end of OpenDevice()
    std::cout << "Asking for ThroughputOptimized...." << std::endl;
    auto statusConnection = device.RequestPreferredConnectionParameters(BluetoothLEPreferredConnectionParameters::ThroughputOptimized());
    std::cout << "Line after Request ThroughputOptimized...." << std::endl;
    Beep(500, 500);<br/> // function never gets to this cout or Beep<br/>
// More code follows to get Rx and TxCharacteristics etc.<br/>
} // end OpenDevice

这是控制台 output: 注意最后一个 cout 是Asking for ThroughputOptimized行。 Line after Request ThroughputOptimized没有 cout 并且没有Beep

尝试定位 TENS 设备:等待设备:已收到广告:
本地名称:[]
广告类型:[ConnectableUndirected]
蓝牙地址:[0x300000e59630]
RawSignalStrengthInDBm:[-60] 服务UUID:[0000fff0-0000-1000-8000-00805f9b34fb]
找到 TENS 设备主服务....
找到 TENS 设备。 允许访问设备....
请求吞吐量优化....

WinRTBle.exe(进程 15576)以代码 -1073740791 退出。
按任意键关闭此 window。

除非语法有问题或缺少 header。 我能想到的唯一另一件事是它需要Win11。 RequestPreferredConnectionParameters 方法的文档说

Windows 要求 器件系列 Windows 11(在 10.0.22000.0 中引入)

这是否意味着不管 SDK 需要 Win11 吗?

@IInspectable 尽管我对此感到恐惧,但显然答案是它需要 Win11。 上面在对原始问题的编辑中提到的控制台应用程序代码被编译成一个 exe。 我在 VM VirtualBox 上有 Win11,我在该 Win11 上运行了该 exe,代码继续通过有问题的ThroughputOptimized()行,并按预期完成了应用程序的 rest。 所以很惭愧。 我还没有真正的机器上的 Win11(叫我偏执狂),但我想我可以将 Win11 操作系统的代码括起来,并且只在用户碰巧运行 Win11 时运行它

总有东西.......

编辑 2....................... “总是有事” 立即出现。 据我所知,无法判断操作系统是 Win10 还是 Win11。 建议使用 RtlGetVersion,但 Win10 和 Win11 都返回 10。 另一张海报建议使用 System32 kernal32.dll 文件的文件版本,但他们也都报告了主要编号 10 和次要编号 0。添加版本清单的 MS 文档具有相同的两个操作系统的 UUID。

太荒谬了,MS 会想出一个 function,它只在 Win11 中运行,然后不必告诉你你在哪个操作系统中运行......Jeeeeeeze......

编辑 3.......................
说得太早了。 我在搞乱控制台应用程序并忘记了在我的 MFC 应用程序中我使用WMI IWbemClassObject Caption property来获取操作系统。
对于 Windows 10 我得到
微软 Windows 10 Pro
对于Win11,我得到
微软 Windows 11 首页
显然,这确实是唯一的方法。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM