簡體   English   中英

c#藍牙LE-寫入配置錯誤-從未調用過ValueChanged

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

因此,我嘗試將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.
        }
    }

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

創建輸出特征寫入結果:status = protocolError,protocolError = 3我在任何地方都找不到這意味着什么。 結果就是方法特性 .ValueChanged永遠不會被調用。

我需要做更多的事情來配置特性嗎? 有人知道為什么不調用該方法或該錯誤消息表示什么嗎?

謝謝你

顯然,某些Windows 10版本具有COM安全漏洞。 解決的辦法是注冊Windows-Insider-Program並更新Windows。

更新:對此還有兩種解決方案:1)從您的C#代碼外部調用CoInitializeSecurity(沒有使其工作)-或2)將兩個新密鑰寫入Windows注冊表。 您無法創建文件“ Bluetooth.reg”,該文件將在doubleklick上添加這兩個鍵。 來自https://social.msdn.microsoft.com/Forums/en-US/58da3fdb-a0e1-4161-8af3-778b6839f4e1/bluetooth-bluetoothledevicefromidasync-does-not-complete-on-10015063?forum=wdk的答案是我的解:

嗨,假面,我遇到了完全一樣的問題。 您可以快速嘗試以下方法:


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,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}”


  1. 將以上內容復制到文本文件。

  2. 將“ YOURAPP.EXE”替換為您的可執行文件名稱

  3. (可選)替換GUID(我為您創建了一個新的GUID)

  4. 將帶有擴展名的文本文件另存為.reg

  5. 雙擊文件。

  6. 您會看到一條消息“ ..... reg中包含的鍵和值已成功添加到注冊表中。”

有了這個,我不需要從我的應用程序調用CoInitializeSecurity。

您需要先致電CoInitializeSecurity

更多信息在這里。 我花了兩個星期尋找解決方案:)

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