Android Tutorial AsyncStreamsObject - Send and receive objects instead of bytes

Erel

Administrator
Staff member
Licensed User
AsyncStreamsObject is a small framework built over AsyncStreams.

Instead of sending and receiving bytes, AsyncStreamsObject allows you to send and receive objects. This makes it much simpler to communicate with other devices.

AsyncStreamsObject uses the WriteStream method of AsyncStreams. This method was introduced in RandomAccessFile v1.5.

You can use AsyncStreamsObject when you implement both sides of the connection.



Supported objects

WriteObject supports: Lists, Arrays, Maps, Strings, primitive types and user defined types.

WriteBitmap allows you to send bitmaps.

WriteFile allows you to send any file.

All the three write methods expect a key (string) and a value (the actual object).

The NewObject event is raised when an object is received. You should use the key to decide how to handle the object. This code is taken from the attached example:
B4X:
Sub astreamO_NewObject(Key As String, Value As Object)
   Select Key
      Case "form"
         Dim m As Map = Value 'Value is a Map
         txtFirst.Text = m.Get("first")
         txtLast.Text = m.Get("last")
         txtAnimal.Text = m.Get("animal")
      Case "simple value"
         Dim number As Int = Value
         ToastMessageShow("Received lucky number: " & number, False)
      Case "image"
         Dim bmp As Bitmap = Value
         Dim r As Rect
         r.Initialize(0, 0, Panel1.Width, Panel1.Height)
         cvs.DrawBitmap(bmp, Null, r)
         Panel1.Invalidate
      Case "file"
         Dim fileName As String = value
         Log("Received file. File size: " & File.Size(astreamO.TempFolder, fileName))
   End Select
End Sub
The ObjectSent event is raised after a value was successfully sent.
B4X:
Sub astreamO_ObjectSent (Key As String)
   Log("Object sent: " & Key)
End Sub
Initializing AsyncStreamsObject

Two steps are required. First you should call AsyncStreamsObject.Initialize and pass the target module and event name:
B4X:
astreamO.Initialize(Me, "astreamO")
Once there is a connection you should call AsyncStreamsObject.Start:
B4X:
Sub StartAstream(s As Socket)
   astreamO.Start(s.InputStream, s.OutputStream)
   SetUIState
End Sub
Note that you can use any type of connection that provides an input stream and output stream.

The Terminated event is raised when the connection is closed.

The attached example connects two devices over the local network. You need to set the IP address of one of the devices and press on the Connect button. Once connect you can send data between the devices.

AsyncStreamsObject class is included in the attached example.
 

Attachments

Last edited:

tkristensen

Member
Licensed User
I'd like to use the AsyncStreamsObject to receive and send files to our PC based host. Has anyone implemented the PC side of AsyncStreamsObject for use with .net?
 

GMan

Well-Known Member
Licensed User
Hoi Erel,

i always get an error:

java.net.SocketException: Bad File number

Doesnt matter, if i a target ip is given or not.
 

Erel

Administrator
Staff member
Licensed User
The connection is not really related to AsyncStreamsObject class. Please start a new thread for this error and also post the full error message.
 

Erel

Administrator
Staff member
Licensed User
I prefer that this thread will be used for discussions about AsyncStreamsObject functionality. AsyncStreamsObject is one level above the actual connection. You should just need to start a new thread.
 

Dominex

Active Member
Licensed User
Thanks Erel,

I have a question, a link between two devices in Wi-Fi works well, but if one of the two devices is under network data does not work, is this normal?
 

Erel

Administrator
Staff member
Licensed User
This is not really related to AsyncStreamsObject.

You cannot directly connect two devices over the internet. Direct connection will only work on a local network.
 

Erel

Administrator
Staff member
Licensed User
AsyncStreamsObject was updated. Fixes two issues related to the temporary files created during the communication.
 

Erel

Administrator
Staff member
Licensed User
You can send and receive files at the same time. However you can only send one file at a time.
 

Nenad Trickovic

Member
Licensed User
How could we achieve functionality to send more files at the same time and to receive more files at the same time?

Can we create the class that wraps the code as in example and later instantiate it per each connection?
 

Erel

Administrator
Staff member
Licensed User
I'm not sure that I understand. AsyncStreams allows you to send multiple files. However they will be sent one after another. Eventually there is a single channel between the two sockets.
 

Rusty

Well-Known Member
Licensed User
I'm trying to use the AsyncStreams with prefix and have a requirement to use port 443. I seem to remember there is a limit on the port numbers below (about) 2000. Can you advise me on how I can use port 443?
Thanks,
Rusty
 

Erel

Administrator
Staff member
Licensed User
Your question is not related to AsyncStreams or AsyncStreamsObject. You should start a new thread for it.

AsyncStreams is not aware of the connection. The port number (and the connection type) doesn't matter.
 

Douglas Farias

Expert
Licensed User
@Erel i made this
Sub btnSendFile_Click
CC.Show("*/*", "Choose file to send")
End Sub



Sub CC_Result (Success As Boolean, Dir As String, FileName As String)
If Success Then
File.Copy(Dir,FileName, fp, "temporario.dat" )
astreamO.WriteFile("file", fp, "temporario.dat")
End If
End Sub
i think the code its correct, but dont send to the pc, on the pc only a file is generated ip.txt with my ip but not the file sent
and no logs with file sended sucefull

edit: solved i m noob , dont see temp folder ¬¬
 
Last edited:
Top