I implemented this app by using a Message Chat via Bluetooth in Android. The app works, it's connect to the obd port and exchange message with it but the problem is that i receive data in multiple rows like this:
Why the reply from the obd port coming like that? There is some problem in my incoming data stream?
Here some code about i take reply:
private class ConnectedThread extends Thread {
private final BluetoothSocket mmSocket;
private final InputStream mmInStream;
private final OutputStream mmOutStream;
public ConnectedThread(BluetoothSocket socket) {
Log.d(TAG, "create ConnectedThread: ");
mmSocket = socket;
InputStream tmpIn = null;
OutputStream tmpOut = null;
// Get the BluetoothSocket input and output streams
try {
tmpIn = socket.getInputStream();
tmpOut = socket.getOutputStream();
} catch (IOException e) {
Log.e(TAG, "temp sockets not created", e);
}
mmInStream = tmpIn;
mmOutStream = tmpOut;
mState = STATE_CONNECTED;
}
public void run() {
Log.i(TAG, "BEGIN mConnectedThread");
byte[] buffer = new byte[1024];
int bytes;
// Keep listening to the InputStream while connected
while (mState == STATE_CONNECTED) {
try {
// Read from the InputStream
bytes = mmInStream.read(buffer);
// Send the obtained bytes to the UI Activity
mHandler.obtainMessage(Constants.MESSAGE_READ, bytes, -1, buffer)
.sendToTarget();
} catch (IOException e) {
Log.e(TAG, "disconnected", e);
connectionLost();
break;
}
}
}
/**
* Write to the connected OutStream.
*
* @param buffer The bytes to write
*/
public void write(byte[] buffer) {
try {
mmOutStream.write(buffer);
// Share the sent message back to the UI Activity
mHandler.obtainMessage(Constants.MESSAGE_WRITE, -1, -1, buffer)
.sendToTarget();
} catch (IOException e) {
Log.e(TAG, "Exception during write", e);
}
}
public void cancel() {
try {
mmSocket.close();
} catch (IOException e) {
Log.e(TAG, "close() of connect socket failed", e);
}
}
}
And the Handler that manage conversation with obd port and other things:
/**
* The Handler that gets information back from the MyBluetoothService
*/
private final Handler mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
//FragmentActivity activity = getActivity();
switch (msg.what) {
case Constants.MESSAGE_STATE_CHANGE:
switch (msg.arg1) {
case MyBluetoothService.STATE_CONNECTED:
setStatus(getString(R.string.title_connected_to, mConnectedDeviceName));
mConversationArrayAdapter.clear();
/*String EchoOffCommand = "ATE0\r";
String LineFeedOff = "ATL0\r";
sendMessage(EchoOffCommand);
sendMessage(LineFeedOff);*/
break;
case MyBluetoothService.STATE_CONNECTING:
setStatus(R.string.title_connecting);
break;
case MyBluetoothService.STATE_LISTEN:
case MyBluetoothService.STATE_NONE:
setStatus(R.string.title_not_connected);
break;
}
break;
case Constants.MESSAGE_WRITE:
byte[] writeBuf = (byte[]) msg.obj;
// construct a string from the buffer
String writeMessage = new String(writeBuf);
mConversationArrayAdapter.add("Me: " + writeMessage);
break;
case Constants.MESSAGE_READ:
byte[] readBuf = (byte[]) msg.obj;
// construct a string from the valid bytes in the buffer
String readMessage = new String(readBuf, 0, msg.arg1);
mConversationArrayAdapter.add(mConnectedDeviceName + ": " + readMessage);
break;
case Constants.MESSAGE_DEVICE_NAME:
// save the connected device's name
mConnectedDeviceName = msg.getData().getString(Constants.DEVICE_NAME);
Toast.makeText(getApplicationContext(), "Connected to "
+ mConnectedDeviceName, Toast.LENGTH_SHORT).show();
break;
case Constants.MESSAGE_TOAST:
Toast.makeText(getApplicationContext(), msg.getData().getString(Constants.TOAST),
Toast.LENGTH_SHORT).show();
break;
}
}
};
OBD2 adapters have little memory. They will not necessarily transmit the whole answer in one go which breaks an assumption of your chat program.
Your receiving process needs to handle the response fragments, buffering all, and – once you parse the response terminator like \r\r>
– reassemble the fragments and deliver it to the upper layer.