Bluetooth connection and app lagging constantly


So, the code basically makes my esp32 send constantly some numbers to the app so a image may or may not appear. but, the block that makes this happen makes the app lag so much after I connect the esp32 to the app... I made something wrong? (the clock check is setted for every 10ms)

In the Clock Timer, you have too loose an availability test.

You are checking BytesAvailable >= 0.

Only let the Receive proceed if BytesAvailable > 0.

P.S. Also post your ESP code, so we can verify that it's not sending faster than the app can receive.

Yeah I noticed that mistake later, but it haven't changed that much.... Still lagging as hell as soon as I connect with the esp32 on my phone.
also, here is the code
image

You are sending text via println.

That's okay as long as you receive text.

Here is a simple BlueTooth text receiver sample, for single value per line:
blocks
initialize global message to


Be sure to use println() at the end of each message to send from the sending device, to signal end of message.

Only use print() in the middle of a message.

Be sure not to println() in the middle of a message, or you will break it into two short messages and mess up the item count after you split the message in AI2.

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.

Some people send temperature and humidity in separate messages with distinctive prefixes like "t:" (for temperature) and "h:" (for humidity).
(That's YAML format.)

The AI2 Charts component can recognize these and graph them. See Bluetooth Client Polling Rate - #12 by ABG

I see... However, I only need to make an image avaible when receiving specific numbers from the esp, in the case of the example, the number 001, and now I have a little doubt about how to do this without cooking off my AI... Is there any already made project with similar functionality? (I'm literal new to this thing, I started learning about it 2 days ago lol)

(Canned Reply: ABG- Export & Upload .aia)
Export your .aia file and upload it here.

export_and_upload_aia

.

See http://www.appinventor.org/bookChapters/chapter18.pdf of the free book at
App Inventor 2 Book: Create Your Own Android Apps

Job_G (1).aia (3.8 KB)
the other blocks are working as expected, only bytes receiver are cooking my head off....
And also, I changed the esp code, serialbt.write seems to work better sending bytes instead of text

If you are still having a problem, post both the updated ESP and AI2 code together.

Mismatches between them cause problems.
(Closely examine my ABG icon for an example.)

yup started lagging and even crashed this time. Just triplicated the conditions and the the code. And instead of images, I've placed to change the label text



here are the arduino sketch and the AI program if it's better
bluetoothtest.aia (4.1 KB)
bluetooth.ino (498 Bytes)

(just noticed that the values for changing the text are all the same, just changed it, but it is still lagging)

So now your ESP values are floats (4 bytes each, if i remember right), and you are sending them using the ESP write() command.

How many bytes do you expect to arrive to AI2 for each float you send?
How does that compare to how many bytes you are requesting?

If you are going to use write() to send data over BlueTooth, you had better be familiar with the internals of the various data types, with their different byte lengths.

I prefer the simplicity of the print and println commands, which send text, and can be used with delimiters like \n and comma.