【问题标题】:C# UWP "Subscribe to value changes"C# UWP“订阅值更改”
【发布时间】:2018-05-27 02:10:56
【问题描述】:
private async void Characteristic_ValueChanged(GattCharacteristic sender, GattValueChangedEventArgs args)
        {

            // BT_Code: An Indicate or Notify reported that the value has changed.
            // Display the new value with a timestamp.
            var newValue = FormatValueByPresentation(args.CharacteristicValue, presentationFormat);
            var message = $"Value at {DateTime.Now:hh:mm:ss}: {newValue}";
            await Dispatcher.RunAsync(CoreDispatcherPriority.Normal,
                () => CharacteristicLatestValue.Text = message);
        }

这是来自 github 的示例。通用 BluetoothLE 示例。我现在面临一个问题,如果我点击“阅读”按钮,它会显示我的 rfduino 阅读。但是因为我希望它在值更改时自动更新,所以我需要订阅值更改。

但是,当我单击“订阅值更改”时,我收到的只是“未知格式”。任何专家都可以帮助我逐步解决这个问题吗?如果可能,我们可以使用 Teamviewer。

private async void CharacteristicReadButton_Click()
        {
             // BT_Code: Read the actual value from the device by using Uncached.
            GattReadResult result = await selectedCharacteristic.ReadValueAsync(BluetoothCacheMode.Uncached);
            if (result.Status == GattCommunicationStatus.Success)
            {
                string formattedResult = FormatValueByPresentation(result.Value, presentationFormat);

                rootPage.NotifyUser($"Read result: {formattedResult}", NotifyType.StatusMessage);  
            }
            else
            {
                rootPage.NotifyUser($"Read failed: {result.Status}", NotifyType.ErrorMessage);
            }     
        }

【问题讨论】:

    标签: c# uwp bluetooth-lowenergy


    【解决方案1】:

    首先,如果特征具有 Indicatable 或 Notifiable 属性,您可以订阅特征值更改通知。您的阅读内容并不总是相同的。它可以是其他特征。

    一旦您找到了您的特征,您必须编写该特征的 ClientConfigurationDescriptor,并将 Indicatable 或 Notifiable 属性设置为 True。它通知 GATT 服务器客户端对此特性感兴趣。

    只有您可以订阅并收到通知。此外,一旦通知到达,您无需从特征原因通知事件中读取新特征值。

    【讨论】:

      【解决方案2】:

      您的编码技能不足以理解正在发生的事情。
      打破您在 Rfuino 和您的 uwp 应用程序之间交换价值的愿望。
      要发送值,您必须制定一些协议才能知道有效消息何时到达。
      如果 Rfuino 允许您发送字节数组,则执行发送起始字节之类的操作 您的值作为字节和结束字节。像 STX,值 X,值 Y,值 Z,ETX .(2,255,255,255,3)。
      如果在接收端您阅读了类似 2、148,250,180,3 的消息,那么您就知道收到了一条有效消息。
      我对Rfuino了解不多,但大多数BLE设备都可以发送这样的消息。
      此外,UWP 示例对于您来说理解起来很复杂。
      在您的 UWP 应用程序中,您可以检查您的消息是否有 5 个字节的长度并且以 2 开头并以 3 结尾。 看看我在 GitHub 上的简单 Ble 示例:https://github.com/GrooverFromHolland/SimpleBleExample_by_Devicename
      如果您对该示例有疑问或想要扩展它,请在 GitHub 上提出问题。 停止在 Stackoverflow 上就这个主题提问,你会得到一些答案,但我们不是来教你如何编码的。

      【讨论】:

      • 如果您对示例有疑问,请在 Github 上发布问题。
      猜你喜欢
      • 1970-01-01
      • 2018-12-13
      • 1970-01-01
      • 1970-01-01
      • 2020-07-27
      • 1970-01-01
      • 1970-01-01
      • 2021-10-05
      • 2021-01-28
      相关资源
      最近更新 更多