2012-10-03 3 views
2

나는 Arduino와 Android 사이의 통신을 위해 Android 용 소프트웨어를 작성 중입니다.Android - Bluetooth를 통해 Arduino에서 이상한 비트 받기

Arduino는 serial.println을 사용하여 데이터를 보냅니다. "It works!"라는 텍스트를 보냅니다.

bytes = mmInStream.read(buffer); 
mHandler.obtainMessage(MESSAGE_READ, bytes, -1, buffer).sendToTarget(); 

을 그리고 대신의 일부 코드 표시, 더 정확하게는 [[email protected]를 표시합니다

안드로이드는 이런 방식으로 데이터를 수신하고 "작동을!".

이유는 무엇이며 어떻게이 문제를 해결할 수 있습니까?

답변

3

방금 ​​바이트 배열을 인쇄하려고했습니다. Java에서는 객체 [B의 유형을 인쇄하고 주소는 @40e3f9b8입니다.

텍스트를 인쇄하려면 new String(bytes)을 사용하여 (기본 문자 세트를 사용하여) bytearray에서 문자열을 가져온 다음 문자열을 인쇄하십시오.

1

바이트 배열에서 문자열을 만들어야합니다. 문자열 strIncom = new String (buffer, 0, msg.arg1); 의 \ r \ n 핸들러 전체 예 :

h = new Handler() { 
    public void handleMessage(android.os.Message msg) { 
     switch (msg.what) { 
     case RECIEVE_MESSAGE:             // if receive massage 
      byte[] readBuf = (byte[]) msg.obj; 
      String strIncom = new String(readBuf, 0, msg.arg1);     // create string from bytes array 
      sb.append(strIncom);            // append string 
      int endOfLineIndex = sb.indexOf("\r\n");       // determine the end-of-line 
      if (endOfLineIndex > 0) {           // if end-of-line, 
       String sbprint = sb.substring(0, endOfLineIndex);    // extract string 
       sb.delete(0, sb.length());          // and clear 
       txtArduino.setText("Data from Arduino: " + sbprint);   // update TextView 
      } 
      //Log.d(TAG, "...String:"+ sb.toString() + "Byte:" + msg.arg1 + "..."); 
      break; 
     } 
    }; 
}; 

이 APK 및 소스와 전체 예제 프로그램을 참조하십시오

here

관련 문제