简体   繁体   English

C++/WinRT GetGattServicesAsync() 在 MFC 应用程序中没有响应

[英]C++/WinRT GetGattServicesAsync() not responding in MFC app

C++, WinRT, VS2017 MFC, Win10 C++,WinRT,VS2017 MFC,Win10

I have a C++/WinRT VS2017 console app as a test platform to find my Bluetooth LE device, enumerate the services and characteristics, and then write a value to the Tx characteristic, etc. I have all of that working and now I am trying to move that code to an existing VS2017 MFC app.我有一个 C++/WinRT VS2017 控制台应用程序作为测试平台来查找我的蓝牙 LE 设备,枚举服务和特征,然后将值写入 Tx 特征等。我已经完成了所有这些工作,现在我正在尝试将该代码移至现有的 VS2017 MFC 应用程序。

In the console app the BluetoothLEAdvertisementWatcher and callback to the watcher.Received() were done in the main.cpp.在控制台应用程序中,BluetoothLEAdvertisementWatcher 和对 watcher.Received() 的回调在 main.cpp 中完成。 Once my BLE device was found, a separate function was called to create the device object from the deviceAddress and then enumerate the services and characteristics.找到我的 BLE 设备后,将调用单独的 function 从 deviceAddress 创建设备 object,然后枚举服务和特征。

In the MFC app I created a separate function run in a separate thread to establish the watcher and attach the callback.在 MFC 应用程序中,我创建了一个单独的 function 在单独的线程中运行以建立观察者并附加回调。 That all works fine up to the point that it needs to GetGattServicesAsync().这一切都很好,直到它需要 GetGattServicesAsync()。 In the console app the function OpenDevice() used to create the device object and get the services was done with a get() as in:在控制台应用程序中,function OpenDevice() 用于创建设备 object 并使用 get() 完成服务,如下所示:

OpenDevice(deviceAddress).get(); 

The first thing OpenDevice() does is create the device object using OpenDevice() 做的第一件事是使用 object 创建设备

auto device = co_await BluetoothLEDevice::FromBluetoothAddressAsync(deviceAddress);

If the device object is created, then the next thing it did was get the Services with:如果设备 object 已创建,那么它所做的下一件事就是通过以下方式获取服务:

auto services = co_await device.GetGattServicesAsync();

Here is where my MFC code fails.这是我的 MFC 代码失败的地方。 In the function thread that creates the watcher and watcher.Received callback my MFC code does the same call to OpenDevice().在创建 watcher 和 watcher.Received 回调的 function 线程中,我的 MFC 代码对 OpenDevice() 执行相同的调用。 In OpenDevice the device object is indeed created but then the call to GetGattServicesAsync() will never finish so matter how long I wait.在 OpenDevice 中,确实创建了设备 object,但是无论我等待多长时间,对 GetGattServicesAsync() 的调用都将永远不会完成。 If I enter the GetGattServicesAsync() in Debug mode, however, then it works fine.但是,如果我在调试模式下输入 GetGattServicesAsync(),那么它工作正常。

For testing I have also put the OpenDevice() code within the watcher thread but, again, it stalls on GetGattServicesAsync().为了进行测试,我还将 OpenDevice() 代码放在了观察者线程中,但它再次停在 GetGattServicesAsync() 上。 In this case, however, I cannot use the co_await but had to use但是,在这种情况下,我不能使用 co_await 但必须使用

auto services = device.GetGattServicesAsync().get();

Regardless, the GetGattServicesAsync() never finished.无论如何,GetGattServicesAsync() 从未完成。

Any suggestions of what I need to do or what I am doing wrong?关于我需要做什么或我做错了什么的任何建议?

Since running in Debug was working I was trying to solve why this was not working in Release mode.由于在 Debug 中运行是有效的,我试图解决为什么它在 Release 模式下不起作用。 I was relying on my log file entries to track the progress after the fact.事后我依靠我的日志文件条目来跟踪进度。 For the halibut I put in some Beeps() to let me hear the progress and I was hearing beeps but not seeing log entries after that GetGattServicesAsync().对于大比目鱼,我输入了一些 Beeps() 来让我听到进度,我听到了哔声,但在 GetGattServicesAsync() 之后没有看到日志条目。 Apparently what was happening is that, for what ever reason, the GetGattServicesAsync() was NULLing out the handle to my log file so no writes were taking place.显然发生的事情是,无论出于何种原因,GetGattServicesAsync() 将我的日志文件的句柄清空,因此没有发生写入。 I did a check for NULL after the GetGattServicesAsync() and, if it was, re-established the log file.我在 GetGattServicesAsync() 之后检查了 NULL,如果是,则重新建立日志文件。 All of a sudden my log file was showing the progress that I thought was not happening.突然间,我的日志文件显示了我认为没有发生的进度。 Sometime I think it is just me...:-(有时我认为这只是我...... :-(

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

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