我正在使用一个应用程序通过蓝牙将数据发送到elm327,我正在尝试使用AT Z命令,但是我从OBD2返回的所有内容也是AT Z,我的代码丢失了某些内容,或者应该回答这样的问题?我希望AT Z会返回elm327文本(经过playstore应用测试,这就是我得到的)
// runs during a connection with a remote device
private class ReadWriteThread extends Thread {
private final BluetoothSocket bluetoothSocket;
private final InputStream inputStream;
private final OutputStream outputStream;
public ReadWriteThread(BluetoothSocket socket) {
this.bluetoothSocket = socket;
InputStream tmpIn = null;
OutputStream tmpOut = null;
try {
tmpIn = socket.getInputStream();
tmpOut = socket.getOutputStream();
} catch (IOException e) {
}
inputStream = tmpIn;
outputStream = tmpOut;
}
public void run() {
byte[] buffer = new byte[1024];
int bytes;
// Keep listening to the InputStream
while (true) {
try {
// Read from the InputStream
bytes = inputStream.read(buffer);
// Send the obtained bytes to the UI Activity
handler.obtainMessage(MainActivity.MESSAGE_READ, bytes, -1,
buffer).sendToTarget();
} catch (IOException e) {
connectionLost();
// Start the service over to restart listening mode
ChatController.this.start();
break;
}
}
}
// write to OutputStream
public void write(byte[] buffer) {
try {
outputStream.write(buffer);
handler.obtainMessage(MainActivity.MESSAGE_WRITE, -1, -1,
buffer).sendToTarget();
} catch (IOException e) {
}
}
public void cancel() {
try {
bluetoothSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
最佳答案
也许您没有足够阅读-确保将所有阅读的片段连接起来,直到收到实际提示\r>
。
ELM327通常以回显模式开始,在该模式中回显您提供给它的每个命令,这可以解释您为何回读它。使用ATE0
关闭此行为。
通常,https://www.sparkfun.com/datasheets/Widgets/ELM327_AT_Commands.pdf会解释所有这些。