I am trying to send bytes from my circuit board to my android app. My hardware is a Microchip BM78 SPP chip and I know for certain the hardware transmits bytes because when I connect an RFCOMM monitor on my computer to the hardware the bytes are received forever. However, on my Android application, the bytes are received from thirty seconds to two minutes, and then the program just stops. The app doesn't crash, it just appears to stop running. The run window and the logcat window show the following when the program stops: Run and Logcat window when program stops

enter image description here

Why is the last buffer full of diamond question marks? Every time the program stops the last buffer is full of diamond question marks and is longer than 60 bytes. Also, when I debug the program, when it stops I don't get any error messages. The program just silently stops. One time I got the message java.io.exception: BT socket closed read return: -1. Why is my Bluetooth socket closing? Thanks in advance.

Here is my code for the ConnectedThread.java:

public class ConnectedThread extends Thread {
private final BluetoothSocket mmSocket;
private final InputStream mmInStream;
private final OutputStream mmOutStream;
private final Handler mHandler;

public ConnectedThread(BluetoothSocket socket, Handler handler) {
    mmSocket = socket;
    mHandler = handler;
    InputStream tmpIn = null;
    OutputStream tmpOut = null;

    // Get the input and output streams, using temp objects because
    // member streams are final
    try {
        tmpIn = socket.getInputStream();
        tmpOut = socket.getOutputStream();
    } catch (IOException e) { }

    mmInStream = tmpIn;
    mmOutStream = tmpOut;
}

@Override
public void run() {
    int bytes; // bytes returned from read()
    // Keep listening to the InputStream until an exception occurs
    while (true) {
        try {
            // Read from the InputStream
            bytes = mmInStream.available();
            byte[] buffer = new byte[60];
            if(bytes != 0) {
                SystemClock.sleep(100); //pause and wait for rest of data. Adjust this depending on your sending speed. Originally 100
                bytes = mmInStream.read(buffer, 0, 50); // record how many bytes we actually read
                mHandler.obtainMessage(AndroidGame.MESSAGE_READ, bytes, -1, buffer)
                            .sendToTarget(); // Send the obtained bytes to the UI activity
            }
        } catch (IOException e) {
            e.printStackTrace();

            break;
        }

    }

}

/* Call this from the main activity to send data to the remote device
public void write(String input) {
    byte[] bytes = input.getBytes();           //converts entered String into bytes
    try {
        mmOutStream.write(bytes);
    } catch (IOException e) { }
}

/* Call this from the main activity to shutdown the connection */
public void cancel() {
    try {
        mmSocket.close();
    } catch (IOException e) { }
}

}

0

There are 0 best solutions below