Android Tutorial AsyncStreamsObject - Send and receive objects instead of bytes

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.

SS-2013-06-26_10.32.47.png


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

  • AsyncStreamsObject.zip
    10.5 KB · Views: 4,124
Last edited:

tkristensen

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

GMan

Well-Known Member
Licensed User
Longtime User
The connection is not really related to AsyncStreamsObject class.

???
I am using the sample code below - isn't that using the AsyncStreamsObject class ?
 

GMan

Well-Known Member
Licensed User
Longtime User
:sign0013: - problem is also solved
 

Nenad Trickovic

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

Rusty

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

Douglas Farias

Expert
Licensed User
Longtime 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