Forum Xamarin.Forms
We are excited to announce that the Xamarin Forums are moving to the new Microsoft Q&A experience. Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

We encourage you to head over to Microsoft Q&A for .NET for posting new questions and get involved today.

Streaming audio through Bluetooth

I have a Bluetooth connection that works ok. When connected, I call this write function, that sends a stream to another device. It goes like this:

public void Write(byte[] bytes)
{
    System.Threading.Tasks.Task.Run(() =>
    {
        int offset = 0;
        int count = 10;
        int len = bytes.Length;

        while (offset < len)
        {
            try
            {
                mmOutStream.Write(bytes, offset, Math.Min(count, len - offset));
                offset += count;
            }
            catch (IOException ex)
            {
                System.Diagnostics.Debug.WriteLine("Error occurred when sending data", ex);
            }
        }
    }).ConfigureAwait(false);
}

This should stream a byte array of 10 bytes. Then On onother device, I call this read method:

public void Read()
{
    System.Threading.Tasks.Task.Run(() =>
    {
        MediaPlayer player = new MediaPlayer();
        try
        {
            byte[] myReadBuffer = new byte[1024];
            int numberOfBytesRead = 0;
            do
            {
                numberOfBytesRead = mmInStream.Read(myReadBuffer, 0, myReadBuffer.Length);
                player.Prepared += (sender, e) =>
                {
                    player.Start();
                };
                player.SetDataSource(new StreamMediaDataSource(new System.IO.MemoryStream(myReadBuffer)));
                player.Prepare();
            }
            while (mmInStream.IsDataAvailable());
        }
        catch (IOException ex)
        {
            System.Diagnostics.Debug.WriteLine("Input stream was disconnected", ex);
        }
    }).ConfigureAwait(false);
}

The StreamMediaDataSource works fine, if I put the entire array in, but this return the Unable to resolve superclass of Lmd5c539bdc79f76d0c80e6cd44011eba829/StreamMediaDataSource; (388)

The method looks like this:

public class StreamMediaDataSource : MediaDataSource
{
    System.IO.Stream data;

    public StreamMediaDataSource(System.IO.Stream Data)
    {
        data = Data;
    }

    public override long Size
    {
        get
        {
            return data.Length;
        }
    }

    public override int ReadAt(long position, byte[] buffer, int offset, int size)
    {
        data.Seek(position, System.IO.SeekOrigin.Begin);
        return data.Read(buffer, offset, size);
    }

    public override void Close()
    {
        if (data != null)
        {
            data.Dispose();
            data = null;
        }
    }

    protected override void Dispose(bool disposing)
    {
        base.Dispose(disposing);

        if (data != null)
        {
            data.Dispose();
            data = null;
        }
    }
}
Sign In or Register to comment.