c#和BLE - 写入特征只能工作一次

问题描述 投票:0回答:1

我正在开发一个Windows表单程序,必须能够每3分钟为BLE设备的特定特性写一个值。我不希望用户选择正确的设备。相反,我想在代码中选择正确的设备。我能够连接到我的设备并第一次写入值,但第二次写入程序在System.IO.FileLoadException中抛出mscorlib.dll

这是我正在使用的代码:

​
  private async void button1_Click(object sender, EventArgs e)
    {
        try
        {
            //var devicex=await DeviceInformation.CreateFromIdAsync(GattDeviceService.GetDeviceSelectorFromUuid(new Guid("00001800-0000-1000-8000-00805f9b34fb")), new string[] { "System.Devices.ContainerId" }); //get all connected devices
            var devices = await DeviceInformation.FindAllAsync(GattDeviceService.GetDeviceSelectorFromUuid(new Guid("00001800-0000-1000-8000-00805f9b34fb")), new string[] { "System.Devices.ContainerId" }); //get all connected devices
            if (devices.Count > 0)
            {
                foreach (var device in devices)
                {
                    BluetoothLEDevice dev = await BluetoothLEDevice.FromIdAsync(device.Id);
                    DevicePairingResult dpr = await dev.DeviceInformation.Pairing.PairAsync(DevicePairingProtectionLevel.Encryption);
                    Debug.WriteLine(dev.ConnectionStatus);
                    var services = await dev.GetGattServicesAsync();
                    foreach (var service in services.Services)
                    {
                        //Debug.WriteLine($"Service: {service.Uuid}");
                        //service.Uuid.GetType();
                        var characteristics = await service.GetCharacteristicsAsync(BluetoothCacheMode.Cached);
                        foreach (var character in characteristics.Characteristics)
                        {                        
                         var result = await character.ReadValueAsync();
                         var reader = DataReader.FromBuffer(result.Value);
                         var input = new byte[reader.UnconsumedBufferLength];
                         reader.ReadBytes(input);
                         Debug.WriteLine(BitConverter.ToString(input));
                         Debug.WriteLine("Characteristic Handle: " + 
                                    character.AttributeHandle + ", UUID: " + 
                                    character.Uuid);
                }
            }

即使我尝试读取或获取UUID也会出现问题,它只发生在特定的UUID上(我的意思是第二次读取第一个和第二个UUID,第二次读取第三个UUID导致异常) )。

关于如何解决这个问题的任何建议?

完整堆栈跟踪是

Eccezione generata: 'System.IO.FileLoadException' in mscorlib.dll
'WindowsFormsApp4.exe' (CLR v4.0.30319: WindowsFormsApp4.exe): caricamento di 'C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\Common7\IDE\PrivateAssemblies\Runtime\Microsoft.VisualStudio.Debugger.Runtime.dll' completato. Caricamento dei simboli ignorato. Il modulo è ottimizzato e l'opzione del debugger 'Solo codice utente' è abilitata.
   in System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   in System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   in System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
   in WindowsFormsApp4.Form1.<button1_Click>d__10.MoveNext() in C:\Users\Andrea\source\repos\WindowsFormsApp4\WindowsFormsApp4\Form1.cs:riga 182

触发异常的行是:

var characteristics = await service.GetCharacteristicsAsync(BluetoothCacheMode.Uncached);

当BluetoothCacheMode设置为Cached或Uncached时触发异常。

c# bluetooth-lowenergy uuid
1个回答
0
投票

从你的问题中不清楚你想要实现什么,但你的例外很可能是因为阅读不支持阅读的特征。您必须过滤read属性。这可以帮助您:

 foreach (var character in characteristics.Characteristics)
 {
   GattCharacteristicProperties properties = character.CharacteristicProperties;
                    if (properties.HasFlag(GattCharacteristicProperties.Read))
                    {
                       var result = await character.ReadValueAsync();
                       var reader = DataReader.FromBuffer(result.Value);
                       var input = new byte[reader.UnconsumedBufferLength];
                       reader.ReadBytes(input);
                       Debug.WriteLine(BitConverter.ToString(input));
                       Debug.WriteLine("Characteristic Handle: " +
                                  character.AttributeHandle + ", UUID: " +
                                  character.Uuid);
                    }
                   // these are other sorting flags that can be used so sort characterisics.
                    if (properties.HasFlag(GattCharacteristicProperties.Write))
                    {
                       Debug.Write("This characteristic supports writing.");
                    }
                    if (properties.HasFlag(GattCharacteristicProperties.Notify))
                    {
                       Debug.Write("This characteristic supports subscribing to notifications.");
                    }

 }
© www.soinside.com 2019 - 2024. All rights reserved.