Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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);
like image 785
JFar Avatar asked Apr 23 '17 06:04

JFar


1 Answers

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.

like image 69
qDot Avatar answered Nov 03 '22 12:11

qDot