1

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

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

例如,需要三次尝试该ati命令才能收到完整的预期响应: 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 项目相同(来源此处)。我只修改了我的蓝牙设备的 UUID 并在传出消息中添加了一个回车符(根据这个 StackOverflow问题)。

更改 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手册供参考

4

1 回答 1

3

我在 Github 上的用户 akexorcist在这个 BluetoothSPP 项目中找到了我的消息拆分问题的解决方案。类中的相关函数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()没有帮助,就无法获取和维护流中出现的任何字节的格式。

于 2014-08-27T15:45:52.293 回答