简体   繁体   English

c#蓝牙LE-写入配置错误-从未调用过ValueChanged

[英]c# Bluetooth LE - write configuration error - ValueChanged never called

So I try to connect my c# WPF program to a BLE device and this is the code to connect to the device: 因此,我尝试将c#WPF程序连接到BLE设备,这是连接到该设备的代码:

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.
        }
    }

The line 线

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

creates the output Characteristics write result: status=protocolError, protocolError=3 I couldn't find anywhere whats that supposed to mean. 创建输出特征写入结果:status = protocolError,protocolError = 3我在任何地方都找不到这意味着什么。 The effect is the Method characteristic.ValueChanged never gets called. 结果就是方法特性 .ValueChanged永远不会被调用。

Do I have to do more to have the characteristic configurated? 我需要做更多的事情来配置特性吗? And has anybody any idea why the method isn't called or what that error message means? 有人知道为什么不调用该方法或该错误消息表示什么吗?

Thanks a bunch. 谢谢你

Apparently, some Windows 10 builds have a COM security bug. 显然,某些Windows 10版本具有COM安全漏洞。 Solution was to register for Windows-Insider-Program and update Windows. 解决的办法是注册Windows-Insider-Program并更新Windows。

Update: There are two more solutions to this: 1) Calling CoInitializeSecurity from outside your c#-code (didn't bring it to work) - or- 2) Writing two new keys into the windows registry. 更新:对此还有两种解决方案:1)从您的C#代码外部调用CoInitializeSecurity(没有使其工作)-或2)将两个新密钥写入Windows注册表。 You cann create a file "Bluetooth.reg", which will add those two keys on a doubleklick. 您无法创建文件“ Bluetooth.reg”,该文件将在doubleklick上添加这两个键。 This answer from https://social.msdn.microsoft.com/Forums/en-US/58da3fdb-a0e1-4161-8af3-778b6839f4e1/bluetooth-bluetoothledevicefromidasync-does-not-complete-on-10015063?forum=wdk was my solution: 来自https://social.msdn.microsoft.com/Forums/en-US/58da3fdb-a0e1-4161-8af3-778b6839f4e1/bluetooth-bluetoothledevicefromidasync-does-not-complete-on-10015063?forum=wdk的答案是我的解:

Hi Kamen, I have experienced exactly same issue. 嗨,假面,我遇到了完全一样的问题。 You can give a quick try of the following: 您可以快速尝试以下方法:


Windows Registry Editor Version 5.00 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,00,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 {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,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,00,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,00 ,20,02,00,00

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


  1. Copy above content to a text file. 将以上内容复制到文本文件。

  2. Replace 'YOURAPP.EXE' with your executable name 将“ YOURAPP.EXE”替换为您的可执行文件名称

  3. Optionally replace GUIDs (I have created a new one for you) (可选)替换GUID(我为您创建了一个新的GUID)

  4. Save text file with an extension as .reg 将带有扩展名的文本文件另存为.reg

  5. Double click on the file. 双击文件。

  6. You would see a message 'The Keys and values contained in ..... .reg have been successfully added to the registry.' 您会看到一条消息“ ..... reg中包含的键和值已成功添加到注册表中。”

With this, I don't need to call CoInitializeSecurity from my app. 有了这个,我不需要从我的应用程序调用CoInitializeSecurity。

you need to call CoInitializeSecurity before 您需要先致电CoInitializeSecurity

more info here. 更多信息在这里。 couple of weeks I spent looking for solution :) 我花了两个星期寻找解决方案:)

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

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM