1

I have an test application that connects with a bluetooth and receives and sends data. Everything is ok here.

My problem is that I need to handle the return before the set TextView.
If I received "OK," I need to return "Turn On".
If I received "OFF", I need to return "Turn Off".

I am not able to manipulate the data. Some tips here?

My Code:

void appendLog(){
    bluetoothIn = new Handler() {
        public void handleMessage(Message msg) {

            if (msg.what == handlerState) {
                String readMessage = (String) msg.obj;

                recDataString.append(readMessage);
                int endOfLineIndex = recDataString.indexOf("\r\n");

                while ((endOfLineIndex= recDataString.indexOf("\r\n")) > 0){
                    String dataInPrint = recDataString.substring(0, endOfLineIndex);

                    txtString.setText(dataInPrint);

                    Log.d("BT", "Received: " + dataInPrint);
                    String aux = dataInPrint.toString();

                    // If i received "OK" I need return in TV "Turn on"
                    if(aux == "OK"){
                        Log.d("BT", "OK");
                        //txtString.setText("Turn on");
                    }else{
                        Log.d("BT, "NOK");
                        //txtString.setText("Turn off");
                    }

                    recDataString.delete(0, recDataString.length());
                }

            }
        }
    };

}

Android Monitor:

07-08 21:06:19.000 29972-29972/com.test D/BT: Received: OK
07-08 21:06:19.000 29972-29972/com.test D/BT: NOK

Tks for all and sorry for my english ;)

fperz
  • 13
  • 3
  • How exactly do you need to handle and what ? Only problem I see is `Log.d("BT, "NOK");`. Change it to `Log.d("BT", "NOK");` – ebyt Jul 09 '16 at 00:29
  • 1
    @ebyt I fixed this =). I forgot to edit in post, sorry! When it the IF validates he is not entering the correct answer. Note that get "OK" and should drop the "OK" and not the "NOK". – fperz Jul 09 '16 at 00:37

1 Answers1

1

Change your if statement to compare strings correctly.

if(aux.equals("OK")){
    Log.d("BT", "OK");
    //txtString.setText("Turn on");
}else{
    Log.d("BT", "NOK");
    //txtString.setText("Turn off");
}

See this SO POST How do I compare strings in Java?

Community
  • 1
  • 1
ebyt
  • 312
  • 1
  • 9