BLE 指示 UWP GATT 客户端

BLE Indicate UWP GATT Client

我想知道 UWP 蓝牙是否有问题 API 和 Indicate。 如果我正确理解 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.
}

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

提前感谢您的帮助

马塞尔

并不是所有的特性都是指示。

我没有 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 当前电池电量信息。 Read/notify 特征。

DeviceName 0x2a00 设备名称数据。 Read/write 特征。

此外,最好使用 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";
         }
      }

我找到了问题的答案。这不是 UWP 的问题,而是 Myo 的问题。上面的代码适用于 Indicate,只需将通知更改为 indicate 即可。

对于未来的其他人。我在命令字节上犯了一个错误。 我误解了蓝牙 Header 文件并认为有效载荷等于命令,但事实并非如此。所以在每个命令字节之后你必须发送数量字节,你给出 "argument"。这是有效载荷。它在 header 中说过,但我不知何故错过了它。

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

01 03 00 03 01

其中第一个 01 是 set_mode, 第一个 03 有效载荷(3 "Arguments"), 00 EMG_none, 第二个 03 IMU_send_all, 最后 01 Classifier_enabled.

希望他们在教程中制作了示例命令:-)

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

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

希望对某人有所帮助。