[UWP] [C#]自动更新读取值 [英] [UWP][C#] auto update read value
问题描述
private async void CharacteristicsReadButton_Click()
{
// BT_Code:使用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#,我可以阅读当我按下"阅读"时的值按钮。但是如果我的值(角度)发生变化,它将不会自动更新以通知用户。我必须手动点击"阅读"按钮再次更改。 如何让
自动更新?
您好yann123,
在此处填写文件:订阅通知,您可以订阅 GattCharacteristic.ValueChanged
事件。
您可以使用以下内容代码作为示例:
GattCommunicationStatus status = await selectedCharacteristic.WriteClientCharacteristicConfigurationDescriptorAsync(
GattClientCharacteristicConfigurationDescriptorValue.Notify);
if(status == GattCommunicationStatus.Success)
{
//服务器已被告知客户的兴趣。
}
characteristic.ValueChanged + = Characteristic_ValueChanged;
void Characteristics_ValueChanged(GattCharacteristic sender,
GattValueChangedEventArgs args)
{
//指示或通知报告值已更改。
var reader = DataReader.FromBuffer(args.CharacteristicValue)
//解析所需的数据。
}
祝你好运,
罗伊
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); }
I'm facing a problem with the read value change. I have setup mpu5060 with my rfduino, so whenever my rfduino moved, it will display the angle on my Arduino serial monitor.
for my c#, I can read the value when I press the "read" button. But if my value(angle) change, it will not auto update to notify user. I have to manually click the "read" button again to change. How do I make it auto update ?
Hi yann123,
Form the document here:Subscribing for notifications, you could subscribe the GattCharacteristic.ValueChanged Event after write to Client Characteristic Configuration Descriptor (CCCD).
You could use the following code as example:
GattCommunicationStatus status = await selectedCharacteristic.WriteClientCharacteristicConfigurationDescriptorAsync( GattClientCharacteristicConfigurationDescriptorValue.Notify); 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. }Best regards,
Roy
这篇关于[UWP] [C#]自动更新读取值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!