2014-06-19 3 views
2

나는 블루투스를 통해 마이크로 컨트롤러와 통신하는 애플리케이션을 개발 중이다. 성공적으로 수신되고있는 바이트 배열로 구성된 패킷을 보내고 있습니다. 그러나, 마이크로 컨트롤러에 의해 전송 된 바이트 배열은 나에게 수신되지 않습니다. 오류가 발생하지 않습니다. 수신 코드는 다음과 같습니다.블루투스를 통해 마이크로 컨트롤러와 안드로이드 통신

private void beginListenForData() { 
    // TODO Auto-generated method stub 
    Log.e("clientsocket", "listening data"); 
    final Handler handler = new Handler(); 
    final byte delimiter = 10; // This is the ASCII code for a newline 
           // character 

    stopWorker = false; 
    readBufferPosition = 0; 
    readBuffer = new byte[1024]; 
    workerThread = new Thread(new Runnable() { 
     public void run() { 
      while (!Thread.currentThread().isInterrupted() && !stopWorker) { 
       try { 
        int bytesAvailable = mmInputStream.available(); 

        if (bytesAvailable > 0) { 
         Log.e("bytes", "" + bytesAvailable); 

         Log.e("bytes", "" + bytesAvailable); 
         byte[] packetBytes = new byte[bytesAvailable]; 
         Log.v("packet Bytes", "" + packetBytes); 
         for (int m = 0; m < packetBytes.length; m++) 
          Log.e("checking array", "" + packetBytes[m]); 
         Log.v("packet bytes len", "" + packetBytes.length); 
         mmInputStream.readFully(packetBytes, 0, 
           packetBytes.length); 

         for (int i = 0; i < bytesAvailable; i++) { 
          byte b = packetBytes[i]; 
          if (b == delimiter) { 
           byte[] encodedBytes = new byte[readBufferPosition]; 
           System.arraycopy(readBuffer, 0, 
             encodedBytes, 0, 
             encodedBytes.length); 
           for (int k = 0; k < encodedBytes.length; k++) 
            Log.v("encoded bytes", "" 
              + encodedBytes[k]); 

           int[] iarray = new int[encodedBytes.length - 1]; 
           int k = 0; 
           for (int j = 0; j < encodedBytes.length - 1; j++) 
            iarray[k++] = encodedBytes[j] & 0xff; 
           StringBuilder sb = new StringBuilder(
             iarray.length); 
           for (int l : iarray) 
            sb.append(l); 
           for (k = 0; k < iarray.length; k++) 
            Log.v("iarray", "" + iarray[k]); 

           final String data = sb.toString(); 
           // final String data = new String(
           // encodedBytes, "UTF-8"); 

           Log.e("data server", data); 

           readBufferPosition = 0; 
           Log.e("clientserver", "" + data); 
           handler.post(new Runnable() { 
            public void run() { 

             myLabel.append("\n" + data); 

             System.gc(); 
             Log.e("clientserver", "" + data); 
            } 
           }); 
          } else { 

           readBuffer[readBufferPosition++] = b; 

          } 
         } 
        } 
       } catch (IOException ex) { 

        stopWorker = true; 
        myLabel.append("\n" + "Try Again!"); 

       } 
      } 
     } 
    }); 

    workerThread.start(); 

} 

처음에는 마이크로 컨트롤러가 문자열로 데이터를 전송할 때 나를 성공적으로 읽었습니다. 그러나 바이트 배열로 전환 할 때 문제가 시작되었습니다. 누군가 이걸로 나를 도울 수 있니? 감사 :

+0

ur logcat을 게시 할 수 있습니까? – Gattsu

+1

전송 코드가 무엇입니까? –

+0

문제가 해결되었습니다! 구분 기호가 양 끝에서 일치하지 않았기 때문입니다. 걱정 해주셔서 감사합니다. :) – Prat

답변

0

해결 된 문제! 구획 문자가 양쪽에서 일치하지 않았기 때문입니다. 걱정 해주셔서 감사합니다. :)

관련 문제