got an error in below accept thread code after reading all data still it gives error socket closed read return -1

502 Views Asked by At

I needed some guidence for streaming to transfer file or data with the remote devices or server. Help me to find what happening in that code just stucked from last 5 days with this error:

W/System.err: java.io.IOException: bt socket closed, read return: -1 
W/System.err: at android.bluetooth.BluetoothSocket.read(BluetoothSocket.java:495) 08-12 03:49:13.242 6973-11690/io.connection.bluetooth 
W/System.err: at android.bluetooth.BluetoothInputStream.read(BluetoothInputStream.java:96) 08-12 03:49:13.242 6973-11690/io.connection.bluetooth 
W/System.err: at io.connection.bluetooth.Thread.AcceptThread$readFile.run(AcceptThread.java:90)

Code:

package io.connection.bluetooth.Thread;

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothServerSocket;
import android.bluetooth.BluetoothSocket;
import android.os.Environment; 
import android.util.Log;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import io.connection.bluetooth.utils.Constants;


public class AcceptThread extends Thread {
private final BluetoothServerSocket serverSocket;
private static final String TAG = "AcceptThread";
BluetoothSocket socket = null;

public AcceptThread(BluetoothAdapter bluetoothAdapter) {
    BluetoothServerSocket tmp = null;

    try {
        tmp = bluetoothAdapter
                .listenUsingRfcommWithServiceRecord(
                        Constants.NAME_UUID, Constants.uuid);

    } catch (IOException e) {
    }
    serverSocket = tmp;
 }

public void run() {


    while (true) {
        try {
            socket = serverSocket.accept();

            if (socket.isConnected()) {
                Log.d(TAG, "run:  connection successfull");
                Log.d(TAG, "run: " + socket.getRemoteDevice().getName() + "  " +
                        socket.getRemoteDevice().getAddress());
                readFile readfile = new readFile(socket);
                readfile.start();
            }


        } catch (IOException e) {
            e.printStackTrace();
            Log.d(TAG, "run: " + e.getMessage());
        }

    }
}

class readFile extends Thread {
    private static final String TAG = "readFile";
    BluetoothSocket socket = null;
    InputStream in = null;
    OutputStream out = null;

    readFile(BluetoothSocket socket) {
        this.socket = socket;
    }

    @Override
    public void run() {
        Log.d(TAG, "run:  reading Start");
        byte[] bytes = new byte[16 * 1024];
        int count;
        int total = 0;
        String filename = System.nanoTime() + ".jpg";
        File file = new File(Environment.getExternalStorageDirectory(), filename);
        Log.d(TAG, "run:  file path "+file.getPath());

        try {
            in = socket.getInputStream();
            out = new FileOutputStream(file);

            try {
   //got error on this while loop after reading all data from stream
         while ((count = in.read(bytes)) > 0) {
                    total += count;
                    out.write(bytes, 0, count);
                    Log.d(TAG, "run: " + total + "    " + count + "  " + in.available());

                }
                Log.d(TAG, "run: count End " + in.available());
                out.close();
                in.close();
                socket.close();

            } catch (Exception e) {
                e.printStackTrace();

            }

        } catch (Exception e) {
            e.printStackTrace();
            Log.d(TAG, "run:  readFile  " + e.getMessage());
        } finally {
            try {

                Log.d(TAG, "run: socket close");
                socket.close();
            } catch (Exception e1) {
                e1.printStackTrace();
            }
        }
    }


   }
 }  
1

There are 1 best solutions below

3
On

Issue:

"If no byte is available because the end of the stream has been reached, the value -1 is returned. This method blocks until input data is available, the end of the stream is detected, or an exception is thrown." https://docs.oracle.com/javase/7/docs/api/java/io/InputStream.html#read()

You are reaching the end of the stream, but still calling read() in the while loop check. The last read() will probably take <1024 bytes, but still more than zero, so the while loop will go through one more iteration (which is intended). But when it tries checking the number of bytes read again, it runs into the error.

Note:

public int read(byte[] b)

"The read(b) method for class InputStream has the same effect as:

read(b, 0, b.length)"

and

public int read(byte[] b, int off, int len) throws IOException

"The read(b, off, len) method for class InputStream simply calls the method read() repeatedly"