Android Question Bytes to fixed packet

Philip Prins

Active Member
Licensed User
Longtime 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

Expert
Licensed User
Yes i can see the packets coming in but they are all the same size
So we are either missing the final (smaller) packet, or it is being sent as a 30000 byte packet too, ie, for the call:
B4X:
ptChannel.SendPublicCustomMessage(SendBuffer, BytesToSend)    'trust that this sends only the specified number of bytes
perhaps SendPublicCustomMessage is sending the full SendBuffer(30000) array, and not just the BytesToSend that we asked it to send.

Can't trust nobody nowadays, not even software :-/
 
Upvote 0

Philip Prins

Active Member
Licensed User
Longtime User
Is possible

I try sending the small picture directly
B4X:
ptChannel.SendPublicCustomMessage(data, data.Length)

This is send and received in one packet

How ever this doesnt display a picture
 
Upvote 0

Philip Prins

Active Member
Licensed User
Longtime User
Try sending just a small packet of easily-identifiable data, eg
B4X:
Dim PacketToSend() As Byte = Array As Byte(65, 66, 67, 49, 50, 51)
which will be six bytes of ASCII "ABC123"

Yes receive 6 bytes

I don't know why my picture is not displayed on the other end:
B4X:
Dim filename As String = DateTime.Now&".jpg"
    
    
    Dim dir As String = File.DirRootExternal
    
    camEx.SavePictureToFile(Data, dir, filename)
    ''camEx.StartPreview 'restart preview
    
    'send a broadcast intent to the media scanner to force it to scan the saved file.
    Dim Phone As Phone
    Dim i As Intent
    i.Initialize("android.intent.action.MEDIA_SCANNER_SCAN_FILE", _
        "file://" & File.Combine(dir, filename))
    Phone.SendBroadcastIntent(i)
    ToastMessageShow("Picture saved." & CRLF  & "File size: " & File.Size(dir, filename), True)
    
    camEx.Release
    
    CallSubDelayed2(Starter,"SendPhoto",Data)
 
Upvote 0

emexes

Expert
Licensed User
Yes receive 6 bytes
Righto, send that exact same packet via the SendPhoto(Data() As Byte) Sub eg:
B4X:
Dim PacketToSend() As Byte = Array As Byte(65, 66, 67, 49, 50, 51)
SendPhoto(PacketToSend)
and we should get a single chunk Log line of:
and you should receive one packet at the other end, and it should be 6 bytes long and read "ABC123" (if you can see the data on the radio)
 
Upvote 0

Philip Prins

Active Member
Licensed User
Longtime User
Righto, send that exact same packet via the SendPhoto(Data() As Byte) Sub eg:
B4X:
Dim PacketToSend() As Byte = Array As Byte(65, 66, 67, 49, 50, 51)
SendPhoto(PacketToSend)
and we should get a single chunk Log line of:

and you should receive one packet at the other end, and it should be 6 bytes long and read "ABC123" (if you can see the data on the radio)
Yes log line is 6 0 6 but received packet length = 30000

when i send the small image directly i get a smaller packet size equal to the one sent
 
Upvote 0

emexes

Expert
Licensed User
Yes log line is 6 0 6 but received packet length = 30000
when i send the small image directly i get a smaller packet size equal to the one sent

That makes it sound like the SendPublicCustomMessage function is sending the full SendBuffer(30000) array, and not just the BytesToSend that we asked it to send.

You might be able to fix that by adding three lines to resize the SendBuffer Array to match BytesToSend, before the ArrayCopy line:
B4X:
If SendBuffer.Length <> BytesToSend Then
    Dim SendBuffer(BytesToSend)
End If

bc.ArrayCopy(data, ChunkStart, SendBuffer, 0, BytesToSend)
I am not sure whether Arrays can be Dimmed more than once, but we'll never know if we don't give it a go :)
 
Upvote 0

Philip Prins

Active Member
Licensed User
Longtime User
That makes it sound like the SendPublicCustomMessage function is sending the full SendBuffer(30000) array, and not just the BytesToSend that we asked it to send.

You might be able to fix that by adding three lines to resize the SendBuffer Array to match BytesToSend, before the ArrayCopy line:
B4X:
If SendBuffer.Length <> BytesToSend Then
    Dim SendBuffer(BytesToSend)
End If

bc.ArrayCopy(data, ChunkStart, SendBuffer, 0, BytesToSend)
I am not sure whether Arrays can be Dimmed more than once, but we'll never know if we don't give it a go :)
Yes

This works, now i have to figure out why my jpeg is not decoded
 
Upvote 0

emexes

Expert
Licensed User
Works now... Phew! i am very happy to hear that :)

Display JPEG... maybe it needs a MIME header or similar, so that it knows how to interpret the remainder of the packet.

If you can keep the data ubder 30000 bytes ie not split it up, that will avoid a host of problems.
 
Last edited:
Upvote 0

Philip Prins

Active Member
Licensed User
Longtime User
Works noe... Phew! i am very happy to hear that :)

Display JPEG... maybe it needs a MIME header or similar, so that it knows how to interpret the remainder of the packet.

If you can keep the data ubder 30000 bytes ie not split it up, that will avoid a host of problems.

Yes i will ask the developer , not much documentation

Thanks for all your help
 
Upvote 0

emexes

Expert
Licensed User
not much documentation
lol... that never happens to me ;-/

Can you send an image from some other device? To either your test device, or even back to the Android device that you're currently sending stuff from here? If you can somehow intercept the image packet, you might see something identifiable at the beginning that gives you a clue as to the header required. Most file formats have a readable string constant, aka magic number, near the start of the file to identify/confirm the file type.
 
Upvote 0

Philip Prins

Active Member
Licensed User
Longtime User
This is what i get as picture:
B4X:
               ��  JFIF     H H  � C                       

                        !         "$" $    � C                                                                 �    , ,  "       �                               

� �               }        !1A  Qa "q 2��#B荏 R剞$3br�
     %&'()*456789:CDEFGHIJSTUVWXYZcdefghijstuvwxyz����������ウЖ�桶患況弦座団兎波品厶壞嶐慵旻矣粤肄蓍襃鴦�����                               

� �               w       !1  AQ aq "2� B�荏    #3R�br� $4��   &'()*56789:CDEFGHIJSTUVWXYZcdefghijstuvwxyz���������■ぅΗ┤�慨偽係杭唾津毒班片嘖孛忤掣矣粤肄蓍褞齡����          ? ����������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������
 
Upvote 0
Top