javaandroidbluetoothelm327

Android phone and bluetooth device communication error


I am modifying an Android bluetooth sample program to realise the communication between my android phone and an ELM327 module through Bluetooth.

The application is build from the program using Android studio. The application works very well between two android phones.

However, when I run it in one of my android phone and try to communicate with ELM327 module, the input messages coming from ELM327 are broken and some time has very serious delay. Could you please explain why the different apperances happen? If you can help to rectify the program, it will be really appreciated.

Below is part of the sample code for this application:

BluetoothChat.java: Handler

            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: run function for Reading input messages

            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;
                        }
                    }
                }

Solution

  • I missed adding "\r" at the end of each command message that I send from phone app to ELM327.

    After adding "\r", I can receive correct feedback from ELM327.