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:

desof

Well-Known Member
Licensed User
Longtime User
ERROR:

B4X:
Parsing code.                           0.01
Compiling code.                         Error
Error compiling program.
Error description: Unknown member: writestream
Occurred on line: 89
astream.WriteStream(countingStream, totalSizeForSending)
Word: writestream
 

bluedude

Well-Known Member
Licensed User
Longtime User
Not using WIFIDirect?

I checked this demo but to my surprise it does not use WIFIDirect?

Are you going to add it so we know how to do file transfers with WIFI Direct?

Cheers,
 

Mahares

Expert
Licensed User
Longtime User
@desof:
Parsing code. 0.01
Compiling code. Error
Error compiling program.
Error description: Unknown member: writestream
Occurred on line: 89
astream.WriteStream(countingStream, totalSizeForSending)
Word: writestream

You need to unzip the library that Erel referred you to in the primary library folder, NOT the additional library folder. The primary lib folder is usually here:
C:\program Files\Anywhere software\basic4Android\Libraries
 

GMan

Well-Known Member
Licensed User
Longtime User
@desof:


You need to unzip the library that Erel referred you to in the primary library folder, NOT the additional library folder. The primary lib folder is usually here:
C:\program Files\Anywhere software\basic4Android\Libraries

I got the same error - updated the both lib-files, restartet the IDE: still shows version 1.32.

Also the name of the Class Appears as FileTranser, not FileTransFer ;)
 
Last edited:

Mahares

Expert
Licensed User
Longtime User
I was having the same error until I deleted them from the additional lib folder and put them in the PRIMARY library folder. Make sure there are no copies of the RandomAccessFile.jar and RandomAccessFile.xml files in the additional lib folder. Everything is working for me now and I show RandomAccessFile version 1.50
 

GMan

Well-Known Member
Licensed User
Longtime User
I was having the same error until I deleted them from the additional lib folder and put them in the PRIMARY library folder. Make sure there are no copies of the RandomAccessFile.jar and RandomAccessFile.xml files in the additional lib folder. Everything is working for me now and I show RandomAccessFile version 1.50

Thx, but they where (only) in the original LIB-Folder

EDIT: after Restarting my PC it works :)
 
Last edited:

Erel

B4X founder
Staff member
Licensed User
Longtime User
As Mahares wrote you should copy them to the internal libraries folder. The internal libraries folder has a higher priority. I will add instructions in the library thread.

@bluedude, this solution will work with any connection supported by AsyncStreams. I preferred to use standard Wifi and Bluetooth as these are supported on all devices.

The nice thing about AsyncStreams is that it allows you to use the same code for all kinds of connections, as done in this example (BT and Wifi).
 

ferdztech

Member
Licensed User
Longtime User
@Erel Sir this filetransfer apps will help me a lot in my project. My Only problem is im currently using vb6 as my IDE which is very similar in b4a . Is it possible to convert the CS FileTransfer-Desktop sample to visual basic 6.0. I'm getting hard to understand and translating the code in vb6. I'm not familiar in CS.

Thank you very much.
 

ComposerB4A

Member
Licensed User
Longtime User
Excellent library,
one question, if i have two phone with wifi (without router and/or hub external) ¿is possible conect this two phones?
 
Status
Not open for further replies.
Top