Android Tutorial FileTransfer - Send and receive files with AsyncStreams

Status
Not open for further replies.
Newer example based on B4XSerializator: https://www.b4x.com/android/forum/threads/72149
AsyncStream v1.50 (part of RandomAccessFile library) includes support for sending and receiving streams of any size.

Unlike the regular write methods that send an array of bytes there is a new WriteStream method that takes an InputStream and sends it to the other size. This method is only supported when AsyncStreams is initialized in prefix mode.

WriteStream can efficiently handle streams of any sizes. In the receiving side the NewStream event is raised after a complete stream is received.

Note that the data is checked for errors automatically using Adler-32 algorithm. The Error event is raised if the checksums do not match.

While a file is received you can check StreamTotal and StreamReceived properties to track the progress.

The received stream is saved in a temporary file. NewStream event includes the file folder and name. The folder can be changed by setting the StreamFolder field. The file name is an arbitrary string. It is recommended to do whatever needs to be done with the file and then delete it. The files will not be deleted automatically and will not be overwritten.

FileTransfer example demonstrates how AsyncStreams can be used to send and receive files of any size. This example allows you to connect two devices over Bluetooth or Wifi (assuming that both are connected to the same local network).

Once connected you can choose files to send.

SS-2013-06-24_13.24.55.png


The code includes an activity and service. All the communication is managed in the service. The activity is responsible for the UI and it also manages the process of finding unpaired Bluetooth devices.

As mentioned above the progress of received files is monitored with a timer that checks the StreamTotal and StreamReceived properties. In order to monitor the progress of the file being sent we use a CountingInputStream. This is a stream that wraps another stream and counts how many bytes were read.


C# implementation of FileTransfer: http://www.b4x.com/forum/basic4andr...ment-asyncstreams-prefix-mode.html#post178604

Edit: example removed as it was outdated.
 
Last edited:

ToolboxZX

Member
Licensed User
Longtime User
I know that it has been asked if a B4J version of this code is available, but would it be possible to get a code snippet of what would need to be loaded into the B4J example pointed to here https://www.b4x.com/android/forum/t...d-and-receive-objects-over-the-network.34612/ so that one could receive files from this B4A app instead of using the AsyncStreamsObject?

In attempting to use the AsyncStreamsObject B4J example as is (as a receiver), and connect, then attempting to transfer a file from this B4A application, I can connect, and it raises the astream_NewData event, but that is all.

I realize that as it is set up(the b4J asyncstreams example referred to in the other thread) that this is to be expected and it won't work. But what I am struggling with is what exactly should be the "receive" code (the events and methodology) to complement this send code. :(

For my intended application, I cannot use the C# Studio code solution, I have to use a B4J one.
 
Status
Not open for further replies.
Top