C#蓝牙LE - 写配置错误 - 的ValueChanged从来没有所谓

问题描述:

因此,我想我的C#WPF程序连接到BLE装置,这是连接到设备的代码:C#蓝牙LE - 写配置错误 - 的ValueChanged从来没有所谓

private async Task ConnectToWatcher(DeviceInformation deviceInfo) { 
     try { 
      // get the device 
      BluetoothLEDevice device = await BluetoothLEDevice.FromIdAsync(deviceInfo.Id); 

      // get the GATT service 
      Thread.Sleep(150); 
      var gattServicesResult = await device.GetGattServicesForUuidAsync(new Guid(RX_SERVICE_UUID)); 
      service = gattServicesResult.Services[0]; 

      // get the GATT characteristic 
      Thread.Sleep(150); 
      var gattCharacteristicsResult = await service.GetCharacteristicsForUuidAsync(new Guid(RX_CHAR_UUID)); 
      characteristic = gattCharacteristicsResult.Characteristics[0]; 

      // register for notifications 
      Thread.Sleep(150); 

      characteristic.ValueChanged += (sender, args) => { 
       Debug.WriteLine($"[{device.Name}] Received notification containing {args.CharacteristicValue.Length} bytes"); 
      }; 

      GattWriteResult result = 
      await characteristic.WriteClientCharacteristicConfigurationDescriptorWithResultAsync(GattClientCharacteristicConfigurationDescriptorValue.Notify); 
      Debug.WriteLine($"Characteristics write result: status={result.Status}, protocolError={result.ProtocolError}"); 

     } catch (Exception ex) when ((uint)ex.HResult == 0x800710df) { 
      Debug.WriteLine("bluetooth error 1"); 
      // ERROR_DEVICE_NOT_AVAILABLE because the Bluetooth radio is not on. 
     } 
    } 

线

Debug.WriteLine($"Characteristics write result: status={result.Status}, protocolError={result.ProtocolError}" 

创建输出 特点写入结果:状态= protocolError,protocolError = 3 我找不到任何地方什么是应该的意思。效果是方法特性.ValueChanged永远不会被调用。

我需要做更多的特征配置吗?任何人都不知道为什么这个方法没有被调用,或者那个错误信息是什么意思?

非常感谢。

显然,某些Windows 10版本存在COM安全漏洞。 解决方案是注册Windows-Insider-Program并更新Windows。

更新: 有两个解决方案,以这样的: 1)从C#-code外调用CoInitializeSecurity(没带它的工作) - 或 - 2)写了两个新的钥匙进入Windows注册表。你不能创建一个文件“Bluetooth.reg”,它会在双击中添加这两个键。来自https://social.msdn.microsoft.com/Forums/en-US/58da3fdb-a0e1-4161-8af3-778b6839f4e1/bluetooth-bluetoothledevicefromidasync-does-not-complete-on-10015063?forum=wdk的这个答案是我的解决方案:

嗨Kamen,我遇到了完全相同的问题。你可以给一个快速的请尝试以下:


Windows注册表编辑器5.00版

[HKEY_LOCAL_MACHINE \ SOFTWARE \ Classes下\ {的AppID C6BFD646-3DF0-4DE5-B7AF-5FFFACB844A5}] “AccessPermission” = hex:01,00,04,80,9c,00,00,00,ac,00,00,00,00,00,00,00,14,00,\ 00,00,02,00,88 ,00,06,00,00,00,00,00,14,00,07,00,00,00,01,01,00,00,00,00,00,\ 05,0a,00,00 ,00,00,00,14,00,03,00,00,00,01,01,00,00,00,00,00,05,12,00,00,00,\ 00,00,18 ,00,07,00,00,00,01,02,00,00,00,00,00,05,20,00,00,00,20,02,00,00,00,\ 00,18 ,00,03,00,00,00,01,02,0 0,00,00,00,00,0f,02,00,00,00,01,00,00,00,00,00,\ 14,00,03,00,00,00,01,01, 00,00,00,00,00,05,13,​​00,00,00,00,00,14,00,03,00,00,\ 00,01,01,00,00,00,00, 00,05,14,00,00,00,01,02,00,00,00,00,00,05,20,00,00,00,\ 20,02,00,00,01,02, 00,00,00,00,00,05,20,00,00,00,20,02,00,00

[HKEY_LOCAL_MACHINE \ SOFTWARE \ Classes \ AppID \ YOURAPP.exe] “AppID”=“ {C6BFD646-3DF0-4DE5-B7AF-5FFFACB844A5}”


  1. 将上述内容复制到文本文件中。

  2. 替换'YOURAPP。EXE”与可执行文件的名称

  3. 可选取代的GUID(我已经为您创建一个新的)

  4. 保存的文本文件的扩展名为.reg

  5. 上双击文件。

  6. 您将看到一条消息'..... .reg中包含的密钥和值已成功添加到注册表中。'

有了这个,我不需要从我的应用程序调用CoInitializeSecurity。

你所需要的前

更多信息调用CoInitializeSecurity。几个星期我花了寻找解决方案:)

https://social.msdn.microsoft.com/Forums/en-US/58da3fdb-a0e1-4161-8af3-778b6839f4e1/

+0

虽然此链接可以回答这个问题,最好是在这里有答案的主要部件,并提供链接以供参考。如果链接页面更改,则仅链接答案可能会失效。 - [来自评论](/评论/低质量帖/ 17707088) –

+0

你好Eriawan,我认为必要的信息是**调用CoInitializeSecurity **。只有这样才能解决问题。通过将这个答案标记为负面,你可能会导致其他人花时间在错误的解决方案上。 –