Android手机和蓝牙设备通信错误

时间:2017-11-16 04:37:07

标签: java android bluetooth elm327

我正在修改Android蓝牙示例程序,以通过蓝牙实现我的Android手机和ELM327模块之间的通信。

应用程序是使用Android studio从程序构建的。该应用程序在两个Android手机之间运行良好。

然而,当我在我的一个Android手机中运行它并尝试与ELM327模块通信时,来自ELM327的输入消息被破坏,并且一段时间有非常严重的延迟。你能解释为什么不同的外表会发生吗?如果您能帮助纠正该计划,我们将非常感激。

以下是此应用程序示例代码的一部分:

BluetoothChat.java:处理程序

            private final Handler mHandler = new Handler() {
                    @Override
                    public void handleMessage(Message msg) {
                        switch (msg.what) {
                            case MESSAGE_WRITE:
                                byte[] writeBuf = (byte[]) msg.obj;

                                // construct a string from the buffer
                                String writeMessage = new String(writeBuf);
                                Log.d("ELM327", "message is send:" + writeMessage +"; length is:" + writeMessage.length());
                                mAdapter.notifyDataSetChanged();
                                messageList.add(new androidRecyclerView.Message(counter++, writeMessage, "Me"));
                                break;
                            case MESSAGE_READ:
                                byte[] readBuf = (byte[]) msg.obj;
                                //Log.d("ELM327", "message is received:" + readBuf + "; length is:" + readBuf.length);
                                // construct a string from the valid bytes in the buffer
                                String readMessage = new String(readBuf, 0, msg.arg1);
                                Log.d("ELM327", "message is received:" + readMessage +"; length is:" + readMessage.length());
                                mAdapter.notifyDataSetChanged();
                                messageList.add(new androidRecyclerView.Message(counter++, readMessage, mConnectedDeviceName));
                                break;
                            case MESSAGE_DEVICE_NAME:
                                // save the connected device's name
                                mConnectedDeviceName = msg.getData().getString(DEVICE_NAME);
                                Toast.makeText(getApplicationContext(), "Connected to "
                                        + mConnectedDeviceName, Toast.LENGTH_SHORT).show();
                                break;
                            case MESSAGE_TOAST:
                                Toast.makeText(getApplicationContext(), msg.getData().getString(TOAST),
                                        Toast.LENGTH_SHORT).show();
                                break;
                        }
                    }
                };

BluetoothChatService.java:用于读取输入消息的运行函数

            private class ConnectedThread extends Thread {
                private final BluetoothSocket mmSocket;
                private final InputStream mmInStream;
                private final OutputStream mmOutStream;

                public ConnectedThread(BluetoothSocket socket) {
                    mmSocket = socket;
                    InputStream tmpIn = null;
                    OutputStream tmpOut = null;
                    // Get the BluetoothSocket input and output streams
                    try {
                        tmpIn = socket.getInputStream();
                        tmpOut = socket.getOutputStream();
                    } catch (IOException e) {
                    }
                    mmInStream = tmpIn;
                    mmOutStream = tmpOut;
                }

                public void run() {
                    byte[] buffer = new byte[1024];
                    int bytes;
                    // Keep listening to the InputStream while connected
                    while (true) {
                        try {
                            // Read from the InputStream
                            bytes = mmInStream.read(buffer);
                            // Send the obtained bytes to the UI Activity
                            mHandler.obtainMessage(BluetoothChat.MESSAGE_READ, bytes, -1, buffer)
                                    .sendToTarget();
                        } catch (IOException e) {
                            connectionLost();
                            break;
                        }
                    }
                }

1 个答案:

答案 0 :(得分:0)

我错过了添加" \ r"在我从电话应用程序发送到ELM327的每条命令消息的末尾。

添加" \ r"后,我可以从ELM327收到正确的反馈。