我正在尝试从.NET Windows桌面应用程序获取BLE广告回调。我能够调用API,但是从未在WPF或命令行应用程序中从OS调用Received事件。当从VS2015或通用Windows应用程序中的单元测试调用代码时,我确实得到了事件。但是,我需要做其他通用Windows中没有的东西。这是代码:
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错误吗?有办法解决它吗?我试图查看应用程序服务,但似乎没有设置好很好地调用桌面代码,从UWP调用桌面代码似乎很难部署。感谢。
答案 0 :(得分:2)
刚刚使用Console .NET 4.6.1应用程序尝试了这一点,广告回调正在通过。我刚刚写了一篇GitHub Sample,这可能会有所帮助。获得WinRT API工作的关键是两个参考:
要使用WinRT API,请添加两个引用:
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
听起来你可能错过了第二个参考。我能够将v4.5 .NetCore框架与4.6.1一起使用。