我升级到 Windows 10,版本 1703 build 15063(创意者更新)正式版。当我在 WPF 桌面应用程序中运行以下代码时,BluetoothLEDevice.FromBluetoothAddressAsync 永远不会返回。
此代码在我的 Windows 10 更新(即之前的 1607 build 14393)之前运行良好。如果此代码在新的 Win 10 1703 中作为 UWP 运行,它也能正常工作。
BluetoothLEAdvertisementWatcher BleWatcher = null;
private void Button_Click(object sender, RoutedEventArgs e)
{
BleWatcher = new BluetoothLEAdvertisementWatcher
{
ScanningMode = BluetoothLEScanningMode.Active
};
BleWatcher.Received += Watcher_Received;
BleWatcher.Start();
}
private async void Watcher_Received(BluetoothLEAdvertisementWatcher sender,
BluetoothLEAdvertisementReceivedEventArgs args)
{
var device = await BluetoothLEDevice.FromBluetoothAddressAsync(args.BluetoothAddress);
// never continues beyond this point above with my BTLE devices that previously worked
}
我按照此处的说明操作 https://stackoverflow.com/a/37335251/3187714设置我的 WPF 桌面应用程序以使用 UWP API。
问题更糟,因为当客户开始升级到 Win 10 1703 时,我现有的 WPF 应用程序将被破坏,因为我现有的 exe 不再有效。
是否有其他人在(非 UWP)桌面 exe 中遇到此 Windows 10 1703 更新问题?
经过进一步的实验,我确实发现如果我将可选的 BluetoothAddressType.Public 第二个参数添加到 FromBluetoothAddressAsync 调用,该函数返回,但返回的设备为空。
var device = await BluetoothLEDevice.FromBluetoothAddressAsync(args.BluetoothAddress, BluetoothAddressType.Public);
最佳答案
看起来 15063 中围绕蓝牙 API 的安全功能中存在错误导致了此问题(我看到了同样的事情)。查看此线程:
tl;dr 对于 C++ 应用程序,它们提供了一个 CoInitializeSecurity 函数来调用。对于其他人,他们似乎建议创建一个 AppId 在注册表中,因为 P/Invoke 不是很有趣。
奇怪的是,目前通过 noble-uwp 对我来说一切正常,它使用 C++ 绑定(bind)到 UWP 函数以通过 node.js 进行访问。我只是通过 C# 遇到问题,并且根据我是在 UWP 还是 WPF/控制台/桌面应用程序中,事情会在不同的点失败。
按照论坛帖子中的概述将 AppId 添加到注册表后,一切对我来说再次有效。
关于c# - FromBluetoothAddressAsync 永远不会在 WPF 应用程序中的 Windows 10 Creators Update 上返回,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43568096/