【问题标题】:BluetoothChat-to-ELM327 split response messageBluetoothChat-to-ELM327 拆分响应消息
【发布时间】:2014-08-19 02:45:04
【问题描述】:

我正在尝试使用 Android BluetoothChat 示例与 ELM327 OBDII 蓝牙加密狗进行通信。我可以毫无问题地连接到设备,并且从 BluetoothChat 到 ODBII 设备的消息似乎被设备正确传输和接收。

但是,来自 OBDII 设备的响应消息通常被拆分为多个消息、加扰或缺少字符。

例如,ati 命令需要 3 次尝试才能收到完整的预期响应: Me: ati OBDII: a OBDII: 327 OBDII: 327 OBDII: 327 v1.5 > Me: ati OBDII: 1 OBDII: 1 OBDII: 1.5 >v OBDII: 1.5 > Me: ati OBDII: ELM327 v1.5 >

同样,发送010c 应触发包含三个十六进制对的单行响应。相反,我通常(但不总是)得到如下结果: Me: 010c OBDII: OBDII: 4 OBDII: 3C OBDII: 3 OBDII: 3C C OBDII: 3C OBDII: OBDII: OBDII: >

我尝试了几种不同的波特率和不同的 OBDII 协议,但更改默认设置似乎只会让事情变得更糟。 我的响应消息处理有问题吗?为什么响应消息会分裂?蓝牙加密狗与可用的应用程序(例如 Torque)正常工作,所以我认为设备没有故障。

我使用的代码与 BluetoothChat 项目(来源 here)几乎相同。我只修改了我的蓝牙设备的 UUID,并在传出消息中添加了一个回车符(根据这个 StackOverflow question)。

更改 1(在 BluetoothChatService.java 中):

    // Unique UUID for this application
private static final UUID MY_UUID_SECURE =
    //UUID.fromString("fa87c0d0-afac-11de-8a39-0800200c9a66");
    UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
private static final UUID MY_UUID_INSECURE =
    //UUID.fromString("8ce255c0-200a-11e0-ac64-0800200c9a66");
    UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");

更改 2(在 BluetoothChat.java 中):

    // The action listener for the EditText widget, to listen for the return key
private TextView.OnEditorActionListener mWriteListener =
    new TextView.OnEditorActionListener() {
    public boolean onEditorAction(TextView view, int actionId, KeyEvent event) {
        // If the action is a key-up event on the return key, send the message
        if (actionId == EditorInfo.IME_NULL && event.getAction() == KeyEvent.ACTION_UP) {
            String message = view.getText().toString();
            //sendMessage(message);
            sendMessage(message + "\r");
        }
        if(D) Log.i(TAG, "END onEditorAction");
        return true;
    }
};

ELM327 manual for reference

【问题讨论】:

    标签: android bluetooth obd-ii elm327


    【解决方案1】:

    我在this BluetoothSPP project by user akexorcist on Github 中找到了我的消息拆分问题的解决方案。类ConnectedThread的相关函数如下:

         public void run() {
       byte[] buffer;
       ArrayList<Integer> arr_byte = new ArrayList<Integer>();
    
       // Keep listening to the InputStream while connected
       while (true) {
          try {
                int data = mmInStream.read();
                if(data == 0x0A) { 
                } else if(data == 0x0D) {
                    buffer = new byte[arr_byte.size()];
                    for(int i = 0 ; i < arr_byte.size() ; i++) {
                        buffer[i] = arr_byte.get(i).byteValue();
                    }
                   // Send the obtained bytes to the UI Activity
                   mHandler.obtainMessage(BluetoothState.MESSAGE_READ
                            , buffer.length, -1, buffer).sendToTarget();
                   arr_byte = new ArrayList<Integer>();
                } else {
                   arr_byte.add(data);
                }
           } catch (IOException e) {
               connectionLost();
               // Start the service over to restart listening mode
               BluetoothService.this.start(BluetoothService.this.isAndroid);
               break;
           }
       }
    }
    

    显然,如果我错了,请纠正我,.read() 在没有帮助的情况下无法获取和维护流中出现的任何字节的格式。

    【讨论】:

    • 顶部的链接已损坏
    猜你喜欢
    • 1970-01-01
    • 2023-03-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-16
    相关资源
    最近更新 更多