How to convert floating-point numbers received from BLE?

Hey,

I am trying to receive five floating-point numbers sent from Raspberry Pi through BLE, each corresponding to 4 bytes, so my App inventor can receive 20 bytes of data at once. But the problem is that because the number of floating-point numbers transmitted is greater than 1, I cannot directly display them by calling the BLE. floatreceived method. As I need to draw these floating-point numbers in the chart, I also do not want them to exist in the form of Strings. I want them to exist as floats in the app inventor because I want to display these five floating-point numbers as floats


For example, the five floating-point numbers I want to transfer are like 12.34, 56.78... and I try to read them one by one. 12.34 was transmitted as A4704541. After reading the first floating-point number using this method, it can correctly display 414570A4, which is the IEEE754 format used for translation of this floating-point number. However, I encountered a question: how can I translate it into a floating-point number?
borrar_float_ver_2.aia (2.0 KB)
I have tried calling the method I saw in another post, but it is displayed as a string. I need to finally draw these values in the chart, so what I need is the float form. This problem has been bothering me for many days, and I sincerely hope that someone can help me.

Why complicate your life. For App Inventor it doesn't matter whether the number is a string or not... it can be easily converted if necessary. You can also get rid of the decimal point by multiplying the values ​​by 100, and then dividing them back in the Inventor app.

1 Like

Thank you Patryk. Sure, sometimes when an idea is wrong at the beginning, it enters a dead cycle and cannot come out... The second method you mentioned is indeed very effective, and I think I can give it a try. But I still don't quite understand the meaning of the sentence you mentioed "whether the number is a string or not doesn't matter", because the data I passed contains the letters a-f. Therefore, I think if we calculate in the form of String instead of hex, there may be problems. Can you explain your meaning in detail to me?

Data sent and received via Bluetooth is always in the binary system. App inventor automatically converts them to decimal. You can achieve the hexadecimal system using conversion blocks. Conversion blocks will work with both numbers, e.g. 55, and strings "55".

You can do a string conversion on the Raspberry side to send "1234". On the app inventor side you get the string "1234", divide it by 100 and you get the floating point value 12.34. You can also send the number 1234 as an integer.

Thank you Patryk. I know what you mean.

It seems like it would be more helpful to use RegisterForFloats and FloatsReceived. You should still be able to pack 5 floats into a packet (20 bytes total for 4-byte floats). The extension will read as many floats as it can from the packet and report them as a list of numbers via the floatValues parameter of the FloatsReceived event.

1 Like

Thank you very much, ewpatton! I used to think that this method could only transfer one float number, so I've been trying other methods, which has made my blocks very complex. It turns out that I can really transfer five floating numbers at once, which is great for me. Thank you!

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.