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:

Kwame Twum

Active Member
Licensed User
Longtime User
I keep getting an error when I try to connect to another device through BT... Please help
 

Attachments

  • screenshot_2013-11-15_2232.png
    screenshot_2013-11-15_2232.png
    55 KB · Views: 877

Kwame Twum

Active Member
Licensed User
Longtime User
This error usually means that the other device isn't listening for connections.
The other device was in listening mode though... even tried my computer's Bluetooth but didn't work (but I guess that was because of the bridge connection)
 

grifoDev

Member
Licensed User
Longtime User
Hello, Erel
I connect to wifi my phone,
installed filetransfert and Ok,
on the pc run shell dos IPCONFIG and find the ip of PC for connection
but on phone "Wifi Status = Disconnected" :((

thanks
grifoDev
 

grifoDev

Member
Licensed User
Longtime User
oops sorry ..
can you sugest me what I can use for connect PC -> device and Device -> PC ?
thanks
grifoDev
 

grifoDev

Member
Licensed User
Longtime User
Hello tuhatinvn
No, I use Delphi , with vcl INDY I can connect to TCPIP or UDP...
thanks
 

Robert Valentino

Well-Known Member
Licensed User
Longtime User
I tried this an keep getting Service Discovery Failed.

BUT if I go into "my files" select a file and then select share and select Bluetooth it sends the file to my PC with no problem.

How can I get this fail transfer program to work?

Using it with a Samsung Galaxy Tab-3 7" and HP Pavilion DM1 (running Windows 8.1)

Thanks

BobVal
 

pgargom

Member
Licensed User
Longtime User
Hi Erel,
i have a doubt about use of Bluetooth.
i'm using the example of file Transfer but implement only 1 side of the conection.
I use "serial1.Connect3(Address,11)" to connect with my device and i get the conection but when i try to send a photo, my device dont receive nothing.
AsynscStreams are initialited like "astream.Initialize(Null,out, "astream")" and i initialited it in serial_conected like in the example

Note: the file in "Dir,Filename" exists

my sendFile method is the next

B4X:
Dim out As OutputStream
      out.InitializeToBytesArray(100)
      File.Copy2(File.OpenInput(Dir, FileName), out)
    astream.Write(out.ToBytesArray) 'write the file name
    lblFile = "Sending: " & currentFile
    SendingFile = True
    UpdateProgress

Thanks
 

Mbc

Member
Licensed User
Longtime User
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).
Hi Erel,
The FileTransfer app gives consistent and identical errors on both my Android phone and my Android tablet.
On trying to connect to an existing WiFi device it produces the error:
Error: (ErmoException) libcore.io ErnoException: isConnecte failed: EHOSTUNREACH (No route to....
On trying to connect to a paired BT device it produces the error:
Error: java.io.IOException: read failed, socket might be closed timeout, read ret: -1

Any idea?
Thanks.
 
Last edited by a moderator:

Mbc

Member
Licensed User
Longtime User
Are the two devices connected to the same local network?

Yes they are.
Some more info:
Communicating using WiFi or BT with other devices on this network but using different apps, runs flawlessly.
Another BT app from Basic4Android (Package=anywheresoftware.b4a.samples.bluetooth) also gives problems. It crashes after a string is sent. After the code:
Sub btnSend_Click
AStream.Write(txtInput.Text.GetBytes("UTF8"))
txtInput.SelectAll
txtInput.RequestFocus
LogMessage("Me", txtInput.Text)
End Sub
 

lemonisdead

Well-Known Member
Licensed User
Longtime User
Erel,
Are you refering to a log FileTransfer produces? Where can I find that?
You will find the log in the right part of your B4A's IDE (look at the tabs). When you are in Debug mode and connected with B4A Bridge, you will see the error messages.
 

GMan

Well-Known Member
Licensed User
Longtime User
you will see the error messages.
But NOT on x64-Systems :(
For my behavior this is a VERY BIG negative point when developing :(:(:(
 

Mbc

Member
Licensed User
Longtime User
You will find the log in the right part of your B4A's IDE (look at the tabs). When you are in Debug mode and connected with B4A Bridge, you will see the error messages.

Yes of course. Thanks for pointing that out.
Log does not seem to give any error but I found out some interesting behaviour in the meantime:
Errors with FileTransfer do not occur when connecting FileTransfer with WiFi or with Bluetooth to Android phone (also used as device to test Basic4Android, might have someting to do with it). Trying to connect with Windows PC or Arduino BT device does produce the error.
 
Status
Not open for further replies.
Top