2012-12-04 55 views
0

我試圖通過藍牙從Android應用程序發送加速器值到PC。我正在研究BluetoothChat演示應用程序。在Android應用程序中,我有一個名爲onSensorChanged的方法,每次加速度更改時都會調用它。該方法看起來像下面:如何從java中的inputStream中讀取每個字符串

@Override 
public void onSensorChanged(SensorEvent e) { 
    // the work done when the accelerometer data changes 
    try { 
     Thread.sleep(25); 
    } catch (InterruptedException e1) { 
     e1.printStackTrace(); 
    } 
    sensorX = e.values[0]; 
    sensorY = e.values[1]; 

    Toast.makeText(BluetoothChat.this, "x coordinate = " + sensorX + "y coordinate = " + sensorY Toast.LENGTH_SHORT).show(); 

    BigDecimal sensorXDec = new BigDecimal(e.values[0]).setScale(2,BigDecimal.ROUND_HALF_UP); 
    BigDecimal sensorYDec = new BigDecimal(e.values[1]).setScale(2,BigDecimal.ROUND_HALF_UP); 


    String vals = String.valueOf(sensorXDec.toPlainString() + "," + sensorYDec.toPlainString()); 

    mChatService.writeFromString(vals); 

} 

方法writeFromString

public void writeFromString(String temp){ 
    // Create temporary object 
    ConnectedThread r; 
    // Synchronize a copy of the ConnectedThread 
    synchronized (this) { 
     if (mState != STATE_CONNECTED) return; 
     r = mConnectedThread; 
    } 
    // Perform the write unsynchronized 
    r.writeString(temp); 
} 

和writeString方法如下:

 public void writeString(String out) { 
     try { 
       if(D) Log.d(TAG, "Sending File....AS STRING"); 
       mmOutStream.write(out.getBytes(), 0, out.getBytes().length); 

     } catch (IOException e) { 
      Log.e(TAG, "Exception during write", e); 
     } 
    } 
在下面的方法

予處理在PC端InputStream的

@Override 
public void run() { 
    try { 
     // prepare to receive data 
     InputStream inputStream = mConnection.openInputStream(); 

     System.out.println("waiting for input"); 

     while (true) { 
      int command = inputStream.read(); 

      if (command == EXIT_CMD) 
      { 
       System.out.println("finish process"); 
       break; 
      } 
      processCommand(command); 
     } 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
} 

問題再次出現:我如何檢索從Android應用程序發送的每組字符串?

+1

檢查這個(讀/轉換的InputStream爲String):http://stackoverflow.com/questions/309424/read-convert-an-inputstream-to-a-string –

回答

1

試試這個

String msg = null;新的InputStreamReader(inputStream) );

msg = br.readLine();

這將解決問題

+0

哎Mohammod,它到底是什麼我需要。我唯一需要改變的是在我的onSensorChanged方法中,我必須在它的最後添加以下內容:mChatService.writeFromString(「\ n」);這樣我可以正確閱讀每一行。否則輸入流是一個巨大的vals字符串。萬分感謝。它正在按預期工作 – bboydflo

1
Scanner scanner = new Scanner (inputStream); 

while(true){ 
    if(!scanner.hasNextInt()){ 
     continue; 
    } 
    // at this point we've got an int, so get it and use it 
    try{ 
     int commmand = scanner.nextInt(); 

     if (command == EXIT_CMD){ 
      System.out.println("finish process"); 
      break; 
     } 

     processCommand(command); 
    } catch (Exception catchThem){ 
     // Deal with the caught exceptions 
    } 
} 

我沒有測試這一點,希望它爲你工作。

+0

嘿IGwe,這真的很容易理解,但我實際上試圖獲得一個字符串,並與它做一些工作。我檢查了掃描器方法,並且hasNextString丟失。任何建議?謝謝回覆 – bboydflo

相關問題