Android Question Bytes to fixed packet

Philip Prins

Active Member
Licensed User
Hello ,

I need to send a jpeg in packets of maximum 30 bytes to a server.
The problem is that the jpeg are different sizes.
How do i divide it?
I tried this , but how to handle oneven total data size?

B4X:
Public Sub SendPhoto(data() As Byte)
    Dim nbytes As Int=data.Length
    Dim bc As ByteConverter
    Dim ninp As Int
    Dim bufjpg(30) As Byte
    For ninp =0 To nbytes-1 Step 30
    bc.ArrayCopy(data,ninp,bufjpg,0,30)
    ptChannel.SendPublicCustomMessage(bufjpg,30)
    Next
End Sub
 

emexes

Well-Known Member
Licensed User
How do i divide it?
I tried this , but how to handle oneven total data size?
Try this:
B4X:
Public Sub SendPhoto(Data() As Byte)

    Dim bc as ByteConverter    'if not already done elsewhere

    Dim ChunkSize As Int = 30    'or whatever, eg 20 for Bluetooth Low Energy
    Dim SendBuffer(ChunkSize) As Byte
    Dim BytesToSend As Int

    For ChunkStart = 0 to Data.Length - 1 Step ChunkSize
        BytesToSend = Data.Length - ChunkStart
        If BytesToSend > ChunkSize Then
            BytesToSend = ChunkSize
        End If

        bc.ArrayCopy(Data, ChunkStart, SendBuffer, 0, BytesToSend)

        ptChannel.SendPublicCustomMessage(SendBuffer, BytesToSend)    'trust that this sends only the specified number of bytes
    Next
)
 
Last edited:

Philip Prins

Active Member
Licensed User
Unfortunatly it does not work, it sends more bytes then the total

Data length =624616 Bytes send =
B4X:
Dim bc As ByteConverter    'if not already done elsewhere
    Dim BytesToSend  As Int
    Dim TotalBytesSend As Int
    Dim ChunkSize As Int = 30000    'or whatever, eg 20 for Bluetooth Low Energy
    Dim SendBuffer(ChunkSize) As Byte

    For ChunkStart = 0 To data.Length - 1 Step ChunkSize
        BytesToSend = data.Length - ChunkStart
        If BytesToSend > ChunkSize Then
            BytesToSend = ChunkSize
        End If

        bc.ArrayCopy(data, ChunkStart, SendBuffer, 0, BytesToSend)

        ptChannel.SendPublicCustomMessage(SendBuffer, BytesToSend)    'trust that this sends only the specified number of bytes
        TotalBytesSend = TotalBytesSend + SendBuffer.Length
        
    Next
    
    Log(data.Length &" "& TotalBytesSend)
630000
 

emexes

Well-Known Member
Licensed User
But those numbers don't make sense. And the roundedness of the 630000 is a highly-suspect coincidence.

Otherwise I would suggest that perhaps the bytes are being encode to UTF-8, or their is some packet overhead being included in the send count.
 

Philip Prins

Active Member
Licensed User
True ,

If i receive a picture i will get a number of packets with a fixed size and the last one the remaining bytes.

Like this

32779

32779

32779

32779
Get new reading

32779

32779

32779

32779

32779

32779

32779

32779

32779

32779

24931
 

emexes

Well-Known Member
Licensed User
Ah, hang on, just noticed your extra lines added to count the sent bytes. Change it to:
B4X:
ptChannel.SendPublicCustomMessage(SendBuffer, BytesToSend)    'trust that this sends only the specified number of bytes
''''TotalBytesSend = TotalBytesSend + SendBuffer.Length    'close, but no cigar
TotalBytesSend = TotalBytesSend + BytesToSend
and give it another burl :)
 

Philip Prins

Active Member
Licensed User
Ok i get this 608067 608067

But what i receive is this :


608067 608067
30000
30000
30000
30000
30000
30000
30000
30000
30000
30000
30000
30000
30000
30000
30000
30000
30000
30000
30000
30000
30000

A total of 630000 bytes instead of 608067

The last packet should be less than 30000
 

emexes

Well-Known Member
Licensed User
Also, what's with the change in ChunkSize from 30
I need to send a jpeg in packets of maximum 30 bytes to a server.
to 30000
B4X:
Dim ChunkSize As Int = 30000    'or whatever, eg 20 for Bluetooth Low Energy
What was the reason for the original 30 byte limit? Are we now bursting that somehow? And what the heck are those extra 2779 bytes coming from? (edit: rhetorical question, not really expecting an answer) If the count wasn't constant, I'd be back to looking for a hidden UTF-8 conversion.
 
Last edited:

emexes

Well-Known Member
Licensed User
Could you please add a Log line:
B4X:
bc.ArrayCopy(data, ChunkStart, SendBuffer, 0, BytesToSend)
Log(Data.Length & " " & ChunkStart & " " & BytesToSend)
and post me the results?
 

Philip Prins

Active Member
Licensed User
453761 0 30000
453761 30000 30000
453761 60000 30000
453761 90000 30000
453761 120000 30000
453761 150000 30000
453761 180000 30000
453761 210000 30000
453761 240000 30000
453761 270000 30000
453761 300000 30000
453761 330000 30000
453761 360000 30000
453761 390000 30000
453761 420000 30000
453761 450000 3761
453761 453761
 

emexes

Well-Known Member
Licensed User
453761 0 30000
453761 30000 30000
453761 60000 30000
453761 90000 30000
453761 120000 30000
453761 150000 30000
453761 180000 30000
453761 210000 30000
453761 240000 30000
453761 270000 30000
453761 300000 30000
453761 330000 30000
453761 360000 30000
453761 390000 30000
453761 420000 30000
453761 450000 3761
453761 453761
Don't know about you, but... I'm pretty happy with those numbers :)
 

Philip Prins

Active Member
Licensed User
Also, what's with the change in ChunkSize from 30

to 30000
B4X:
Dim ChunkSize As Int = 30000    'or whatever, eg 20 for Bluetooth Low Energy
What was the reason for the original 30 byte limit? Are we now bursting that somehow? And what the heck are those extra 2779 bytes coming from? (edit: rhetorical question, not really expecting an answer) If the count wasn't constant, I'd be back to looking for a hidden UTF-8 conversion.

sendPublicCustomMessage
int sendPublicCustomMessage(byte[] DataBuffer, int DataLength)
DataBuffer Buffer of data
DataLength Data size. Max size is 30KB.
Return Request Sequence ID. If less then 0, ErrorCode.
 

emexes

Well-Known Member
Licensed User
Can you shrink the JPEG down, eg, load MyFace.jpeg, scale it down to eg 300x300 max, save as MyFace300.jpeg?
 

emexes

Well-Known Member
Licensed User
Yes but when i receive a picture the last packet is smaller.
Correct. Is that not what we want? The original question was: how to handle uneven total data size?
When i send a picture all the packets are 30000.
Except the last one, which is whatever is left to send, and usually less than 30000 (99.997% of the time ;-)

Can you see the packets being received at the other end? Does it show a number of 30000 byte packets coming in, followed by the final packet usually < 30000 ?
 

emexes

Well-Known Member
Licensed User
Perhaps you could log the return code per
Return Request Sequence ID. If less then 0, ErrorCode.
eg
B4X:
Dim ReturnCode As Int = ptChannel.SendPublicCustomMessage(SendBuffer, BytesToSend)    'trust that this sends only the specified number of bytes
Log(ReturnCode)
just to make sure that we're not missing an ErrorCode about overloading a packet queue or sending packets too quickly or something like that.
 

Philip Prins

Active Member
Licensed User
Yes i can see the packets coming in but they are all the same size

The source is the camera , tried to set it but still a large file 634190 634190

camEx.SetJpegQuality(45)
camEx.SetPictureSize(300,300)
camEx.CommitParameters
camEx.StartPreview
 

Philip Prins

Active Member
Licensed User
Perhaps you could log the return code per

eg
B4X:
Dim ReturnCode As Int = ptChannel.SendPublicCustomMessage(SendBuffer, BytesToSend)    'trust that this sends only the specified number of bytes
Log(ReturnCode)
just to make sure that we're not missing an ErrorCode about overloading a packet queue or sending packets too quickly or something like that.
I get return code 44
 
Top