0

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?

bboydflo
  • 907
  • 1
  • 15
  • 24
  • 1
    Check this(Read/convert an InputStream to a String) : http://stackoverflow.com/questions/309424/read-convert-an-inputstream-to-a-string – rai.skumar Dec 04 '12 at 11:06

2 Answers2

1

Try this

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

msg = br.readLine();

This will solve problem

Mohammod Hossain
  • 4,134
  • 2
  • 26
  • 37
  • hey Mohammod, it is exactly what I needed. The only thing I had to change was in my onSensorChanged method, i had to add the following in the end of it: mChatService.writeFromString("\n"); this way i could read each line properly. otherwise the input stream was a huge big line of vals strings. thank you alot. It is working as expected – bboydflo Dec 04 '12 at 11:59
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
    }
}

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

Igwe Kalu
  • 14,286
  • 2
  • 29
  • 39
  • hey IGwe, it is really easy to understand but I am actually trying to get a String and do some job with it. I checked the scanner methods and the hasNextString is missing. any suggestion ? thanks for the reply – bboydflo Dec 04 '12 at 11:49