How to convert received ASCII data to a value

Dear @Thenk_Hou,
welcome to the community !
Regarding your issue: what about using the "print" instead of "write" in your microbit code ?
Is this changing the way the data are received ? Remember also to send your last data by using a "println" to correctly terminate the sending.
Another possible solution is to make a dictionary (here in my example stopped at 2 but has to be fiilled 'tll value 9, obviously):
image

With this approach any single digit received by the BT has to be stored into an element of a list, then, at the end of the data stream receiving, the final data value has to be composed with something like:

This is just a rough idea. Maybe browsing the forum you'll find something easier.
Anyway, I hope it can help !

EDIT: Another solution, instead of using a dictionary is that the ASCII character is always the decimal value + 48 (the ASCII representation for the value 0) therefore a block like:
image
converts any single digit. The final value shall be composed like before, anyway.

AI2 automatically converts text to numbers in math blocks if needed, within reason.

Use the Receive Text block and println.

Please see the Delimiter article in FAQ

Be sure to use println() at the end of each message to send from the sending device, to signal end of message. Do not rely on timing for this, which is unreliable.

In the AI2 Designer, set the Delimiter attribute of the BlueTooth Client component to 10 to recognize the End of Line character.
BlueToothClient1_Properties
Also, return data is not immediately available after sending a request,
you have to start a Clock Timer repeating and watch for its arrival in the Clock Timer event. The repeat rate of the Clock Timer should be faster than the transmission rate in the sending device, to not flood the AI2 buffers.

In your Clock Timer, you should check

  Is the BlueTooth Client still Connected?
  Is Bytes Available > 0?
     IF Bytes Available > 0 THEN
       set message var  to BT.ReceiveText(-1) 

This takes advantage of a special case in the ReceiveText block:

ReceiveText(numberOfBytes)
Receive text from the connected Bluetooth device. If numberOfBytes is less than 0, read until a delimiter byte value is received.

If you are sending multiple data values per message separated by | or comma, have your message split into a local or global variable for inspection before trying to select list items from it. Test if (length of list(split list result) >= expected list length) before doing any select list item operations, to avoid taking a long walk on a short pier. This bulletproofing is necessary in case your sending device sneaks in some commentary messages with the data values.

In c++, the rule is sometimes used:

a = 50;
b = 49;
c = 51;
a = a-48;
b = b-48;
c = c-48;
x = 100*a + 10*b + c;

But in AppInventor you can receive text instead of bytes.

Hi uskiara, thanks for your advice. May i know if you have any idea how to get rid of the "[" and "]" as well? I have tried to use the 'split text..at..' block with a separator ",". but the "[" and "]" are still there..

Hi ABG, thanks for your suggestion. I am using microbit block coding, they have a pre-written block 'bluetooth uart service' and can't seem to find print command.. :sweat_smile: :sweat_smile: I am sorry that i am not a coding-savvy guy, still much dependent on block coding

You can also read this topic:

i try to use the block 'segment text..start...length' but get into error also..
[50,49,51] => segment text getLDR, start 2, length 8 doesn't seem to be the correct approach. Please advise. Thank You.

Thank you Patryk_F, I'll have a look.

This way you can convert numbers and text.

test_tool.aia (9.7 KB)

Dear @Thenk_Hou, as far as your "[" and "]" question, you can do like this:
image

in other words, you can replace whatever character with a null character (not a blank/space, but an empty/null one). Then you can split the remaining at commas.
Of course the suggestions from @Patryk_F or @ABG are good as well !!! You can try and choose whichever ir simpler (the simplest the best, always :grinning:)

Another algorithm...

If you write [50,49,51,10] and do a Receive Text in AI2, there is a good chance you will receive your 3 digit number as a piece of text, with no extra markup ("",)

The extra 10 I added at the end if a Line Feed, that can be used like in my Delimiter article to catch a whole message in AI2.

Hi Juan, thanks for your advice. I have tried ur code but so strange that I am getting back some funny output. :joy:

if i set the label1.text to global ldr, the output value is "50,49,51";
if i set the label1.text to global decimal, the output value is back to [50,49,51] and
if i set it to global ldrList, the value is [[50,49,51]].. seems like the code will add back the "[" and "]" automatically..

Please englighten. Thanks in advance.

Try

borrar_decimal.aia (2.7 KB)

Let's have a contest who has the simplest solution. This is mine:
Add a Webviewer to your design. It does not have to be visible, therefore you do not have to see it.
Now, add this block:


The string in the join is actually this:
window.AppInventor.setWebViewString(String.fromCharCode(

So, instead of waiting for BytesReceived, you wait for WebViewStringChange.
Also add this block:


Here I add the value to a label containing previously received text. You may want to do something different.
The trick is that JavaScript knows how to convert ASCII and AppInventor does not. We have asked for this alreay to be implemented. Keep your fingers crossed.

Great Juan! I have found out I pick some wrong block thus resulting in error. The code works! Tq very much!

Hi Ghica, I'll try ur code out after this to see if it does simplify the process. May I know if the java script actually already built in? Cos i dun see any website address in ur code..

Thanks for your help. Appreciate it.

Hi ABG, microbit have their own Bluetooth extension which I m using. Therefore I can't apply any trick u share on the ClientBluetooth1 block..:sweat_smile::sweat_smile:

Still thanks to ur offer of help. :pray: