2011-04-26 5 views
0

블루투스를 통해 수신하는 일부 바이너리 데이터를 처리하고 있는데, 많은 양의 데이터가 동시에 전송되면 데이터 스트림에 누락되거나 유효하지 않은 바이트가 생깁니다. 좀 더 일관된 응답을 얻으려면 도움이 필요하기 때문에 누군가가 나를 도와 줄 수 있기를 바랬습니다. 나는 단순히 "나쁜 데이터를 버리는"일을하는 것으로 괜찮습니다. 관련 메서드는 다음과 같습니다. 연결 스레드 내에서 실행됩니다.데이터 처리로 인해 Android에서 블루투스를 통해 데이터 무결성이 저하되었습니다.

public void run() { 
     Log.i(TAG, "BEGIN mConnectedThread"); 
     byte[] buffer = new byte[1024]; 
     int num_bytes = 0; 
     // Keep listening to the InputStream while connected 
     while (true) { 
      try { 
       // Read from the InputStream 
       mmInStream.read(buffer, 0, 11); 
       processBytes(buffer); 

      } catch (IOException e) { 
       Log.e(TAG, "disconnected", e); 
       connectionLost(); 
       break; 
      } 
     } 
    } 

    private void processBytes(byte[] bytes) throws IOException { 
     if (bytes[0] != delimiter) { 
      int c; 
      do { 
       c = mmInStream.read(); 
      } while (c != delimiter && c != -1); 
      if (c == -1) { 
       return; 
      } 
     } 
     /*Some processing happens here to generate the variables cellId and 
         voltage and cellId2 and voltage2 
        */ 
     // Send the obtained bytes to the UI Activity 
     mHandler.obtainMessage(BluetoothInterface.DATA_READ, 
       new DataPoint(cellId, voltage)).sendToTarget(); 
     // Send the obtained bytes to the UI Activity 
     mHandler.obtainMessage(BluetoothInterface.DATA_READ, 
       new DataPoint(cellId2, voltage2)).sendToTarget(); 

    } 

답변

1

이전에이 문제가있었습니다. processBytes를 호출하기 전에 스트림을 문자열 객체로 읽음으로써 해결할 수 있을지 확실하지 않습니다.

관련 문제