I have a program that finds a peer and connects to it (arduino with hc-05 bluetooth module). I wrote a function that receives data from this bluetooth.

private async void read() { try { uint bytesRead = await dataReader.LoadAsync(sizeof(uint)); if (bytesRead > 0) { uint strLenght = (uint)dataReader.ReadUInt32(); bytesRead = await dataReader.LoadAsync(strLenght); if (bytesRead > 0) { String message = dataReader.ReadString(strLenght); messageTextBlock.Text = message.ToString(); read(); } } } catch (Exception ex) { messageTextBlock.Text = ex.Message; } } 

But it does not work. What could be the error, or can use another method?

    1 answer 1

    Try this

     private async void read() { try { uint bytesRead = await dataReader.LoadAsync(sizeof(uint)); bytesRead.AsTask.Wait(); uint count = bytesRead.GetResults(); if (count == sizeof(int)) { uint strLenght = (uint)dataReader.ReadUInt32(); bytesRead = await dataReader.LoadAsync(strLenght); bytesRead.AsTask.Wait(); count = bytesRead.GetResults(); if (count == 4) { String message = dataReader.ReadString(strLenght); messageTextBlock.Text = message.ToString(); read(); }}} catch (Exception ex) { messageTextBlock.Text = ex.Message; }} 
    • gives an error in bytesRead, AsTask.Wait (); WindowsRuntimeSystemExtensions.AsTask (IAsyncAction) ' GerrDott pm
    • bug fixed IAsyncOperation <uint> bytesRead = dataReader.LoadAsync (sizeof (uint)); and bytesRead.AsTask (). Wait (); But it gives the error Object Reference not set - GerrDott
    • @GerrDott Well, the object instance is not created, then dataReader.LoadAsync(sizeof(uint)); gives null most likely. Something I did not see that the question was asked long ago, although one of the first in the list: W - Herrgott