读取值自动更新UWA / UWP

时间:2018-05-24 06:00:35

标签: c# uwp bluetooth-lowenergy

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);
    }
}

我遇到了读取值更改的问题。我用我的rfduino设置了mpu5060,所以每当我的rfduino移动时,它会在我的Arduino串口显示器上显示角度。

对于我的c#,当我按下“读取”按钮时,我可以读取该值。但是如果我的值(角度)发生变化,它将不会自动更新以通知用户。我必须再次手动点击“阅读”按钮进行更改。 如何让它自动更新?

2 个答案:

答案 0 :(得分:1)

您需要创建计时器线程来监控您的值的变化。

看到这个 Timer class

答案 1 :(得分:0)

为什么不设置你的RfDuino来发送通知,如果可能的话 或者甚至将您的数据添加为广告,然后就不需要连接,只需在OnAdvertisementReceived中使用。

在你的RfDuino中这样做:

void advertise(const char *data, uint32_t ms)
{
  // this is the data we want to appear in the advertisement
  // /make sure that the RfDuino name + data is not more than 31 bytes
  RFduinoBLE.advertisementData = data;

  // start the BLE stack
  RFduinoBLE.begin();

  // advertise for ms milliseconds
  RFduino_ULPDelay(ms);

  // stop the BLE stack
  RFduinoBLE.end();
}

如果您想发送新数据电话:

// advertise "data" for indicated time
advertise("your data", duration);