BluetoothChat-to-ELM327拆分响应消息

时间:2014-08-19 02:45:04

标签: android bluetooth obd-ii elm327

我正在尝试使用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项目相同(来源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

1 个答案:

答案 0 :(得分:3)

我在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()无法获取和维护流上出现的任何字节的格式,如果没有一些帮助。

相关问题