因此,我尝试将我的 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 我找不到任何地方这是什么意思。结果是方法 characteristic.ValueChanged 永远不会被调用。
我是否需要执行更多操作才能配置该特征?有谁知道为什么不调用该方法或者该错误消息意味着什么?
非常感谢。
显然,某些 Windows 10 版本存在 COM 安全错误。 解决方案是注册 Windows-Insider-Program 并更新 Windows。
更新: 对此还有两个解决方案: 1)从c#代码外部调用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”=十六进制: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\YOURAPP.exe] “应用程序ID”=“{C6BFD646-3DF0-4DE5-B7AF-5FFFACB844A5}”
将以上内容复制到文本文件。
将“YOURAPP.EXE”替换为您的可执行文件名称
可选择替换 GUID(我已为您创建了一个新的 GUID)
保存扩展名为.reg的文本文件
双击该文件。
您将看到一条消息“......reg 中包含的键和值已成功添加到注册表中。”
有了这个,我不需要从我的应用程序中调用 CoInitializeSecurity。
在 C# 中调用以下函数时,出现 Characteristics write result: status=protocolError, protocolError=3 错误。
await characteristic.WriteClientCharacteristicConfigurationDescriptorWithResultAsync(GattClientCharacteristicConfigurationDescriptorValue.Notify)
问题是我尝试从中获取通知数据的 BLE 特性没有 BLE 描述符。
ESP32 C++
BLEService *service = pServer->createService(SERVICE_UUID);
BLECharacteristic* characteristic = service->createCharacteristic(
CHARACTERISTIC_UUID,
BLECharacteristic::PROPERTY_NOTIFY);
// This code was missing and causing the error
BLEDescriptor *descriptor = new BLEDescriptor((uint16_t)0x2901);
descriptor->setValue("The Descriptor Is No Longer Missing");
characteristic->addDescriptor(descriptor);
您需要先调用 CoInitializeSecurity
更多信息请点击这里。我花了几周时间寻找解决方案:)
https://social.msdn.microsoft.com/Forums/en-US/58da3fdb-a0e1-4161-8af3-778b6839f4e1/