C# 使用Windows10BLEAPI获取自发异常

C# 使用Windows10BLEAPI获取自发异常,c#,uwp,windows-runtime,windows-10,bluetooth-lowenergy,C#,Uwp,Windows Runtime,Windows 10,Bluetooth Lowenergy,我正在与一个BLE设备配对,并在一个小型测试控制台程序中查询它的所有服务和特性 在查询过程中,我不时会遇到以下错误: Scanning for BLE Devices with service 00008000-0000-1000-8000-0012345678 Finding nearest device Connecting to MAC CC12345678CF Pairing... Waiting for DevicePairingResult result... Waiting

我正在与一个BLE设备配对,并在一个小型测试控制台程序中查询它的所有服务和特性

在查询过程中,我不时会遇到以下错误:

Scanning for BLE Devices with service 00008000-0000-1000-8000-0012345678
Finding nearest device
Connecting to MAC CC12345678CF
Pairing...
  Waiting for DevicePairingResult result...
  Waiting for DevicePairingResult result...
Getting info...
  Waiting for GattDeviceServicesResult result...
  Waiting for GattReadResult result...
  Waiting for GattCharacteristicsResult result...
  Async op has Error: System.InvalidOperationException A method was called at an unexpected time. (Exception from HRESULT: 0x8000000E)
  Async op has Error: System.ObjectDisposedException The object has been closed. (Exception from HRESULT: 0x80000013)
  Async op has Error: System.ObjectDisposedException The object has been closed. (Exception from HRESULT: 0x80000013)
  Async op has Error: System.ObjectDisposedException The object has been closed. (Exception from HRESULT: 0x80000013)
Unpairing...
System.NullReferenceException: Object reference not set to an instance of an object.
   at UWPWrapper.BLE.DeviceLister.ListServices(GattDeviceService[] services, Int32 level) in UWPWrapper\UWPWrapper\BLE\DeviceLister.cs:line 96
这一行在这里只是if的情况,但它似乎发生在for循环和许多其他地方

 var descResult = chr.GetDescriptorsAsync().Wait();
 if (descResult.Status != GattCommunicationStatus.Success)
 {
   sb.AppendLine($"{indent}    No descriptors: {descResult.Status}");
 }
我正在访问的对象似乎在我使用它们时被处理,因为我在非常奇怪的地方出错,尽管我一直在保留引用

其他问题指向这一点(听GATTCharacteristics.ValueChanged事件)。你知道怎么解决这个问题吗


FWIW,我正在编写一个普通的C#桌面应用程序,但使用WinRT API包装器。除了这个问题之外,它似乎工作得很好。

对于特定的设备,它是否一直在发生?也就是说,是否有设备从未发生过这种情况?有经常发生这种情况的设备吗?我在连接蓝牙时也遇到了一些问题。不确定我是否也犯了同样的错误。然而,我也得到了“一个方法在一个意外的时间被调用”的错误,这听起来很奇怪。事实证明,问题是,我使用了一些异步方法连接到设备,没有使用wait关键字,只是将结果存储在如下内容中:var stream;然后我尝试使用stream的结果。使用async关键字对我很有效。异步的东西也可以解释为什么它只是偶尔发生,因为该方法可能在使用流或流之前就已经完成了not@Fildor这是一个正在开发的特定设备,我正在为它编写测试。然而,解除分配我正在迭代的对象似乎无论如何都是错误的。@Hannes在访问任何异步操作的结果之前,我总是等待“完成”。如果结果是其他结果(错误等),我不会访问结果。我也有同样的问题。所处理的对象实际上是characteristic的父服务,而不是characteristic本身。