2017-11-16 12 views
0

블루투스를 통해 Android 휴대 전화와 ELM327 모듈 간의 통신을 실현하기 위해 Android 블루투스 샘플 프로그램을 수정합니다.Android 휴대 전화 및 블루투스 기기 통신 오류

응용 프로그램은 Android 스튜디오를 사용하는 프로그램에서 빌드됩니다. 이 응용 프로그램은 두 개의 안드로이드 전화간에 매우 잘 작동합니다.

그러나 내 안드로이드 폰 중 하나에서 실행하고 ELM327 모듈과 통신하려고하면 ELM327에서 오는 입력 메시지가 손상되고 시간이 많이 걸릴 수 있습니다. 왜 다른 apperances가 발생하는지 설명해 주시겠습니까? 프로그램을 바로 잡을 수 있다면 정말 감사하겠습니다.

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에서 올바른 피드백을받을 수 있습니다.