Android Question How do you use B4XSerializator to tranfer files between B4A and B4J using Bluetooth?

ToolboxZX

Member
Licensed User
Longtime User
I have heard that the B4XSerializator can be used to transfer files between B4A and B4J rather than using asyncstreams or the asyncstreamsobject.

I would like to do this using bluetooth between B4A and B4J as no other route will be available between the devices (no network path, internet, etc, only bluetooth).

Is there anyone that can post example code on how to do this, and how to use the Serializator? Thanks :)
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
You should always use AsyncStreams.

1. Initialize AsyncStreams in prefix mode.
2. Create a custom type, note that it should be declared in the main module:
B4X:
Type FileToSend(FileName As String, Data() As Byte)
3. Load the file:
B4X:
Dim fs As FileToSend
fs.Initialize
fs.FileName = "..."
fs.Data = Bit.InputStreamToBytes(File.OpenInput(...))
'convert object to bytes
Dim ser As B4XSerializator
Dim bytes() As Byte = ser.ConvertObjectToBytes(fs)
'send the bytes
astream.Write(bytes)

In the receiving side:
B4X:
Sub AStream_NewData(Buffer() As Byte)
Dim ser As B4XSerializator
Dim fs As FileToSend = ser.ConvertBytesToObject(Buffer)
Log(fs.FileName)
Dim out As OutputStream = File.OpenOutput(...)
out.WriteBytes(fs.Data, 0, fs.Data.Length)
out.Close
End Sub
 
Upvote 0
Top