0

Bluetooth を介して Android 携帯電話と ELM327 モジュール間の通信を実現するために、Android Bluetooth サンプル プログラムを変更しています。

アプリケーションは、Android studio を使用してプログラムからビルドされます。このアプリケーションは、2 台の Android フォン間で非常にうまく機能します。

ただし、Android フォンの 1 つで実行し、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;
                        }
                    }
                }
4

1 に答える 1