2012-12-04 54 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

相关问题