Android Tutorial FileTransfer - Send and receive files with AsyncStreams

Discussion in 'Tutorials & Examples' started by Erel, Jun 24, 2013.

Thread Status:
Not open for further replies.
  1. Erel

    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.

    [​IMG]

    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
     

    Attached Files:

    Last edited: Dec 30, 2016
  2. JorgeMC

    JorgeMC Member Licensed User

    Thanks !!!!!:sign0087:
     
  3. Rusty

    Rusty Well-Known Member Licensed User

    Erel,
    Just what I need :)
    Thanks,
    Rusty
     
  4. bluedude

    bluedude Well-Known Member Licensed User

    Awesome

    So I guess you immediately went to work after the WIFIDirect questions, awesome!
     
    iandbrebet likes this.
  5. desof

    desof Well-Known Member Licensed User

    ERROR:

    Code:
    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
     
  6. Erel

    Erel Administrator Staff Member Licensed User

    First sentence:
    Have you downloaded this version?
     
  7. desof

    desof Well-Known Member Licensed User

    Sorry I do not understand .. Do I have to include the library?
     
  8. Erel

    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.
     
  9. bluedude

    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,
     
  10. Mahares

    Mahares 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
     
  11. GMan

    GMan Well-Known Member Licensed User

    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: Jun 24, 2013
  12. Mahares

    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
     
  13. GMan

    GMan Well-Known Member Licensed User

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

    EDIT: after Restarting my PC it works :)
     
    Last edited: Jun 24, 2013
  14. Erel

    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).
     
    spairo likes this.
  15. bluedude

    bluedude Well-Known Member Licensed User

    Ok, thanks.
     
  16. ferdztech

    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.
     
  17. Erel

    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.
     
  18. Erel

    Erel Administrator Staff Member Licensed User

    WriteStream uses a custom protocol (described here: .Net FileTransfer - Implement AsyncStreams Prefix mode )
    You will not be able to use it with VB6 unless you will implement the protocol yourself. If the files that you are sending are not too large then you can just send the raw bytes directly.
     
  19. ComposerB4A

    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?
     
  20. Erel

    Erel Administrator Staff Member Licensed User

    Please start a new thread for this question.
     
Thread Status:
Not open for further replies.
Loading...