2017-11-16 12 views
0

AndroidのBluetoothサンプルプログラムを変更して、Android携帯とELM327モジュール間のBluetooth通信を実現します。Android携帯電話とBluetoothデバイスの通信エラー

アプリケーションは、Androidスタジオを使用してプログラムからビルドされています。このアプリケーションは、2台の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; 
         } 
        } 
       } 

答えて

0

入力メッセージを読み取るための実行機能、私は逃しました電話アプリからELM327に送信する各コマンドメッセージの最後に「\ r」を追加します。

「\ r」を追加した後、ELM327から正しいフィードバックを受け取ることができます。

関連する問題