Question

I receive data from Arduino, but the data appears as "String", when I want to compare the data as "int".

Example: On the ListView from my app, the readMessage , I see 10, on my app. <==== Not sure

I want this behaviour:

if readMessage >0 and <=100
    My app sends the message "Hello"

if readMessage >=100 and <=200
    My app sends a message "Try again"

I used "equals", but it's only for a status, and my app changes in real time, so it is not working for me.

case MESSAGE_READ:
    byte[] readBuf = (byte[]) msg.obj;
    // construct a string from the valid bytes in the buffer
    String readMessage = new String(readBuf, 0, msg.arg1);

    mConversationArrayAdapter.add("Nivel de alcohol"+":  " + readMessage);

    if (readMessage.equals("100")) {
        mensaje.setText("1.0 g/l en aliento, Aliento alcohólico, disminución mínima de los sentidos");
    }
    else if (readMessage.equals("300")) {
        mensaje.setText("2.4 g/l en aliento, Ebriedad incompleta, disminución significativa de los sentidos.Se recomienda interrumpir cualquier actividad física de riesgo.");
    }
    else if (readMessage.equals("400")) {
        mensaje.setText("3.8 g/l en aliento, Ebriedad completa en la mayoría de los estados de la República Mexicana");
    }
    else if (readMessage.equals("500")) {
        mensaje.setText("4.8 g/l en aliento, Limite BAC más alto aceptado en pocos países");
    }
    else if (readMessage.equals("600")) {
        mensaje.setText("15.0 g/l en aliento,A este nivel la mayoría de las personas pierden la conciencia");
    }

    break;
Was it helpful?

Solution

The best thing to do here is to try to convert readMessage to an integer, and then compare that integer to your different numbers. The parseInt() method tries to do the conversion, throwing an exception if it fails.

case MESSAGE_READ:
    byte[] readBuf = (byte[]) msg.obj;
    // construct a string from the valid bytes in the buffer
    String readMessage = new String(readBuf, 0, msg.arg1);

    mConversationArrayAdapter.add("Nivel de alcohol"+":  " + readMessage);

    try {
        // Convert the string to an integer. It may fail (see the catch block).
        int messageNumber = Integer.parseInt(readMessage);

        if (messageNumber == 100) {
            mensaje.setText("1.0 g/l en aliento, Aliento alcohólico, disminución mínima de los sentidos");
        }
        else if (messageNumber == 300) {
            mensaje.setText("2.4 g/l en aliento, Ebriedad incompleta, disminución significativa de los sentidos.Se recomienda interrumpir cualquier actividad física de riesgo.");
        }
        else if (messageNumber == 400) {
            mensaje.setText("3.8 g/l en aliento, Ebriedad completa en la mayoría de los estados de la República Mexicana");
        }
        else if (messageNumber == 500) {
            mensaje.setText("4.8 g/l en aliento, Limite BAC más alto aceptado en pocos países");
        }
        else if (messageNumber == 600) {
            mensaje.setText("15.0 g/l en aliento,A este nivel la mayoría de las personas pierden la conciencia");
        }
    }
    catch (NumberFormatException)
    {
        // If you got here, then readMessage was not a valid integer. You should add some error handling here.
    }
    break;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top