9

I am facing a problem in sending large files over bluetooth sockets. Smaller files get transferred correctly. I believe upto 161280 bytes get transferred correctly.

EDIT: I did some more testing and narrowed down the cause. It seems that

outStream.write(mybytearray, 0, mybytearray.length);

in the sending code part is NOT writing more than 161280 bytes. I saw this behavior by not closing the socket connection, thereby causing the read in the receiving part to "block" on 161280 bytes. What is wrong with the bluetooth output stream here? What am I doing wrong?

EDIT 2: Doing this lets it through.

for(int i = 0 ; i < mybytearray.length ; i++){
    outStream.write(mybytearray[i]);
}

Sending code:

    try {
        outStream = mBluetoothSocket.getOutputStream();
        Log.d(TAG,"outStream created success!");
    } catch (IOException e) {
        Log.d(TAG,
                "ON RESUME: Output stream creation failed.",
                e);
    }

    File myFile = new File(file_name);
    Log.d(TAG,"file /source.pdf created success!");

    byte[] mybytearray = new byte[(int)myFile.length()];
    Log.d(TAG,"file length() =" + (int)myFile.length());

    FileInputStream fis = new FileInputStream(myFile);
    Log.d(TAG,"fis created");

    BufferedInputStream bis = new BufferedInputStream(fis,1272254 );
    Log.d(TAG,"bis created success");

    bis.read(mybytearray,0,mybytearray.length);
    Log.d(TAG,"ALL Bytes read from bis");

    outStream.write(mybytearray, 0, mybytearray.length);
    Log.d(TAG,"BYTES WRITTEN to OUTSTREAM of socket");


    outStream.flush();
    Log.d(TAG,"bytes flushed");
    outStream.close();

Receiving code:

// Attach the i/p stream to the socket
    try {
        InputStream in = socket.getInputStream();
        mIn = in;
        Log.d(TAG, "input stream acquired");

    } catch (IOException e1) {
        e1.printStackTrace();
    }
    // Create output streams & write to file
    FileOutputStream fos = new FileOutputStream(
            Environment.getExternalStorageDirectory()
                    + "/copy.pdf");
    try {
        bytesRead = mIn.read(buffer, 0, buffer.length);
        Log.d(TAG, "bytesRead first time =" + bytesRead);
        current = bytesRead;

        do {
            Log.d(TAG, "do-while -- current: " + current);
            bytesRead = mIn.read(buffer, current,
                    buffer.length - current);
            Log.d(TAG, "bytesRead: =" + bytesRead);

            if (bytesRead >= 0)
                current += bytesRead;
        } while (bytesRead > -1);
    } catch (IOException e) {
        e.printStackTrace();
        Log.d(TAG, "do while end:-- buffer len= "
                + buffer.length + "  current: " + current);

        fos.write(buffer);
        Log.d(TAG, "fos.write success! buffer: "
                + buffer.length + "  current: " + current);

        fos.flush();
        fos.close();
    }
}
socket.close();

Logcat:

D/ReceiveService( 5761): do-while -- current: 155232
D/ReceiveService( 5761): bytesRead: =1008
D/ReceiveService( 5761): do-while -- current: 156240
D/ReceiveService( 5761): bytesRead: =1008
D/ReceiveService( 5761): do-while -- current: 157248
D/ReceiveService( 5761): bytesRead: =1008
D/ReceiveService( 5761): do-while -- current: 158256
D/ReceiveService( 5761): bytesRead: =1008
D/ReceiveService( 5761): do-while -- current: 159264
D/ReceiveService( 5761): bytesRead: =1008
D/ReceiveService( 5761): do-while -- current: 160272
D/ReceiveService( 5761): bytesRead: =1008
D/ReceiveService( 5761): do-while -- current: 161280
W/System.err( 5761): java.io.IOException: Software caused connection abort
W/System.err( 5761):    at android.bluetooth.BluetoothSocket.readNative(Native Method)
W/System.err( 5761):    at android.bluetooth.BluetoothSocket.read(BluetoothSocket.java:307)
W/System.err( 5761):    at   android.bluetooth.BluetoothInputStream.read(BluetoothInputStream.java:96)
W/System.err( 5761):    at com.bt.server.ReceiveService$AcceptThread.run(ReceiveService.java:141)

I am using Motorola Milestone. Android 2.1

shiraz
  • 1,208
  • 2
  • 12
  • 26
  • Paste the logs of the sending device including timestamps. Hope sending & receiving is being done on a separate thread. – Dheeraj Vepakomma Mar 17 '12 at 07:56
  • Seach SO for "Software caused connection abort". – Dheeraj Vepakomma Mar 17 '12 at 08:00
  • Yes they are on separate threads. Different phones. The sending device sends data almost instantaneously. – shiraz Mar 17 '12 at 08:06
  • 1
    Hey can you post the full source code I am also stuck on this sending file problem using Bluetooth. If you share your code then I can get solution of my problem. Thanks in advance. – anddev Mar 28 '12 at 05:04
  • 1
    hi,I tried this code.But i have some doubts.1) Is any protocol is used for transfering data from one phone to another here.If so which the protocol used? – Sanal Varghese Apr 17 '12 at 10:16
  • Well I am using simple bt sockets here. http://developer.android.com/reference/android/bluetooth/BluetoothDevice.html#createRfcommSocketToServiceRecord%28java.util.UUID%29 – shiraz Apr 17 '12 at 13:41
  • This is just part of the code and I believe it won't work as is. Go through the Bluetooth chat program http://developer.android.com/resources/samples/BluetoothChat/index.html. Its an excellent reference. I derived my code from it too. – shiraz Apr 17 '12 at 13:44
  • @Shjiraz.... Can u post your full code? – Denny Sharma Oct 29 '13 at 10:46
  • @shiraz what is "mIn" in your code – pranjal khanduri Apr 23 '18 at 11:38

1 Answers1

3

I was able to solve this problem by sending small chunks of data out to the bluetooth outstream. It turned out that 8 * 1024 was a good buffer size, which helped in sending out data seamlessly over the stream as well as preventing corruption of data at the receiving end.

BufferedInputStream bis = new BufferedInputStream(fis, 8 * 1024);


byte[] buffer = new byte[8192];
int len
while ((len = bis.read(buffer)) != -1) {
    outStream.write(buffer, 0, len);
}
shiraz
  • 1,208
  • 2
  • 12
  • 26
  • 2
    I am having a problem with send simple image file over bluetooth using bluetooth chat sample. Actually, My requirement was an send any file oevr bluetooth. For this how can I set the buffer size, how to receive small chunk of data and combine it at the receiver side. Could u please share me your code. – M Vignesh Jan 02 '13 at 15:29
  • can you have a look at this http://stackoverflow.com/questions/16954082/send-text-file-using-bluetooth-sockets-in-android – neerajDorle Jun 06 '13 at 05:29
  • I'm developing a solution which is based on your approach, but I'm stuck on outStream.write(mybytearray, 0, mybytearray.length);, it simply won't write data to output stream. No errors, file is created with size of 0 bytes and logs stop right after bis.read(mybytearray,0,mybytearray.length). Any ideas? – jacek_podwysocki Nov 21 '16 at 23:58
  • @shiraz Dear shiraz I am struggling to transfer any file greater than 1024 bytes. Can you please share your send/receive code here to help us who are struggling? Thank you – Ana Oct 20 '17 at 08:48