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.basic4ppc.com/forum/basi...ment-asyncstreams-prefix-mode.html#post178604

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

androidtom

Member
Licensed User
When using the async stream file transfer with the UI in the activity and a separate service module (FileTransfer.bas) handling the network read/writes, what's the "correct way" to notify the activity that a file has been received completely? I can set a flag in AStream_NewStream event and then monitor that flag with a timer in the activity but that seems more like a hack than a clean method. Is there a more acceptable approach to monitoring completion of a service?
 

androidtom

Member
Licensed User
You can use CallSub or CallSubDelayed to send a message from the service to the activity.
Thank you Erel! CallSubDelayed seems much better to me than monitoring a flag with a timer to detect completion by a service.
 

janderkan

Active Member
Licensed User
Hi
I have read this code and the .net filetransfer code.
If I stick to using the .write method, in .net I will receive 4 bytes with the length and then the data.
Is the length of data only limited to length set by the 4 bytes?

Jan
 

tdocs2

Well-Known Member
Licensed User
@Erel,

The File Transfer is indeed a wonderful application. I have modified it (to just use WiFi and display more user information and also incorporated the code into my app to transfer files between devices running my app.

I am very grateful to you for this app.

The BIG question is:

Is there a B4J version of this app? If there is, I plan to incorporate it into a B4J app I am planning.

Thank you.

Sandy
 

LucaMs

Expert
Licensed User
Last edited:

LucaMs

Expert
Licensed User
@Erel,

The File Transfer is indeed a wonderful application. I have modified it (to just use WiFi and display more user information and also incorporated the code into my app to transfer files between devices running my app.

I am very grateful to you for this app.

The BIG question is:

Is there a B4J version of this app? If there is, I plan to incorporate it into a B4J app I am planning.

Thank you.

Sandy


I could be wrong (EVEN because I'm not Erel :D) but since there is the b4j library JRandomAccessFile, make this version should not be very difficult.
But in this case, why Erel has made the PC side sw using C #? Perhaps because at the time jRandomAccessFile not yet exist.
 

AscySoft

Active Member
Licensed User
Hi.
I am trying to implement this thing in my app. I was able to connect from my windows pc to android phone/ copy file etc.
But when I try the other way around, from Android - File Transfer app to windows pc, when i hit the Connect WiFi button, connection fails.
First with the following message "(ErrnoException) libcore.io.ErrnoException: isConnected failed: ECONNREFUSED (Connection refused)";
Then after I deactivate my firewall and antivirus, with this message: "(SocketTimeoutException) java.net.SocketTimeoutException: failed to connect to /192.168.0.47 (port 21341) after 30000ms"
PS: my windows PC lan ip is 192.168.0.47
 

AscySoft

Active Member
Licensed User
Windows firewall blocks incoming connections. You must add a rule to open the port.

What are you running on the PC?

Are you running the C# implementation?

I deactivate all firewalls, both from windows (8.1) and from KIS. Optionally I did create a rule in windows firewall not to block incoming activity on port 21341. None of this helped. And yes, I am running your c# example app. With it, connection from PC to Android is possible, but as describe above, not vice-versa.
Could someone confirm that connection from android 2 pc is working as expected?
 

AscySoft

Active Member
Licensed User
I see. So this was the problem. I don't have Visual Studio on my PC so I was not able to tell this. Is there a link in this forum that might help in creating a server "missing" part from your c# example? This forum is too big! (or I am so lazy!)
 
Status
Not open for further replies.
Top