0

I programmed a Bluetooth messaging app, the app works correctly when it connects and pairs with another phone, but when with HC-05 it connects, I get the received message as a syllable (first letter and then the rest of the letters) so what is the reason for that?

Screenshot My Android App of BTChat

this my code :

private class ConnectedThread extends Thread {
private final BluetoothSocket mmSocket;
private final InputStream mmInStream;
private final OutputStream mmOutStream;
public ConnectedThread(BluetoothSocket socket, String socketType) {
    mmSocket = socket;
    InputStream tmpIn = null;
    OutputStream tmpOut = null;
    try {
        tmpIn = socket.getInputStream();
        tmpOut = socket.getOutputStream();
    } catch (IOException e) {

    }
    mmInStream = tmpIn;
    mmOutStream = tmpOut;
    mState = STATE_CONNECTED;
}

public void run() {
    byte[] buffer = new byte[1024];
    int bytes;
    while (mState == STATE_CONNECTED) {
        try {
            bytes = mmInStream.read(buffer);
            //***NEW Edition
            String readMessage = new String(buffer, 0, bytes);
            mHandler.obtainMessage(Constants.MESSAGE_READ, bytes, -1, buffer).sendToTarget();
        } catch (IOException e) {
            connectionLost();
            break;
        }
    }
}

1 Answers1

0

Two reasons:

  • packet data transmition by BT protocol. If slave have buffered only one char and master make transmit request than slave send only one char
  • multitasking in android OS. Your run function not run whole time. There is many threads and proccesses which compete for hw cores. During run is suspended many packets can arrive.

You must count that stream not deliver data continously

Peter Plesník
  • 524
  • 1
  • 2
  • 7