Weird behaviour for global variable from bluetooth reception

Hello experts!! I am facing a weird problem with a global variable content... I have used a button where when i press it , i send to a microcontroller a value that has been stored previously to another variable. After that the microcontroller responds with "Reg_OK" if the operation was successfull.
Doing some tests i tried different scenarios and i realized that when i tried to re-run the particular code, although the microcontroller sends "Reg_OK" then the code executes the else block. I also clear the variable before every reception but only the first time is working as expected....

Any thoughts ??? Regards Panagiotis

P.s I also noticed that if the bluetoothClient hasn't received the 6 bytes length text, then the program freezes although i have made a test using a clock timer to abort this operation, if the microcontroller fails to respond..

Your code looks to be suffering from impatience.

It is expecting immediate responses from BlueTooth.

Most BlueTooth projects poll with a Clock Timer to see if input is available before asking for the input from BlueTooth.

Please post your microcontroller code here, to help for more specific advice.

Make a label and display the data received by the applications in it. Then you will see if the received message is really what you expect.

1 Like

Dear Panagiotis,
two things: AI2 is not behaving like an embedded system: no clock fires until the running thread is finished. This means that the clock that you tried to put to interrupt the 6 bytes reception block won't fire until the 6ht byte is received. No matter.
Second : follow the hint given by Patrick because it is the best thing that you can do to trace what is really arriving to your app.
Supposing that after the 6 bytes of the first sending ("Reg_OK") your BT sender also sends a CRLF characters pair. This will change the count of bytes. from the second message onward you will receive "CRLFReg_" (which is 6 bytes: "CR = 0x0D LF = 0x0A Reg_").
Try instead of using the Receive Byte the Receive Text using as a terminator the LF (0x0A) character.
Good luck.
Ugo.

Firstly i would like to thank you all for your answers!!! Very nice tips from all !!! i used a label as a debugger to monitor the buffer which helped a lot... i also used a clock for the bluetooth reception... and I did the following where right now it works..... (please let me know if my approach is good)

But i have one more question.... On the block below what is execution order?
I mean what happens first and what next....?? Where can i read for priorities?

Untitled2

THANK YOU all regards Panagiotis

A better way would be to use "serial.println (Reg_OK);" in your arduino code and in the code appinventor "receiveText.numerOfBytes = -1". Set "DelimiterByte" in bluetooth component settings to "10".

Hello can you please explain it to me in more detail?
Why to put -1 ?
And also what does the delimeterbyte setting in bluetooth component? i cannot understand the explanation of mit resources...

DelimiterByte

  • Returns the delimiter byte to use when passing a negative number for the numberOfBytes parameter when calling ReceiveText, ReceiveSignedBytes, or ReceiveUnsignedBytes.*

Regards