I check that the appropriate text is actually stored in the text member of the label. It is. But the text is not shown onscreen. The label is visible but it is blank and does not show the text "Connecting ...". I assume it some interaction with the Bluetooth action or some other complexity with the order of completion of execution of the different statements. Any ideas are really appreciated. The texts "Connected" and "Connection error" are shown as expected.
Maybe a thread problem then.
What you could try is adding a clock, initially disabled.
Instead of calling BTConnect, you enable the Clock.
In the clock timer event you do the same test and if true call BTConnect.
When connected, you should disable the clock again.
This has as effect that the thread of the button-click is finished and another thread for the clock waits for a little while.
If that does not help, I promise to look at your video.
Using a Delimiter Byte value of 13 is unusual. Usually people use 10 for New Line recognition in conjunction with println() to complete a sent message.
Standard delimiter advice:
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.
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.
Ghica, this works perfectly. Thank you very much for the idea. This mechanism provided a work-around for the problem but does not fundamentally solve it. Looks like a threading problem. This is the broad problem but understanding what gives rise to this thread problem is not as easy. My use of timers is very sparingly (in fact I only use one, except for a 2nd one you suggested) and I cannot see any obvious cause of a race condition. Unfortunately my relatively scanty knowledge of AI2 does not allow me to even think about investigating the fundamental cause of this problem. Any ideas from your side??
Thank you for the advice about delimiters. The present system arose because of the idiosyncrasies of the microcontroller that talks to the Android app. I will have a look at the delimiter implementation.