用于蓝牙LE设备通知的cppwinrt [英] cppwinrt for Bluetooth LE Device Notifications

查看:74
本文介绍了用于蓝牙LE设备通知的cppwinrt的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,


我正在尝试使用cppwinrt从桌面非托管C ++应用程序与蓝牙LE设备通信。我已设法使用  BluetoothLEAdvertisementWatcher来发现设备并访问GATT服务中的值。但是,现在我正在尝试
来获取值更改的通知:

 characteristic.ValueChanged([&](GenericAttributeProfile :: GattCharacteristic特性,GenericAttributeProfile :: GattValueChangedEventArgs eventArgs){
std :: wclog<< L"收到通知!"<< std :: endl;
});

GenericAttributeProfile :: GattCommunicationStatus status = co_await characteristic.WriteClientCharacteristicConfigurationDescriptorAsync(GenericAttributeProfile :: GattClientCharacteristicConfigurationDescriptorValue :: Notify);
if(status!= GenericAttributeProfile :: GattCommunicationStatus :: Success){
std :: wclog<< L"设备有错误" << int(status)<< L"在尝试写入特征配置时" <<的std :: ENDL;
返回;
}

我收到的状态是成功,但我没有收到任何通知(即使设备不断改变值)。通过相同的调用,桌面C#应用程序的通知工作正常,但我需要在非托管C ++中使用它。
是否有人知道什么是错的?也许缺少某种事件循环存在问题?我不确定这个ValueChanged事件应该在哪个线程上被触发(我的std :: wclog可以安全地从那里的任何线程调用)。只需读取
,使用ReadValueAsync()的值就可以了。

解决方案

嗨anlumo,


感谢您在此处发帖。


>>通过相同的调用,桌面C#应用程序的通知正常,但我需要在非托管C ++中使用此功能。有人知道什么是错的吗?也许缺少某种事件循环存在问题?我不是
确定这个ValueChanged事件应该被触发到哪个线程(我的std :: wclog可以安全地从那里的任何线程调用)。只需使用ReadValueAsync()读取值就可以了。


如何在本机c ++代码中调用这些方法?


与您使用的WinRT GATT API相关的异步操作基于Windows运行时功能,用于UWP应用程序。在我看来,它可能需要一些.Net框架的支持。


如果你想使用它,也许你可以使用COM通过C ++ / CLI从托管DLL调用。


以下是一些文件。


https://msdn.microsoft.com/en-us/library/68td296t.aspx


https://msdn.microsoft.com/en-us/library/2x8kf7zx.aspx?f=255&MSPPError=-2147217396


如果您想了解有关UWP的更多信息,请发布在
UWP论坛
以获得更好的支持。


希望这可以为您提供帮助。


最诚挚的问候,


Sera Yu




Hello,

I'm trying to talk to a Bluetooth LE device from a desktop non-managed C++ application using cppwinrt. I've managed to use the BluetoothLEAdvertisementWatcher to discover devices and access the values in the GATT services. However, now I'm trying to get notifications for value changes:

characteristic.ValueChanged([&](GenericAttributeProfile::GattCharacteristic characteristic, GenericAttributeProfile::GattValueChangedEventArgs eventArgs) {
  std::wclog << L"Notification received!" << std::endl;
});

GenericAttributeProfile::GattCommunicationStatus status = co_await characteristic.WriteClientCharacteristicConfigurationDescriptorAsync(GenericAttributeProfile::GattClientCharacteristicConfigurationDescriptorValue::Notify);
if (status != GenericAttributeProfile::GattCommunicationStatus::Success) {
  std::wclog << L"Device had error " << int(status) << L" while trying to write to characteristic configuration" << std::endl;
  return;
}

The status I receive back is success, but I don't get any notifications (even though the device constantly changes the values). The notifications work fine from a desktop C# application with the same calls, but I need this in non-managed C++. Does anybody have an idea what's wrong? Maybe there's a problem with the lack of an event loop of some kind? I'm not sure on which thread this ValueChanged event is supposed to be fired on (my std::wclog is safe to be called from any thread there). Just reading the values with ReadValueAsync() works fine.

解决方案

Hi anlumo,

thanks for posting here.

>>The notifications work fine from a desktop C# application with the same calls, but I need this in non-managed C++. Does anybody have an idea what's wrong? Maybe there's a problem with the lack of an event loop of some kind? I'm not sure on which thread this ValueChanged event is supposed to be fired on (my std::wclog is safe to be called from any thread there). Just reading the values with ReadValueAsync() works fine.

How do you call these methods in your native c++ code?

The WinRT GATT API related Async operation you used is based on Windows Runtime feature, used in UWP applications. In my opinion, it may need some support with .Net framework.

If you want to use this, maybe you could use COM to call from a managed DLL with C++/CLI.

Here are some documents for you.

https://msdn.microsoft.com/en-us/library/68td296t.aspx

https://msdn.microsoft.com/en-us/library/2x8kf7zx.aspx?f=255&MSPPError=-2147217396

If you want to know more things about UWP, please post on UWP forum for better support.

Hope this could be help of you.

Best Regards,

Sera Yu


这篇关于用于蓝牙LE设备通知的cppwinrt的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆