Android Tutorial FileTransfer - Send and receive files with AsyncStreams

Status
Not open for further replies.

Erel

Administrator
Staff member
Licensed User
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.



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.basic4ppc.com/forum/basic4android-getting-started-tutorials/30741-net-filetransfer-implement-asyncstreams-prefix-mode.html#post178604
 

Attachments

Last edited:

desof

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

Erel

Administrator
Staff member
Licensed User
First sentence:
AsyncStream v1.50 (part of RandomAccessFile library) includes support for sending and receiving streams of any size.
Have you downloaded this version?
 

Erel

Administrator
Staff member
Licensed User
You should download the new version of RandomAccessFile. There is a link to it at the bottom of the first post.
 

bluedude

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

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

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

Administrator
Staff member
Licensed 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
@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.
 

Erel

Administrator
Staff member
Licensed User
I'm sorry but the code is quite complicated. I do not know whether VB6 supports it or not.
 

ComposerB4A

Member
Licensed 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