当前位置: 首页 > 编程语言 > C#

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

时间:2023-04-11 03:05:15 C#

C#学习教程:FromBluetoothAddressAsyncneverreturnsonWindows10CreatorsUpdateinWPF应用程序(CreatorsUpdate)正式发布。当我在WPF桌面应用程序中运行以下代码时,BluetoothLEDevice.FromBluetoothAddressAsync永远不会返回。此代码在我的Windows10更新(即1607之前的版本14393)之前运行良好。如果代码在新的Win101703中作为UWP运行,它也能正常工作。BluetoothLEAdvertisementWatcherBleWatcher=null;privatevoidButton_Click(objectsender,RoutedEventArgse){BleWatcher=newBluetoothLEAdvertisementWatcher{ScanningMode=BluetoothLEScanningMode.Active};BleWatcher.Received+=Watcher_Received;BleWatcher.Start();}privateasyncvoidWatcher_Received(BluetoothLEAdvertisementWatchersender,BluetoothLEAdvertisementReceivedEventArgsargs){vardevice=awaitBluetoothLEDevice.FromBluetoothAddressAsync(args.BluetoothAddress);//我以前工作过的BTLE设备永远不会超出这一点}我按照1314说明中的https://stackoverflow.com/a757/37设置我的WPF桌面应用程序以使用UWPAPI。问题更严重,因为当客户开始升级到Win101703时,我现有的WPF应用程序将被破坏,因为我现有的exe不再有效。有没有其他人在(非UWP)桌面exe中遇到Windows101703更新的这个问题?经过进一步的实验,我确实发现,如果我将可选的BluetoothAddressType.Public第二个参数添加到FromBluetoothAddressAsync调用中,该函数返回,但返回的设备为空。vardevice=awaitBluetoothLEDevice.FromBluetoothAddressAsync(args.BluetoothAddress,BluetoothAddressType.Public);看起来15063中蓝牙API的安全功能中存在错误导致了这一点(我见过同样的事情)。查看这篇文章: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-8a80d2323a7ftl;dr对于C++应用程序,它们提供了一个CoInitializeSecurity函数来调用。对于其他人,他们似乎建议在注册表中创建一个AppId,因为P/Invoke不是很有趣。奇怪的是,它目前通过noble-uwp对我来说工作正常,它使用C++绑定到UWP函数以通过node.js访问。只有通过C#,我才会遇到问题,并且根据我是在UWP还是WPF/控制台/桌面应用程序中,事情会在不同的点失败。按照论坛帖子中的说明将AppId添加到注册表后,一切再次对我有用。有趣的是,它适用于使用cppwinrt的桌面应用程序:voidStart(){watcher.ScanningMode(Advertisement::BluetoothLEScanningMode::Active);Windows::Foundation::TimeSpan超时=std::chrono::seconds(2);watcher.SignalStrengthFilter().OutOfRangeTimeout(超时);watcher.SignalStrengthFilter().OutOfRangeThresholdInDBm(-90);watcher.Received([&](Advertisement::BluetoothLEAdvertisementWatcherwatcher,Advertisement::BluetoothLEAdvertiseArgiveArgiveArgiveRe){connect(eventArgs.BluetoothAddress());});观察者.Start();}Windows::Foundation::IAsyncActionconnect(uint64_tuuid){co_awaitresume_background();BluetoothLEDevicedevice=co_awaitBluetoothLEDevice::FromBluetoothAddressAsync(uuuit;GenericAttributeProfile::GattDeviceServicesResultgatt=co_awaitdevice.GetGattServicesForUuidAsync(myServiceUUID);//工作正常!}然而,关于GATT特性的通知在此之后对我不起作用。我已经提交一个错误报告给cppwinrt项目,但开发人员似乎并不特别愿意调查它。我不知道该API与桌面C#版本有何不同。如果您想在VB中使用CoInitializeSecurity解决方案,这里有一个pinvoke解决方案。请记住取消/修复所有BLE设备!请注意,这必须是代码中执行的第一件事。请注意,这并不完全安全。以上是C#学习教程:FromBluetoothAddressAsync永远不会在WPF应用程序中返回Windows10CreatorsUpdate上分享的全部内容。如果对大家有用,需要详细了解C#学习教程,希望大家多加关注—PublicEnumRpcAuthnLevelDefault1=0None=1Connect=2Call1=3Pkt=4PktIntegrity=5PktPrivacy=6EndEnumPublicEnumRpcImpLevelDefault1=0Anonymous=1Identify=2Impersonate=3Delegate1=4EndEnumPublicEoH0CapNMutualAuth=&H1StaticCloaking=&H20DynamicCloaking=&H40AnyAuthority=&H80MakeFullSIC=&H100Default1=&H800SecureRefs=&H2AccessControl=&H4AppID=&H8Dynamic=&H10RequireFullSIC=&H200AutoImpersonate=&H400NoCustomMarshal=&H2000DisableAAA=&H1000EndEnumDeclareFunctionCoInitializeSecurityLib"ole32.dll"(pVoidAsIntPtr,cAuthSvcAsInteger,asAuthSvcAsIntPtr,pReserved1AsIntPtrvel,dReserved1AsIntPtrvel作为整数,pAuthList作为IntPtr,dwCapabilitiesAsInteger,pReserved3AsIntPtr)AsIntegerPrivateSubForm1_Load(senderAsObject,eAsEventArgs)处理MyBase.LoadCoInitializeSecurity(IntPtr.Zero,-1,IntPtr.Zero,IntPtr.Zero,RpcAuthnLevel.Default1,RpcImpLevel.Identify,IntPtr.Zero,EoAuthnCap.None,IntPtr.Zero)'其他代码EndSub本文收集自网络,不代表立场。如涉及侵权,请点击右侧联系管理员删除。