Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android:Outputstream.write - issue

I am trying to transfer file to remote bluetooth device as given below

    while (availableBytes > 0 ) {

                 bytesRead = inStream.read(tempData,0,tempData.length);

                 mmOutStream.write(tempData,0,bytesRead);  
                 mmOutStream.flush();
                 availableBytes = inStream.available();
    }/* End of availableBytes */

While write is in progress for large file ( 500 KB), I am getting IO exception as "IOException: Connection timed out" after 12 to 15 mins. When i am sending small file which is of < 100 KB, i am successful in transferring. I am not closing socket while file transfer is in progress. Is there any limitation in Android that Bluetooth socket can be active only for some fixed time limit?

Please throw your views on this

like image 746
Vidz Avatar asked May 18 '26 00:05

Vidz


1 Answers

InputStream.available() does not do what you think it does:

Returns the number of bytes that can be read (or skipped over) from this input stream without blocking by the next caller of a method for this input stream.

The correct approach is to write until you hit bytesRead == -1, signalling EOF (End-of-File).

like image 68
ThomasRS Avatar answered May 19 '26 14:05

ThomasRS



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!