Android Question Sub AStream_NewData (Buffer() As Byte) Problem

floatingpoint

Member
Licensed User
Longtime User
B4X:
Sub AStream_NewData (Buffer() As Byte)
    Dim localmsg As String
    'If Buffer(0) = 1 AND Buffer(1) = 1 Then
        localmsg = BytesToString(Buffer, 0, Buffer.Length, "UTF8")    ' 3rd byte is start of data
        Log(localmsg)
        msg = localmsg.SubString(2)            ' Process Global - First 2 bytes are command bytes
        If IsPaused(Page2) = False Then
            CallSub(Page2,"LoadLabelText")    ' Only update label text if Page2 is active
        End If   
    'End If
End Sub

This code is running in a service
I am sending 54 byte packets over USB Accessory.
However the CallSub(Page2,"LoadLabelText") only appears to execute when the log has filled up:
Message longer than Log limit (4000). Message was truncated.

Naturally I'd like the Page2 label text to update every packet.

What am I doing wrong?

Thanks in advance

FP
 

floatingpoint

Member
Licensed User
Longtime User
This is a warning message. It means that the message you are logging is larger than the log limit. The string itself was not truncated. Only the text in the logs.

Thank you Erel.

I did not explain the issue very well.

I am receiving USB packets of 54 bytes say 1 every second.
However it is only when the Android device has received over a few thousand bytes does the AStream_NewData event fire and then I get an update of my labels and the buffer overflow message.

So this is only every few minutes.

I want the AStream_NewData event to fire every time a packet is received.

Is there a terminating character that I have to add to each packet at the source?

EDIT: When all else fails - read the instructions!
Prefix mode

When the object is initialized in prefix mode the data is expected to adhere to the following protocol: every message (bytes array) should be prefixed with the bytes array length (as an Int). So if another device sends us a message made of 100 bytes. The stream is expected to include 4 bytes with a value of 100 and then the 100 bytes.
The NewData event will be raised with the 100 bytes. It will not include the 4 prefix bytes.

I'll try that!

FP
 
Last edited:
Upvote 0

floatingpoint

Member
Licensed User
Longtime User
Well,

Unfortunately, Prefix mode doesn't stop the messages queuing.

I am prefixing messages with little endian 4 byte value (first byte only = 60) and I can see the 64 byte USB messages coming in at regular intervals but the :

B4X:
Sub AStream_NewData (Buffer() As Byte)
    Dim localmsg As String
    'If Buffer(0) = 1 AND Buffer(1) = 1 Then
        localmsg = BytesToString(Buffer, 0, Buffer.Length, "UTF8")    ' 3rd byte is start of data
        Log(localmsg)
        msg = localmsg.SubString(2)            ' Process Global - First 2 bytes are command bytes
        If IsPaused(Page2) = False Then
            CallSub(Page2,"LoadLabelText")    ' Only update label text if Page2 is active
        End If
    'End If
End Sub

Sub is only firing after about 2 minutes when 129 messages (60 bytes each) have queued.
Then all messages are dumped and the last message only is written to my labels.

I've tried moving AStream_NewData back into the current activity.
Tried stripping all other code out of activity but no change.

My messages are 64 bytes including 4 prefix bytes, so I use a prefix value of 60.
Running out of ideas.

Outgoing packets when buttons are pressed are immediate so there is no delay with the outgoing USB traffic.

Is there a way to stop messages queuing at AStream_NewData?

FP
 
Upvote 0

floatingpoint

Member
Licensed User
Longtime User
AsyncStreams doesn't queue the messages. Once there is a complete message it raises the event.

What is the length of Buffer when the event is raised?

There are 128 x 64 byte messages when the event is raised.
These messages have then had the 4 byte prefix stripped AND then had a carriage return character added to the already existing new line end of line character when printed to the log.

End of Log......

VIEW INFO PRESS OK COOL HEAT and COOL 18.7
Message Number:126
THIS COOLER MSTR 18.7 DEG HEAT and COOL 18.7
Message Number:127
VIEW INFO PRESS OK G HEAT and COOL 18.7
Message Number:128

FP
 
Last edited:
Upvote 0

floatingpoint

Member
Licensed User
Longtime User
There are 128 x 64 byte messages when the event is raised.
These messages have then had the 4 byte prefix stripped AND then had a carriage return character added to the already existing new line end of line character when printed to the log.

End of Log......

VIEW INFO PRESS OK COOL HEAT and COOL 18.7
Message Number:126
THIS COOLER MSTR 18.7 DEG HEAT and COOL 18.7
Message Number:127
VIEW INFO PRESS OK G HEAT and COOL 18.7
Message Number:128

FP


OK, I use a new post because I have fixed the issue!

There is a note on here: http://developer.android.com/guide/topics/connectivity/usb/accessory.html#permission-a

Note: At a lower level, the packets are 64 bytes for USB full-speed accessories and 512 bytes for USB high-speed accessories. The Android accessory protocol bundles the packets together for both speeds into one logical packet for simplicity.

I use Full speed USB comms.

So I reduce my packet size to 62 bytes (I only need 58) and it works perfectly!

So the trap appears to be that the application packet size of 64 bytes fools the Astream_NewData to not firing but to wait for 8k of data!
If you use USB Accessory Full Speed then don't use packet sizes of 64 bytes.


FP
 
Upvote 0
Top