Java – Bluetooth transfer application stops after using InputStream.read() without errors

Bluetooth transfer application stops after using InputStream.read() without errors… here is a solution to the problem.

Bluetooth transfer application stops after using InputStream.read() without errors

I’m trying to make the file transfer bluetooth app work with these sources :

http://developer.android.com/guide/topics/connectivity/bluetooth.html

https://android.googlesource.com/platform/development/+/25b6aed7b2e01ce7bdc0dfa1a79eaf009ad178fe/samples/BluetoothChat/

When I try to get the InputStream bytes using the InputStream.read() method in this way:

public class ConnectedThread extends Thread {

... (some code here)

public void run(){

byte[] buffer = new byte[1024];
        int bytes = -1;

Keep listening to the InputStream while connected
        while (true){

try {

bytes = this.mmInStream.read(buffer);

* this part is not reached
                if (bytes==-1){
                    Log.d("NoData:","-1");  
                }

}
            catch(Exception e){
                Log.d("inStream exception:",e.getMessage());
                break;
            }

}

}

... (some code here)

}

The next part of the code (in this case, the "if" part) never arrives, nor does it reach the Log.D debug output or anything else I put below. I just got this message from LogCat:

BluetoothSocket read in: android.net.LocalStocketImpl$SocketInputStream@f7e
                b08 len: 1024

To transfer data from the client to the server, I do this:

public class MainActivity extends Activity {

... (some code here)

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

clientConnect();
    serverConnect();

}

... (some code here)

public void clientConnect(){

Set<BluetoothDevice> devices;

devices = bConfig.getPairedDevices(); 

if (devices == null){                   
            return;
        }

if (devices.size() > 0) {           

BluetoothDevice device = devices.iterator().next();

ConnectThread connectTransmit = new ConnectThread(device,bConfig.getBluetoothAdapter(),BluetoothConfig.mUUID);
            connectTransmit.start();

Toast.makeText(this, "connected", Toast.LENGTH_SHORT).show();

socket = connectTransmit.mmSocket;
            ConnectedThread connectedThread = new ConnectedThread(socket);

write file bytes to the connected thread, so the thread can receive its own input written bytes later
            File file_to_transfer = new File(Environment.getExternalStorageDirectory() + "/txtTransfer.txt");           

get bytes from our File
            int size = (int) file_to_transfer.length();
            byte[] bytes = new byte[size];

try {

14b are read succesfully, the whole text file 
                BufferedInputStream buf = new BufferedInputStream(new FileInputStream(file_to_transfer));
                buf.read(bytes,0,bytes.length);
                buf.close();                

}catch (FileNotFoundException e){
                Log.d("FileNotFoundException:",e.getMessage());
            }catch (IOException e){ 
                Log.d("IOException:",e.getMessage());
            }

send the data to the server
            connectedThread.start();
            connectedThread.write(bytes);
            connectedThread.cancel();

}

}

... (some code here)

}

AcceptThread (the server part of the implementation) works because when I run the client part connect and then transfer the data, when debugging in the device the LogCat on the server part activates and reaches the thread’s run method, where I call the ConnectedThread implementation, but after it “obviously” reads the bytes, it gets stuck on LogCat without error.

Please tell me how I can finish reading bytes to move to the next part of the process.

Thanks

Solution

You are blocked from waiting for more input.

Marked as... (Some code here). The section should be inside the read loop, after the stream finishes testing. Note: If read() returns -1, this does not mean “no data”, it means that the stream is over and you should close the socket and jump out of the read loop. Otherwise, you should continue to process the data you just read. At the moment you just read and ignore all inputs until the end of the stream, which doesn’t make sense. At best, you can only handle the last part of the buffer, and you won’t know how long it is.

Related Problems and Solutions