簡體   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