简体   繁体   English

FromBluetoothAddressAsync永远不会在WPF应用程序中的Windows 10 Creators Update上返回

[英]FromBluetoothAddressAsync never returns on Windows 10 Creators Update in WPF Application

I upgraded to Windows 10, version 1703 build 15063 (Creators Update) official release. 我升级到Windows 10,版本1703 build 15063(Creators Update)正式发布。 When I run the following code in a WPF desktop application, BluetoothLEDevice.FromBluetoothAddressAsync never returns. 当我在WPF桌面应用程序中运行以下代码时,BluetoothLEDevice.FromBluetoothAddressAsync永远不会返回。

This code worked on fine before my Windows 10 update (ie the previous 1607 build 14393). 在我的Windows 10更新之前(即之前的1607版本14393),此代码工作正常。 This code also works fine if it is running as a UWP in the new Win 10 1703. 如果在新的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 
}

I followed the instructions here https://stackoverflow.com/a/37335251/3187714 to setup my WPF desktop app to use the UWP APIs. 我按照https://stackoverflow.com/a/37335251/3187714中的说明设置我的WPF桌面应用程序以使用UWP API。

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. 问题更严重,因为我的现有WPF应用程序将在客户开始升级到Win 10 1703时被破坏,因为我现有的exe不再有效。

Is anyone else experiencing this problem with the Windows 10 1703 update in a (Non UWP) desktop exe? 是否有其他人在(非UWP)桌面exe中使用Windows 10 1703更新遇到此问题?

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. 经过进一步的实验,我确实发现如果我将可选的BluetoothAddressType.Public第二个参数添加到FromBluetoothAddressAsync调用中,则返回该函数,但返回的设备为null。

var device = await BluetoothLEDevice.FromBluetoothAddressAsync(args.BluetoothAddress, BluetoothAddressType.Public);

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). 看起来15063中的蓝牙API周围的安全功能中存在一个错误导致这种情况(我看到了同样的事情)。 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 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. tl; dr对于C ++应用程序,它们提供了一个CoInitializeSecurity函数来调用。 For everyone else, it looks like they're recommending creating an AppId in the registry, since P/Invoke isn't much fun. 对于其他人来说,看起来他们建议在注册表中创建一个AppId,因为P / Invoke并不是很有趣。

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. 奇怪的是,目前通过noble-uwp为我工作正常,它使用C ++绑定到UWP函数以通过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. 只有通过C#,我才遇到问题,并且根据我是否在UWP或WPF / Console / Desktop应用程序中,事情会在不同点失败。

After adding the AppId to the registry as outlined in the forum post, things worked for me again. 按照论坛帖子中的说明将AppId添加到注册表后,事情再次对我有用。

Interestingly, it does work on desktop applications using cppwinrt: 有趣的是, 使用cppwinrt桌面应用程序的工作:

Advertisement::BluetoothLEAdvertisementWatcher watcher;

void Start() {
    watcher.ScanningMode(Advertisement::BluetoothLEScanningMode::Active);
    Windows::Foundation::TimeSpan timeout = std::chrono::seconds(2);
    watcher.SignalStrengthFilter().OutOfRangeTimeout(timeout);
    watcher.SignalStrengthFilter().OutOfRangeThresholdInDBm(-90);

    watcher.Received([&](Advertisement::BluetoothLEAdvertisementWatcher watcher, Advertisement::BluetoothLEAdvertisementReceivedEventArgs eventArgs) {
        connect(eventArgs.BluetoothAddress());
    });
    watcher.Start();
}

Windows::Foundation::IAsyncAction connect(uint64_t uuid) {
    co_await resume_background();
    BluetoothLEDevice device = co_await BluetoothLEDevice::FromBluetoothAddressAsync(uuid);
    GenericAttributeProfile::GattDeviceServicesResult gatt = co_await device.GetGattServicesForUuidAsync(myServiceUUID);
    // works fine!
}

However, notifications for GATT characteristics don't work for me after that. 但是,关于GATT特征的通知在此之后对我不起作用。 I have filed a bug report with the cppwinrt project, but the devs don't seem to be particularly inclined to look into it. 我已经向 cppwinrt项目提交了一份错误报告 ,但开发人员似乎并不特别倾向于调查它。

I have no idea what that API is doing differently than the desktop C# version. 我不知道那个API与桌面C#版本有什么不同。

If you wish to use the CoInitializeSecurity solution in VB, here is a pinvoke solution. 如果您希望在VB中使用CoInitializeSecurity解决方案,这里有一个pinvoke解决方案。 Remember to unpair/repair all your BLE devices! 请记住取消/修理所有BLE设备!

Note that is must be the fist thing executed in your code. 请注意,这必须是代码中执行的第一件事。 Note that this is NOT AT ALL secure. 请注意,这并非完全安全。

Public Enum RpcAuthnLevel
    Default1 = 0
    None = 1
    Connect = 2
    Call1 = 3
    Pkt = 4
    PktIntegrity = 5
    PktPrivacy = 6
End Enum

Public Enum RpcImpLevel
    Default1 = 0
    Anonymous = 1
    Identify = 2
    Impersonate = 3
    Delegate1 = 4
End Enum

Public Enum EoAuthnCap
    None = &H0
    MutualAuth = &H1
    StaticCloaking = &H20
    DynamicCloaking = &H40
    AnyAuthority = &H80
    MakeFullSIC = &H100
    Default1 = &H800
    SecureRefs = &H2
    AccessControl = &H4
    AppID = &H8
    Dynamic = &H10
    RequireFullSIC = &H200
    AutoImpersonate = &H400
    NoCustomMarshal = &H2000
    DisableAAA = &H1000
End Enum

Declare Function CoInitializeSecurity Lib "ole32.dll" (pVoid As IntPtr, cAuthSvc As Integer, asAuthSvc As IntPtr, pReserved1 As IntPtr, dwAuthnLevel As Integer, dwImpLevel As Integer, pAuthList As IntPtr, dwCapabilities As Integer, pReserved3 As IntPtr) As Integer

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    CoInitializeSecurity(IntPtr.Zero, -1, IntPtr.Zero, IntPtr.Zero, RpcAuthnLevel.Default1, RpcImpLevel.Identify, IntPtr.Zero, EoAuthnCap.None, IntPtr.Zero)
    'Other code
End Sub

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Windows 10 Creators更新后,DateTimePicker出现乱码 - DateTimePicker garbled after Windows 10 Creators Update 如何从Windows 10 Creators Update上运行的WPF应用程序注册BLE通知? - How to register for BLE notifcations from a WPF app running on Windows 10 Creators Update? 使用Windows 10 Creators Update,.NET 4.7或KB4034658在平板电脑上未触发WPF .NET应用程序崩溃,ComboBox和Menu事件 - WPF .NET app crashes, ComboBox and Menu events not fired on Tablets with Windows 10 Creators Update, .NET 4.7 or KB4034658 Windows 10 Creators更新.net 4.7 System.AccessViolationException问题 - Windows 10 Creators update .net 4.7 System.AccessViolationException issue 如何正确使用Windows 10 Creators Update中的Acrylic Accent(CreateHostBackDropBrush())? - How to use Acrylic Accent (CreateHostBackDropBrush()) in Windows 10 Creators Update correctly? 如何在Windows 10 Creators Update中使用Acrylic Accent? - How to use Acrylic Accent in Windows 10 Creators Update? Windows 10上的WPF应用程序问题 - WPF application issue on Windows 10 Windows 10 Fall Creators Update之后的ASP.NET HttpHandler错误 - ASP.NET HttpHandler error after Windows 10 Fall Creators Update 安装Windows 10创建者更新1703后,将数据复制到便携式设备时出现System.AccessViolationException - System.AccessViolationException when copying data to portable device after installing windows 10 creators update 1703 从Windows 10 1709开始,正常重启重启Explorer.exe,Fall Creators Update aka Redstone 3 - Gracefully restarting explorer.exe as of Windows 10 1709, Fall Creators Update aka Redstone 3
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM