Forum Xamarin.Forms

How to wait for an event to happen?

Hi,
I am trying to develop an app which uses BLE to communicate with another device. My app will send something over BLE which requests data from the other device. The device will then send the requested data back and I will display it on my app. The problem is that the other device sometimes is a bit slow to respond so my app will try and display the data before it has actually been received. I therefore want to wait for the BLE receive event before displaying anything on my app, but I am unsure how to do this.

The BLE received is currently being handled by this event handler (but this does not wait for the event to happen):

try
{
    App.readCharacteristic.ValueUpdated += (o, args) =>
    {
    LiveReadingsRxBytes = args.Characteristic.Value;
    };

    await App.readCharacteristic.StartUpdatesAsync();
}
catch (Exception ex)
{
    await DisplayAlert("Notice", ex.Message.ToString(), "Error !");
} 

//then display the results stored in LiveReadingsRxBytes

Thanks,
Sophie

Best Answers

Answers

  • JarvanJarvan Member, Xamarin Team Xamurai

    Use async-await and put the try-catch code in a task method.

    ...
    await ReceiveMethod();
    //then display the results stored in LiveReadingsRxBytes
    ...
    private async Task<object> ReceiveMethod()
    {
        try
        {
            App.readCharacteristic.ValueUpdated += (o, args) =>
            {
                LiveReadingsRxBytes = args.Characteristic.Value;
            };
    
            await App.readCharacteristic.StartUpdatesAsync();
        }
        catch (Exception ex)
        {
            await DisplayAlert("Notice", ex.Message.ToString(), "Error !");
        }
        return null;
    }
    
  • SophieHerSophieHer Member ✭✭

    Thanks for your reply @Jarvan, unfortunatley it does not seem to work :/ I am getting the same error where it says:
    "Parameter cannot be null. Parameter name: value"
    Which I believe is caused by the app trying to display the rx values before they have been received.

  • SophieHerSophieHer Member ✭✭
    edited September 2019

    @JohnHardman,
    Thanks for your reply, I tried doing it how you said:

                try
                {
                    App.readCharacteristic.ValueUpdated += (o, args) =>
                    {
                        try
                        {
                            LiveReadingsRxBytes = args.Characteristic.Value;
    
                            if (LiveReadingsRxBytes != null)
                            {
                                temp = BitConverter.ToInt16(LiveReadingsRxBytes, 0) / 16;//LiveReadingsRxBytes[0] << 8 | LiveReadingsRxBytes[1];
                                pH = (float)BitConverter.ToUInt32(LiveReadingsRxBytes, 2) / 1000;
                                turbidity = LiveReadingsRxBytes[6];
                                conductivity = LiveReadingsRxBytes[7];
    
                                TempValueLabel.Text = $"{temp} °C";
                                pHValueLabel.Text = $"{pH}";
                                TurbidityValueLabel.Text = $"{turbidity} mg/L";
                                ConductivityValueLabel.Text = $"{conductivity} μS/cm";
                                LiveReadingsRxBytes = null;
                            }
                        }
                            catch (Exception ex)
                    {
                        DisplayAlert("Notice", ex.Message.ToString(), "Error !");
                    }
                };
    
                    await App.readCharacteristic.StartUpdatesAsync();
                }
                catch (Exception ex)
                {
                    await DisplayAlert("Notice", ex.Message.ToString(), "Error !");
                }
    

    (Sorry about the indenting, the code does not seem to copy from VS very well)

    But when I get to the TempValueLabel.Text = $"{temp} °C"; line the following exception is thrown:
    Android.Util.AndroidRuntimeException: Only the original thread that created a view hierarchy can touch its views.

    Any ideas on how to fix this?

  • SophieHerSophieHer Member ✭✭

    Thanks @JohnHardman that works exactly like I wanted it to! :)

Sign In or Register to comment.