Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
872 views
in Technique[技术] by (71.8m points)

c# - FromBluetoothAddressAsync never returns on Windows 10 Creators Update in WPF Application

I upgraded to Windows 10, version 1703 build 15063 (Creators Update) official release. When I run the following code in a WPF desktop application, BluetoothLEDevice.FromBluetoothAddressAsync never returns.

This code worked on fine before my Windows 10 update (i.e. the previous 1607 build 14393). This code also works fine if it is running as a UWP in the new Win 10 1703.

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 
}

I followed the instructions here https://stackoverflow.com/a/37335251/3187714 to setup my WPF desktop app to use the UWP APIs.

The problem is even worse because my existing WPF application will be broken when customers start upgrading to Win 10 1703 because my existing exe no longer works.

Is anyone else experiencing this problem with the Windows 10 1703 update in a (Non UWP) desktop exe?

After further experiments, I did find if I added the optional BluetoothAddressType.Public 2nd argument to the FromBluetoothAddressAsync call, the function returned, but the device returned was null.

var device = await BluetoothLEDevice.FromBluetoothAddressAsync(args.BluetoothAddress, BluetoothAddressType.Public);
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

It looks like there's a bug in the Security functions around the Bluetooth APIs in 15063 that's causing this (I'm seeing the same thing). Check out this thread:

https://social.msdn.microsoft.com/Forums/en-US/58da3fdb-a0e1-4161-8af3-778b6839f4e1/bluetooth-bluetoothledevicefromidasync-does-not-complete-on-10015063?forum=wdk#ef927009-676c-47bb-8201-8a80d2323a7f

tl;dr For C++ applications, they provide a CoInitializeSecurity function to call. For everyone else, it looks like they're recommending creating an AppId in the registry, since P/Invoke isn't much fun.

Weirdly enough, things are working fine for me at the moment through noble-uwp, which uses C++ bindings to UWP functions for access via node.js. It's only via C# that I'm having issues, and things fail at different points depending on whether I'm in a UWP or WPF/Console/Desktop application.

After adding the AppId to the registry as outlined in the forum post, things worked for me again.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...