Android与OBD口通讯单行接收数据
Android and OBD port comunication received data in single row
我在 Android 中通过蓝牙使用消息聊天实现了这个应用程序。
该应用程序有效,它连接到 obd 端口并与其交换消息,但问题是我收到多行数据,如下所示:
为什么obd端口的回复是这样的?
我的传入数据流有问题吗?
这里有一些关于我回复的代码:
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);
}
}
}
以及管理与 obd 端口和其他事物的对话的处理程序:
/**
* 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 适配器内存不足。他们不一定会一次性传输整个答案,这打破了您的聊天程序的假设。
您的接收进程需要处理响应片段,缓冲所有片段,并且 – 一旦您像 \r\r>
那样解析响应终止符 – 重新组合片段并将其传递到上层。
我在 Android 中通过蓝牙使用消息聊天实现了这个应用程序。 该应用程序有效,它连接到 obd 端口并与其交换消息,但问题是我收到多行数据,如下所示:
为什么obd端口的回复是这样的? 我的传入数据流有问题吗?
这里有一些关于我回复的代码:
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);
}
}
}
以及管理与 obd 端口和其他事物的对话的处理程序:
/**
* 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 适配器内存不足。他们不一定会一次性传输整个答案,这打破了您的聊天程序的假设。
您的接收进程需要处理响应片段,缓冲所有片段,并且 – 一旦您像 \r\r>
那样解析响应终止符 – 重新组合片段并将其传递到上层。