0

I'm building android app that receives an mqtt message from esp8266 with the percent of water on water sensor for my school project. So, I need to send app notification, when the percent is not equal to zero, but there's one problem. Received message is a byte array, but for some reason it's not just number, it has specific U+FFFD � replacement character, so I can't compare this byte array to another array like [0]. I'm trying to convert received message into string, remove all strange symbols and compare it to "0". But for some reason again I can't. For example, String "0����" clears from all the � characters, but the java code still considers that it is not equal to "0".

My code:

if (topic.equals(topicstr2)){
                    byte[] res = message.getPayload();
                    result_notif = new String(res).replaceAll("�", "");
                    if (result_notif != "0"){
                        sendNotif();
                    }
                }

result_notif is a string, it was declared in the beggining of code.

P.S. sorry for my bad english, I hope I've made my problem pretty clear.

softer
  • 1
  • 1
  • Try `!result_notif.equals("0")` to compare the string to zero. Have a look at [this post](https://stackoverflow.com/a/513839/7947994). – JANO Feb 06 '22 at 11:53
  • @JANO no, with !result_notif.equals("0") it still considers that 0 procent is not "0":( – softer Feb 06 '22 at 11:59
  • Ok, what is the output if you print out the variable `result_notif`? – JANO Feb 06 '22 at 12:01
  • @JANO just "0" without any empty spaces, my notification includes that "result_notif" string, I use .setContentText(result_notif + "%!") and it works fine, printing "0%!" – softer Feb 06 '22 at 12:09
  • Does specifying the charset make a difference like this: `result_notif = new String(res, StandardCharsets.UTF_8);` – JANO Feb 06 '22 at 12:21
  • @JANO It doesn't make any difference – softer Feb 06 '22 at 12:38
  • Ok, can you print the length `System.out.println(result_notif.length());` and also the ASCII value of the zero character: `System.out.println((int) result_notif.charAt(0));`? – JANO Feb 06 '22 at 12:51
  • @JANO omg, THANK YOU. The string length was surprisingly equal to 6! I used the .trim() method and everything worked well! Thanks for quick response!!! I've been fighting with this code for 3 hours. – softer Feb 06 '22 at 13:05

1 Answers1

0

Using !result_notif.equals("0") instead of "result_info != "0"" and using the .trim() method was the solution of my problem.

Code:

if (topic.equals(topicstr2)){
    byte[] res = message.getPayload();
    result_notif = new String(res).replaceAll("�", "").trim();
    intent.putExtra(MainActivity.PARAM_RESULT, result_notif);
    sendBroadcast(intent);
    if (!result_notif.equals("0")){
        sendNotif();
    }
}
rikyeah
  • 1,896
  • 4
  • 11
  • 21
softer
  • 1
  • 1
  • That may work for the moment but it is a bad hack. You should decode the content of the byte array in the right way. But... you did not tell what exactly this array contains.. So we cannot give help. – blackapps Feb 06 '22 at 14:59
  • @blackapps the byte array contains one number from 0 to 100 without any "%" characters. – softer Feb 06 '22 at 15:28
  • That tells us not much. Would 99 be two bytes or one? Both is possible. It could be four bytes too if it was sent as an integer. You should tell us exactly how many bytes and the content of every byte. You could give examples. – blackapps Feb 06 '22 at 15:57
  • @blackapps ok, I checked it. After the first connection to mqtt even if the percent is "0" array contains 10 bytes, then numbers from 0 to 9 - 1 byte and from 10 to 99 - 2 bytes. The percent was sent as a string. – softer Feb 06 '22 at 16:58