【问题标题】:BLE Indicate UWP GATT ClientBLE 指示 UWP GATT 客户端
【发布时间】:2017-11-01 03:38:49
【问题描述】:

我想知道,UWP 蓝牙 API 和指示是否有问题。 如果我正确理解documentation,UWP 将处理收到的指示包的确认。 但是由于某种原因,示例代码适用于通知,但不适用于指示。我正在尝试使用 Myo 腕带。 我可以通过通知特性接收通知,但不能通过指示特性接收通知。不幸的是,我必须使用指示。

我将示例代码稍微改了一下,但它不起作用:

GattCommunicationStatus status = await selectedCharacteristic.WriteClientCharacteristicConfigurationDescriptorAsync(
    GattClientCharacteristicConfigurationDescriptorValue.Indicate);

if(status == GattCommunicationStatus.Success)
{
    // Server has been informed of clients interest.
}

并且处理程序保持不变:

characteristic.ValueChanged += Characteristic_ValueChanged;
// ... 
void Characteristic_ValueChanged(GattCharacteristic sender, 
                                    GattValueChangedEventArgs args)
{
    // An Indicate or Notify reported that the value has changed.
    var reader = DataReader.FromBuffer(args.CharacteristicValue)
    // Parse the data however required.
}

任何想法我做错了什么?设备已连接并正确编程,它会发送通知。

提前感谢您的帮助

马塞尔

【问题讨论】:

    标签: notifications uwp bluetooth-lowenergy gatt myo


    【解决方案1】:

    我找到了我的问题的答案。这不是 UWP 的问题,而是 Myo 的问题。上面的代码适用于指示,只需将通知更改为指示和你的好去。

    为未来的其他所有人。我在命令字节上犯了一个错误。 我误解了蓝牙头文件并认为有效负载等于命令,但事实并非如此。因此,在每个命令字节之后,您必须发送数量字节,作为“参数”给出。这是有效载荷。它在标题中说,但我不知何故错过了它。

    例如,要将 myo 设置为 EMG_none、IMU_send_all、Classifier_Enabled,您必须将此字节发送到 CommandCharacteristic:

    01 03 00 03 01
    

    其中第一个 01 是 set_mode, 前 03 有效载荷(3 个“参数”), 00 EMG_none, 第二个 03 IMU_send_all, 最后 01 Classifier_enabled。

    希望他们在教程中做了一个示例命令 :-)

    完整的标题可以在这里找到:https://github.com/thalmiclabs/myo-bluetooth/blob/master/myohw.h

    这里有一个简短的解释:http://developerblog.myo.com/myo-bluetooth-spec-released/

    希望这对某人有所帮助。

    【讨论】:

    • 是的,忘记了。谢谢
    【解决方案2】:

    并非所有特征都显示。

    我没有MYO,但做了一些研究,发现了一个具有MYO特点的列表:

    ControlService 0x0001 Myo 信息服务

    MyoInfoCharacteristic 0x0101 此 Myo 的序列号和特定于此固件的各种参数。只读属性。

    FirmwareVersionCharacteristic 0x0201 当前固件版本。只读特性。

    CommandCharacteristic 0x0401 向 Myo 发出命令。只写特性。

    ImuDataService 0x0002 IMU 服务

    IMUDataCharacteristic 0x0402 仅通知特性。

    MotionEventCharacteristic 0x0502 运动事件数据。仅供参考。

    ClassifierService 0x0003 分类器事件服务。

    ClassifierEventCharacteristic 0x0103 分类器事件数据。仅供参考。

    EmgDataService 0x0005 原始 EMG 数据服务。

    EmgData0Characteristic 0x0105 原始 EMG 数据。仅通知特性。

    EmgData1Characteristic 0x0205 原始 EMG 数据。仅通知特性。

    EmgData2Characteristic 0x0305 原始 EMG 数据。仅通知特性。

    EmgData3Characteristic 0x0405 原始 EMG 数据。仅通知特性。

    BatteryService 0x180f 电池服务

    BatteryLevelCharacteristic 0x2a19 当前电池电量信息。读取/通知特征。

    DeviceName 0x2a00 设备名称数据。读/写特性。

    另外,最好使用 Ibuffer 而不是 DataReader。我认为MYO发送的数据是BigEndian。使用 Ibuffer 更容易更改编码。 以下是如何使用 Ibuffer 的示例:

        private async void Characteristic_ValueChanged(GattCharacteristic sender,GattValueChangedEventArgs args)
          {         
             var newValue = FormatValue(args.CharacteristicValue);
             await Task.Run(() => Process_received(newValue));
      }
    
     private string FormatValue(IBuffer buffer)//using Windows.Storage.Streams;
          {
              CryptographicBuffer.CopyToByteArray(buffer, out byte[] data);//using Windows.Security.Cryptography;
             try
             {
               // return Encoding.BigEndianUnicode.GetBytes(data) gives char array
               // return Encoding.UTF32.GetString(data)
                return Encoding.ASCII.GetString(data);
             }
             catch (ArgumentException)
             {
                return "Unknown format";
             }
          }
    

    【讨论】:

    • 感谢您的回答。我知道蓝牙标题列表。我可以阅读每个 Notfiy Characterisitc(例如 EMG 特征)。但我需要从 ClassifierEventCharacteristic 获取数据,这是一个指示。问题不在于解码,我的处理程序甚至没有被触发。但是感谢 IBuffer 的提示,将在我的其余代码中改变它:-)
    • 我唯一能想到的是,您需要为每个特征使用不同的事件处理程序。您是否尝试过仅指示?在我能找到的所有样本中,Characteristic_ValueChanged 首先被删除,然后在更改通知或指示后重新添加,如果你想同时听的话,这没有任何意义。
    猜你喜欢
    • 1970-01-01
    • 2017-01-24
    • 1970-01-01
    • 1970-01-01
    • 2017-06-25
    • 1970-01-01
    • 2023-03-24
    • 2016-05-17
    • 2015-01-20
    相关资源
    最近更新 更多