none
[UWP][C#] auto update read value RRS feed

  • Question

  • 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 ?


    Thursday, May 24, 2018 7:51 AM

All replies

  • 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


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, May 25, 2018 2:09 AM
    Moderator
  • Hi,

    Any updates for the problem? Does the answer work for you?

    Best regards,

    Roy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, June 13, 2018 12:58 AM
    Moderator