Android Question How to write data to a .dat file from "astreams new data"

mhewett737

Member
Licensed User
Longtime User
I have successfully read .dat files and sent them over Bluetooth serial to another device.

example data : 55 55 56 55 69 4a .................... 55 55 9a 6a 4d normally 17 to 23 bytes.

I am now using Astreams new data to receive this same type of data from the device. I wish to write it to a .dat file.


B4X:
Sub AStreams_NewData (Buffer() As Byte) 
 
Dim msg As String 
msg = (BConv.HexFromBytes(Buffer))
dirname = ("/engine "&selectedengine&"/testload.dat")
raf.initialize(File.DirRootExternal,dirname,False)
Log(msg)
raf.WriteObject(msg, True, raf.CurrentPosition)
raf.Close
End Sub


The log (msg) displays the correct hex data recieved as a string however the file "testload.dat" gets created but does not have the correct data. A lot of extraneous hex bytes.

I am a novice at this stuff what am I missing?
 

Kevin L. Johnson

Member
Licensed User
Longtime User
You should use WriteBytes instead of WriteObject. WriteObject uses a special format.

Ok, this sheds some light on an issue I was having while collecting data, as my binary file of Data was larger than the same data written as a text file, which had me scratching my head.

It begs the question of, when to use WriteBytes and when to use WriteObject as I was creating my own data types (structures) of Longs, Doubles, floats and strings, then populating them with collected data then writing them to file using WriteObject.

Later I want to actually do the same thing with bitmaps, images or pictures and data for transmission across the network, then what do I use because I would Like to have the option of persisting them in files as well.

Thanks
Kevin
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
WriteBytes is a low level method. You pass an array of bytes and they are directly written to a stream.

WriteObject is much more sophisticated. It takes an object and serializes the object to the stream. Later you can just call ReadObject and get the object back. Note that images are not serializable and therefore cannot be used with this method.
 
Upvote 0

Kevin L. Johnson

Member
Licensed User
Longtime User

Ok, so how would one write a custom data-type to file. One that is create via the 'type' keyword, that is based upon a hierarchy of other custom data types that are in-turn made up of standard data-types (float, int, string, etc.).

AND ... Ultimately my biggest question is ... Even though it worked, why would my binary file be larger than my text file of the same data using 'WriteObject' to write my custom data-type (structure) to file (which incidently, contains no images)?
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…