I have an activity (with an ISurfaceTextureListener) with an async OnSurfaceTextureUpdated method in which I call the async SendPixel method:
public async void OnSurfaceTextureUpdated(SurfaceTexture surface)
{
bool sendPixel = false;
[...]
if (Settings.Pixel.period >= 0)
{
if (stopWatch.ElapsedMilliseconds >= Settings.Pixel.nextSending)
{
if (Settings.Pixel.period > 0)
Settings.Pixel.nextSending += Settings.Pixel.period * ((int)((stopWatch.ElapsedMilliseconds - Settings.Pixel.nextSending) / Settings.Pixel.period) + 1);
sendPixel = true;
}
}
[...]
if (sendPixel)
await SendPixel();
}
Here is the async SendPixel method:
async Task SendPixel()
{
try
{
if (Ev3Messaging.IsConnected())
{
Color color = new Color(bm.GetPixel(Settings.Camera.viewport.X_VpToCv(Settings.Pixel.x), Settings.Camera.viewport.X_VpToCv(Settings.Pixel.y)));
Ev3Messaging.AddNumberMessage("MC_Hue", color.GetHue());
Ev3Messaging.AddNumberMessage("MC_Brightness", color.GetBrightness());
Console.WriteLine("SendPixel");
await Ev3Messaging.SendMessages();
Console.WriteLine("SendPixelEnd");
}
}
catch (Exception ex)
{
Console.WriteLine("Error SendPixel: " + ex.Message);
}
}
The ISurfaceTextureListener reads a video stream, then the SendPixel method is called many times. The issue is that the console well displays "SendPixel", but doesn't display "SendPixelEnd", and no error is thrown. Afterwards, when I close the activity, every missing "SendPixelEnd" are displayed.
EDIT: The EV3Messaging.SendMessages sends the messages (by Bluetooth) added in the previous lines:
static List<byte> btMessage = new List<byte>();
static public async Task SendMessages()
{
if (_socket != null)
await _socket.OutputStream.WriteAsync(btMessage.ToArray(), 0, btMessage.Count);
}
Why does that happen and how can I solve that ?
Thanks in advance.
PS: Sorry for my poor English ^^