蓝牙聊天应用程序回复我发送的相同文本

时间:2019-01-24 17:41:46

标签: android bluetooth obd-ii

我正在使用一个应用程序通过蓝牙将数据发送到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();
        }
    }
}

1 个答案:

答案 0 :(得分:0)

也许您的阅读不足–确保将所有阅读的片段连接起来,直到收到实际的提示\r>

ELM327通常以 echo模式开头,在该模式中,它回显您提供给它的每个命令,这也许可以解释您为何回读它的原因。使用ATE0来关闭此行为。

通常,https://www.sparkfun.com/datasheets/Widgets/ELM327_AT_Commands.pdf会解释所有这些情况。