0

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

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

提前感谢您的帮助

马塞尔

4

2 回答 2

0

并非所有特征都是指示的。

我没有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";
         }
      }
于 2017-05-31T20:16:51.713 回答
0

我找到了我的问题的答案。这不是 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/

希望这会帮助某人。

于 2017-06-02T07:03:02.917 回答