Developing Bluetooth App For Raspberry Pi3 on Win IOT
<p>I am new at developing softwares for Windows IOT. I have information about .Net 3.5 and 4. When I start to develop for Win IOT, I saw that lots of things have changed. There are a lot of new words async, await, Task etc.</p>

<p>Now I want to read and write data from bluetooth. I can do it but if I try to write and read data in a infinite loop it throws exceptions.</p>

<p>I have 2 functions</p>

<p>Read : </p>

<pre><code>    private async Task ReadAsync(CancellationToken cancellationToken)
    {
        uint ReadBufferLength = 1024;

        Task&lt;UInt32&gt; loadAsyncTask;

        // If task cancellation was requested, comply
        cancellationToken.ThrowIfCancellationRequested();

        // Set InputStreamOptions to complete the asynchronous read operation when one or more bytes is available
        dataReaderObject.InputStreamOptions = InputStreamOptions.Partial;

        // Create a task object to wait for data on the serialPort.InputStream
        loadAsyncTask = dataReaderObject.LoadAsync(ReadBufferLength).AsTask(cancellationToken);

        // Launch the task and wait
        UInt32 bytesRead = await loadAsyncTask;
        if (bytesRead &gt; 0)
        {
            receivedData = dataReaderObject.ReadString(bytesRead);
        }
        else
        {
            receivedData = "";
        }
    }
</code></pre>

<p>Write :</p>

<pre><code>    private async Task SendData(string data)
    {
        if (deviceService != null)
        {
            //send data
            if (string.IsNullOrEmpty(data))
            {
                uiTxtError.Text = "Please specify the string you are going to send";
            }
            else
            {
                //DataWriter dwriter = new DataWriter(streamSocket.OutputStream);              
                UInt32 len = dwriter.MeasureString(data);
                dwriter.WriteUInt32(len);
                dwriter.WriteString(data);
                await dwriter.StoreAsync();
                await dwriter.FlushAsync();


            }

        }
        else
        {
            uiTxtError.Text = "Bluetooth is not connected correctly!";
        }
    }
</code></pre>

<p>Function that use read and write.</p>

<pre><code>        await SendData("010C" + Environment.NewLine);
        await ReadAsync(ReadCancellationTokenSource.Token);


        if (!string.IsNullOrEmpty(receivedData))
        {
            string[] splitted = receivedData.Replace("\r", "").Replace("&gt;", "").Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
            int a = 0;
            int b = 0;
            if (int.TryParse(splitted[splitted.Length - 1], System.Globalization.NumberStyles.HexNumber, null as IFormatProvider, out b))
            {

                if(int.TryParse(splitted[splitted.Length - 2], System.Globalization.NumberStyles.HexNumber, null as IFormatProvider, out a))
                {
                    uiGaugeRpm.Value = ((a * 256) + b) / 4;
                }

            }

            receivedData = "";

        }
</code></pre>

<p>I call function above in a infinite loop with 200 ms delays (Task.Delay(200)). </p>

<pre><code>        ReadCancellationTokenSource = new CancellationTokenSource();
        if (streamSocket.InputStream != null)
        {
            dataReaderObject = new DataReader(streamSocket.InputStream);

            try
            {
                while (true)
                {
                    await GetRPM();
                    await Task.Delay(200);
                }
            }
            catch (Exception excp)
            {
                MessageDialog dialog = new MessageDialog(excp.Message);
                await dialog.ShowAsync();
            }


        }
</code></pre>

<p>After loop starts, it gets data true and fine, but a few loop later it throws exceptions.</p>

<p>If I create datawriter object in write function it throws unhandled exception and stops application. I solve this by creating this object only one time after connection. Now I get new exception. </p>

<p>loadAsyncTask = dataReaderObject.LoadAsync(ReadBufferLength).AsTask(cancellationToken);</p>

<p>At this line I get ObjectDisposedException, I observed the objects but I could not see anything disposed.</p>

<p>I am connecting to ELM327 bluetooth device with rfcomm protocol. I am newbie at developing for Win IOT. </p>

<p>Also I could not use bindings with async functions.</p>

<p>Please, could you help me?</p>