InTheHand Bluetooth - Weird stream problems

Search This thread

scilor

Senior Member
Jan 5, 2008
1,270
36
@scilor.com
www.scilor.com
I am currently working on my "SciLor's WiMoBlue". The new protocol is ready. Everything works fine, until I send much data at the same time.

For example if I try to send an image in that way:

Code:
    Public Sub SendImage(ByVal Image As Bitmap, ByVal Position As Point, ByVal Format As ImageFormat)
        Dim PosX, PosY, Width, Height As Byte()
        Dim ImageStream As New IO.MemoryStream
        Dim ImageLength As Integer
        Dim ImageLengthBytes As Byte()
        Dim ImageBuffer(MaxChunkSize - HeaderSize - 1) As Byte

        PosX = BitConverter.GetBytes(Position.X)
        PosY = BitConverter.GetBytes(Position.Y)
        Width = BitConverter.GetBytes(Image.Width)
        Height = BitConverter.GetBytes(Image.Height)

        Image.Save(ImageStream, ImageFormat2ImagingFormat(Format))
        ImageStream.Seek(0, SeekOrigin.Begin)
        ImageLength = ImageStream.Length
        ImageLengthBytes = BitConverter.GetBytes(ImageLength)

        SendData(BuildCommand(BaseProtocol.Image, ImageProtocol.Initiate, CombineBytes(PosX, PosY, Width, Height, ImageLengthBytes)))
        Thread.Sleep(2000)
        Dim DataPos As Integer
        For DataPos = 0 To ImageStream.Length - MaxChunkSize - HeaderSize - 1 Step MaxChunkSize - HeaderSize
            ImageStream.Read(ImageBuffer, 0, MaxChunkSize - HeaderSize)
            SendData(BuildCommand(BaseProtocol.Image, ImageProtocol.Data, ImageBuffer))
            'WaitForNextChunk = True
            'Do While WaitForNextChunk = True
            'Loop
            'Thread.Sleep(2000)
        Next

        ImageBuffer = New Byte(ImageLength - DataPos - 1) {}
        ImageStream.Read(ImageBuffer, 0, ImageBuffer.Length)
        SendData(BuildCommand(BaseProtocol.Image, ImageProtocol.End, ImageBuffer))
    End Sub

SendData:
Code:
        If Data.Length > MaxChunkSize Then
            MsgBox("Data to long... " & vbNewLine & "SendSize:" & Data.Length & vbNewLine & "MaxSize:" & MaxChunkSize)
        Else
            If btClient.Connected = True And isRecieving = True Then
                btStream.Write(Data, 0, Data.Length)
                btStream.Flush()
            End If
        End If

Recieving Part:
Code:
        While isRecieving = True
            If btStream IsNot Nothing And btStream.DataAvailable = True Then
                Try
                    Recieved = btStream.Read(myHeader, 0, myHeader.Length)
                    If myHeader(0) = HeaderIdentifier And myHeader(1) = OtherModeHeader Then
                        btStream.Read(myDataSize, 0, 2)
                        DataLength = BitConverter.ToInt16(myDataSize, 0)

                        myBuffer = New Byte(DataLength - 1) {}
                        Recieved = 0
                        Do Until Recieved = DataLength
                            If btStream.DataAvailable = True Then
                                Recieved += btStream.Read(myBuffer, Recieved, DataLength - Recieved)
                            End If
                        Loop
                        ExecuteCommand(myHeader(2), myHeader(3), myBuffer)
                    Else
                        Debug.WriteLine("WrongData")
                        Exit While
                    End If
                Catch ex As Exception
                    Debug.WriteLine(ex.Message)
                    If Recieved = 0 Then
                        Exit While
                    End If
                End Try
            End If
        End While

If I send the image, without Thread.Sleeps (huighe ones). The recieving stream gets weird. The gets btStream.DataAvailable = False forever. If I remove that ckeck, it hangs at the btStream.Read.
I also tried to fix that with waiting for the answer of the server, but the problem doesn't get solved.

Do you have any idea how to fix that problem?. In that speed the image sending is worthless :(

SciLor