1

我想通过在 Visual Studio 2017 中使用通用 Windows 平台 C# 中的 ValueChanged 回调,继续从我的 BLE 4.0 设备中读取特征/设置值更改的事件处理程序。

我遵循了这些站点的一些教程:Damian Blog 的 Windows Universal with BLEBluetooth Gatt 的 Git HubBluetooth Generic Attribute Profile - Heart Rate ServiceDr. Jukka 的 BLE 移动博客。他们所有人都在使用 ValueChanged,我试图遵循他们的做法。

不幸的是,我在使用 ValueChanged 回调时收到以下错误,而不是触发 ValueChanged。

System.ArgumentException: 'Value does not fall within the expected range.'

这行代码正在产生错误:

characteristic.ValueChanged += Oncharacteristic_ValueChanged;

这是我的源代码的更多详细信息:

注意:我为我的加密狗使用 COM 7,我的程序可以发现 BLE 的设备名称,并且可以发现服务和特征的 Uuid。

    public List<string> serviceList = new List<string>();
    public List<string> characteristicList = new List<string>();
    public BluetoothLEDevice myDevice { get; set; }

    public MainPage()
    {
        this.InitializeComponent();
    }

        private async void Page_Loaded(object sender, RoutedEventArgs e)
    {
        // Find the com port
        string selector = SerialDevice.GetDeviceSelector("COM7");
        DeviceInformationCollection devices = await DeviceInformation.FindAllAsync(selector);
        if (devices.Count > 0)
        {
            var dialog = new MessageDialog("Com Device found");
            await dialog.ShowAsync();

            DeviceInformation deviceInfo = devices[0];
            SerialDevice serialDevice = await SerialDevice.FromIdAsync(deviceInfo.Id);
            serialDevice.BaudRate = 9600;
            serialDevice.DataBits = 8;
            serialDevice.StopBits = SerialStopBitCount.One;
            serialDevice.Parity = SerialParity.None;
        }
        else
        {
            MessageDialog popup = new MessageDialog("Sorry, no device found.");
            await popup.ShowAsync();
        }

        // After com port is found, search for device
        foreach (DeviceInformation di in await DeviceInformation.FindAllAsync(BluetoothLEDevice.GetDeviceSelector()))
        {
            BluetoothLEDevice bleDevice = await BluetoothLEDevice.FromIdAsync(di.Id);

            // Display BLE device name
            var dialogBleDeviceName = new MessageDialog("BLE Device Name " + bleDevice.Name);
            await dialogBleDeviceName.ShowAsync();

            myDevice = bleDevice;
        }

        // Check device connection
        myDevice.ConnectionStatusChanged += OnConnectionStatusChanged;

        foreach (var service in myDevice.GattServices)
        {
            serviceList.Add(service.Uuid.ToString());

            // Verify if service is discovered by displaying a popup
            MessageDialog serviceUuidPopUp = new MessageDialog("Adding Service Uuid to list " + service.Uuid.ToString() );
            await serviceUuidPopUp.ShowAsync();

            foreach (var characteristic in service.GetAllCharacteristics())
            {
                var characteristicUuid = characteristic.Uuid.ToString().ToLowerInvariant();
                characteristicList.Add(characteristicUuid);

                // Verify if characteristic is discovered by displaying a popup 
                MessageDialog charUuidPopUp = new MessageDialog("Adding characteristic Uuid to list " + characteristicUuid);
                await charUuidPopUp.ShowAsync();

                // set value changed event handlers for characteristics
                characteristic.ValueChanged += Oncharacteristic_ValueChanged;

            }
        }
    }

    private void OnConnectionStatusChanged(BluetoothLEDevice sender, object args)
    {
        if (sender.ConnectionStatus == BluetoothConnectionStatus.Connected)
        {
            System.Diagnostics.Debug.WriteLine("Connected");
        }
        else
        {
            System.Diagnostics.Debug.WriteLine("Disconnected");
        }
    }    

    private void Oncharacteristic_ValueChanged(GattCharacteristic sender, GattValueChangedEventArgs args)
    {
        byte[] data = new byte[args.CharacteristicValue.Length];
        DataReader.FromBuffer(
            args.CharacteristicValue).ReadBytes(data);
        string text = Encoding.UTF8.GetString(data, 0, data.Length);
    }

更新 1 我尝试在设置值更改事件处理程序之前检查特性属性,方法是按照rudi belt 在 SO 上给出的答案

if (characteristic.CharacteristicProperties == (GattCharacteristicProperties.Read | GattCharacteristicProperties.Notify))
                {
                    characteristic.ValueChanged += Oncharacteristic_ValueChanged;
                }   

不幸的是,这个 IF 语句没有被执行。

更新 2 我试图删除Oncharacteristic_ValueChanged方法中的所有代码。但它仍然给我同样的错误

System.ArgumentException: 'Value does not fall within the expected range.'

我花了很多时间试图解决这个问题。如果有人可以帮助我,我将非常高兴。谢谢!

4

1 回答 1

1

阅读您在前一个问题中的努力,我可以提供一个工作示例,但首先要进行一些解释。 myDevice.ConnectionStatusChanged不是必需的,它仅用于通知连接丢失或连接。您必须先连接到您的设备并在连接方法中处理事情。

成功连接后,您必须获得包含您要用于读取、写入、通知或指示的特征的服务。

选择服务后,您可以获得该服务的特征。

通过Uuid选择特征,或者在我的示例中选择CharacteristicProperties.HasFlag。在我的示例中,这个标志是Notify。在代码注释中,您可以找到额外的信息。

using System;
using System.Diagnostics;
using System.Threading.Tasks;
using Windows.Devices.Bluetooth;
using Windows.Devices.Bluetooth.GenericAttributeProfile;
using Windows.Devices.Enumeration;
using Windows.UI.Popups;
using Windows.UI.Xaml.Controls;

namespace App1
{
public sealed partial class MainPage : Page
{
   GattDeviceServicesResult serviceResult = null;
  private BluetoothLEDevice myDevice;
  private GattCharacteristic selectedCharacteristic;

  public MainPage()
  {
     this.InitializeComponent();
     ConnectDevice();
  }

  private async void ConnectDevice()
  {
     //This works only if your device is already paired!
     foreach (DeviceInformation di in await DeviceInformation.FindAllAsync(BluetoothLEDevice.GetDeviceSelector()))
     {
        BluetoothLEDevice bleDevice = await BluetoothLEDevice.FromIdAsync(di.Id);
        // Display BLE device name
        var dialogBleDeviceName = new MessageDialog("BLE Device Name " + bleDevice.Name);
        await dialogBleDeviceName.ShowAsync();
        myDevice = bleDevice;
     }

     if (myDevice != null)
     {
        int servicesCount = 3;//Fill in the amount of services from your device!!!!!
        int tryCount = 0;
        bool connected = false;
        while (!connected)//This is to make sure all services are found.
        {
           tryCount++;
           serviceResult = await myDevice.GetGattServicesAsync();
           if (serviceResult.Status == GattCommunicationStatus.Success && serviceResult.Services.Count >= servicesCount)
           {
              connected = true;
              Debug.WriteLine("Connected in " + tryCount + " tries");
           }
           if (tryCount > 5)//make this larger if faild
           {
              Debug.WriteLine("Failed to connect to device ");
              return;
           }
        }
        if (connected)
        {
           for (int i = 0; i < serviceResult.Services.Count; i++)
           {
              var service = serviceResult.Services[i];
              //This must be the service that contains the Gatt-Characteristic you want to read from or write to !!!!!!!.
              string myServiceUuid = "0000ffe0-0000-1000-8000-00805f9b34fb";
              if (service.Uuid.ToString() == myServiceUuid)
              {
                 Get_Characteriisics(service);
                 break;
              }
           }
        }
     }
  }
  private async void Get_Characteriisics(GattDeviceService myService)
  {
     var CharResult = await myService.GetCharacteristicsAsync();
     if (CharResult.Status == GattCommunicationStatus.Success)
     {
        foreach (GattCharacteristic c in CharResult.Characteristics)
        {
           if (c.CharacteristicProperties.HasFlag(GattCharacteristicProperties.Notify))
           {
              selectedCharacteristic = c;
              break;
           }
        }
        try
        {
           // Write the ClientCharacteristicConfigurationDescriptor in order for server to send notifications.               
           var result = await selectedCharacteristic.WriteClientCharacteristicConfigurationDescriptorAsync(
                                                     GattClientCharacteristicConfigurationDescriptorValue.Notify);
           if (result == GattCommunicationStatus.Success)
           {
              var dialogNotifications = new MessageDialog("Successfully registered for notifications");
              await dialogNotifications.ShowAsync();
              selectedCharacteristic.ValueChanged += SelectedCharacteristic_ValueChanged;
           }
           else
           {
              var dialogNotifications = new MessageDialog($"Error registering for notifications: {result}");
              await dialogNotifications.ShowAsync();
           }
        }
        catch (Exception ex)
        {
           // This usually happens when not all characteristics are found
           // or selected characteristic has no Notify.
           var dialogNotifications = new MessageDialog(ex.Message);
           await dialogNotifications.ShowAsync();
           await Task.Delay(100);
           Get_Characteriisics(myService); //try again
           //!!! Add a max try counter to prevent infinite loop!!!!!!!
        }
     }
     else
     {
        var dialogNotifications = new MessageDialog("Restricted service. Can't read characteristics");
        await dialogNotifications.ShowAsync();
     }
  }

  private void SelectedCharacteristic_ValueChanged(GattCharacteristic sender, GattValueChangedEventArgs args)
  {

  }
 }
}

如果您对此代码有任何问题,请随时在评论中提问。

于 2017-06-11T16:27:52.773 回答