How do you deal with this last CHUNK? (Bluetooth)

Hi all.
I have a long string need to split as CHUNK of 20b, the last one resulted error of: Segment: Start(11221) + length(20) -1 exceeds text length(11224), how to fix?
Thanks.

Start(11221) + length(20) -1 exceeds text length(11224) indicates an attempt to extract a chunk of the string starting at index 11221 and extending 20 bytes long. However, since the total length of the string is only 11224, trying to access beyond the end of that range resulted in an out-of-bounds error. Diff(length) is just 3 but you are trying for 20

set longString to "your very long string here"
set chunkSize to 20
set chunksList to create empty list

// Loop over the string in increments of chunkSize
for i from 0 to length of longString by chunkSize
    // Calculate the end index for the substring
    set endIndex to min(i + chunkSize, length of longString)
    
    // Extract the substring
    set chunk to substring(longString, i, endIndex)
    
    // Add the chunk to the list
    add item chunk to chunksList
end 
1 Like

Thank.
I'll test it.
I modified block like this, is this right? build it and transfer data very slow, few seconds per CHUNK.

Where is the error message from exactly lladam? Is it from App Inventor? Is it from your microcontroller?

I ask because the advantage of BLE is that you can change the MTU ( Maximum Transmission Unit) to receive larger amounts of data at once.

ProfessorCad: Tips & Tricks Bluetooth
ProfessorCad: Tips & Tricks Bluetooth Failure
ProfessorCad: Tips & Tricks Create/Validate BLE UUIDs

1 Like

thanks.
the error come from phone, the transfer was slow, added the i-loop got very slow now.
you said that the BLE can change the MTU ( Maximum Transmission Unit) to receive larger amounts of data at once. how big can be?

I doubt the for each i-loop may run through all loop and then send one CHUNK for each CHUNK?

272

That last 4 bytes is killing you, regardless of speed.

You must do the math, and stop asking for 20 bytes for the last chunk.

Here's a sample, using reusable value procedures.

image


blow_chunks.aia (3.1 KB)

Note that I used a While loop because I did not have a long running process to feed.

I set up the head and tail functions so that they could be used in Events, for single shot calls.

1 Like

Great.
Thank you.

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