如何在桌面.NET应用程序中获取BLE广告回调? [英] How do you get a BLE Advertisment callback in a desktop .NET app?

查看:76
本文介绍了如何在桌面.NET应用程序中获取BLE广告回调?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从.NET Windows桌面应用程序获取BLE广告回调.我可以调用API,但是从不从WPF或命令行应用程序中的OS调用Received事件.当从VS2015或通用Windows应用程序中的单元测试中调用代码时,我确实会收到事件.但是,我需要做其他事情在通用Windows中不可用.这是代码:

I am trying to get a BLE advertisement callback from a .NET Windows desktop application. I am able to call into the APIs, however the Received event is never called from the OS in a WPF or Command line application. I do get events when the code is called from a Unit Test in VS2015, or universal Windows app. However, I need to do other things not available in universal Windows. Here is the code:

    using Windows.Devices.Bluetooth.Advertisement;
    public static void ScanForAdvertisments()
    {
        mWatcher = new BluetoothLEAdvertisementWatcher();           
        mWatcher.Received += OnAdvertismentReceived;
        mWatcher.ScanningMode = BluetoothLEScanningMode.Active;
        mWatcher.Start();
    }

    public static void OnAdvertismentReceived(BluetoothLEAdvertisementWatcher sender, BluetoothLEAdvertisementReceivedEventArgs args)
    {

        ulong address = args.BluetoothAddress;
        short rssi = args.RawSignalStrengthInDBm;
    }

  • 我已引用Windows.winmd以便包含BLE/UWP名称空间.
  • 我在Windows 10周年纪念版上使用的框架版本为4.6.1,在以前的Windows版本中,我也遇到了同样的问题.
  • 使用UWP应用程序,我在appxmanifest中设置了蓝牙的DeviceCapability.单元测试项目似乎不需要这样做,而且我不确定如何将appxmanifests应用于桌面项目.
  • status属性报告扫描仪正在运行,并且停止事件也绝不会触发.
    • I've referenced Windows.winmd in order to include the BLE/UWP namespaces.
    • I am using framework version 4.6.1 on Windows 10 Anniversary Edition, I had the same problem on the previous windows build as well.
    • Using a UWP app I set the DeviceCapability for bluetooth in the appxmanifest. The unit test project seems not to require this and I'm not sure how to apply appxmanifests to desktop projects.
    • The status property reports the scanner is running and the stopped event is also never triggered.
    • 无论如何,这是Windows错误吗?有没有解决方法?我尝试研究应用程序服务,但似乎无法很好地调用它们,因此无法从UWP调用桌面代码进行部署.谢谢.

      Anyway, is this a Windows bug? Is there a way to workaround it? I tried looking into app services but they seem not to be set up to call into desktop code very well and calling desktop code from UWP seems complex to deploy. Thanks.

      推荐答案

      只需使用Console .NET 4.6.1应用程序进行尝试,广告回调就会通过.我之前写了一个 GitHub示例,这可能会有所帮助.它们使WinRT API正常工作的关键是两个参考:

      Just tried this out with a Console .NET 4.6.1 app and the advertisement callback is coming through. I wrote up a GitHub Sample of this a bit ago, which might help out. They key for getting WinRT APIs working are two references:

      要使用WinRT API,请添加两个引用:

      To use the WinRT APIs, add two references:

      1. C:\ Program Files(x86)\ Windows Kits \ 10 \ UnionMetadata \ Windows.winmd

      1. C:\Program Files (x86)\Windows Kits\10\UnionMetadata\Windows.winmd

      C:\ Program Files(x86)\ Reference Assemblies \ Microsoft \ Framework.NETCore \ v4.5 \ System.Runtime.WindowsRuntime.dll

      C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework.NETCore\v4.5\System.Runtime.WindowsRuntime.dll

      听起来您可能缺少第二个参考.我能够将v4.5 .NetCore框架与4.6.1一起使用.

      It sounds like you may be missing the second reference. I was able to use the v4.5 .NetCore framework with 4.6.1.

      这篇关于如何在桌面.NET应用程序中获取BLE广告回调?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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