简体   繁体   中英

how to read each string from the inputStream in java

I am trying to send accelerator values over bluetooth from an Android App to the PC. I am working on the BluetoothChat demo application. In the Android App I have a method called onSensorChanged that will be called every time when the accelerations changes. the method looks like below:

@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);

}

The method 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);
}

and the writeString method is the following:

        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);
        }
    }

in the following method I process the inputStream on the PC side

    @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();
    }
}

The question again is: How can I retrieve each set of Strings I am sending from the Android App?

Try this

String msg = null;
BufferedReader br = new BufferedReader( new InputStreamReader(inputStream) );

msg = br.readLine();

This will solve problem

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
    }
}

I didn't test this, hope it works for you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM