2014-06-20 3 views
1

나는 안드로이드 블루투스 작동 원리에 관한 질문이 있습니다. HC-06 방패가있는 arduino에 연결하는 앱이 있습니다. 그리고 수신시 첫 번째 문자 뒤에 새 라인을 얻습니다. 내가 아두 이노Android + Arduino 블루투스 통신

12345

에서 보내고 안드로이드에 내가

1 2345

내가 PC에 아두 이노를 연결 내가 올바른 받았기 때문에이, 안드로이드 아두 이노되지 관한 생각의 ListView에서 볼 말할 수 데이터.

case MESSAGE_READ: 
byte[] readBuf = (byte[]) msg.obj; 
// construct a string from the valid bytes in the buffer 
String readMessage = new String(readBuf, 0, msg.arg1); 
mConversationArrayAdapter.add(readMessage); 
break; 

어떤 아이디어를 디버깅하는 것은 정말 안드로이드인가 아니면 arduino가 실패한 것입니까? 당신이 관심이 있다면 내가 아두 이노 코드를 게시 :

void loop() // run over and over 
{ 
    if (mySerial.available()) 
    Serial.write(mySerial.read()); 
    if (Serial.available()) 
    mySerial.write(Serial.read()); 
} 

편집 "\ n을"에 대한 문제 만 아두 이노 + HC-06 블루투스 방패로 나온다. PC에 연결되어있는 동안 안드로이드 타블렛은 정확한 데이터를 보여줍니다.

+0

왜 BufferedReader 및 readLine()을 사용하지 않습니까 ?? –

답변

1

답은 문자열 결말을 확인하는 것이 었습니다.

private void onBluetoothRead(byte[] buffer, int len) { 

     String output = new String(buffer, 0, len); // Add read buffer to new string 
     Log.i(LOGGER_TAG, String.format("Received: "+ output + " , " + "%d bytes", len)); 
     outputTemp += output; 
     if (outputTemp.endsWith("\n")){ 
     m_deviceOutput.append(outputTemp); // Add (not replace) string to TextView 
     StringTokenizer splitStr = new StringTokenizer(outputTemp, ","); // split string by comma 
     String numberOne = splitStr.nextToken().replaceAll("\\D+",""); // First split string 
     String numberTwo = splitStr.nextToken().replaceAll("\\D+",""); // Second split string 
     m_deviceOutputPrs.setText(numberOne); 
     m_deviceOutputSpeed.setText(numberTwo); 
     outputTemp = ""; 
     } 
    } 
0

전체 메시지를 수신하는 데 시간이 더 필요합니다. 새로운 버퍼가 아닌 동일한 버퍼에 데이터를 추가 할 수도 있습니다. 또는 새로운 입력 데이터를 감지 한 시간과 전체 입력 데이터를 읽은 시간 사이에 약간의 지연을 추가 할 수 있습니다.

+0

kirill 지연 제안 방법? 동일한 버퍼에 데이터를 추가하는 방법은 무엇입니까? 더 구체적으로 말하자면 : 나는 자바 전문가가 아니다 ... – Martynas