BLE data - buffer.

Discussion about programs, libraries and tools that work with MicroPython. Mostly these are provided by a third party.
Target audience: All users and developers of MicroPython.
Post Reply
prem111
Posts: 111
Joined: Sun Feb 23, 2020 3:18 pm

BLE data - buffer.

Post by prem111 » Mon Nov 23, 2020 8:29 am

Hi. When transmitting BLE data, sending separately one by one:

"abcdef"
"abcdef"

Sometimes they go to the buffer in this form: b'abcdefabcdef '

Should I use some sort of separator / delimiter? If so, what exactly?

I was thinking about "\n" or "#" but in the future I want to transfer files so I don't know if that's a good solution. Thanks for tips.

User avatar
jimmo
Posts: 2236
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

Re: BLE data - buffer.

Post by jimmo » Mon Nov 23, 2020 11:08 am

prem111 wrote:
Mon Nov 23, 2020 8:29 am
Should I use some sort of separator / delimiter? If so, what exactly?
I'm assuming you're using the buffer mode on the characteristic. (i.e. append=True on https://docs.micropython.org/en/latest/ ... set_buffer)

What you're describing is a fundamental thing in the design of any protocol. The two most common options are to either length-prefix your data (i.e. some number of bytes containing an integer with the length, followed by that many bytes of data), or like you say, to use a delimiter.

Or you can use both together.

prem111
Posts: 111
Joined: Sun Feb 23, 2020 3:18 pm

Re: BLE data - buffer.

Post by prem111 » Mon Nov 23, 2020 2:06 pm

Yes apeend is set to True, but there is one more thing. When I have the buffer set to len = 100, I will send a packet which has e.g. 80 bytes, then it will send another 80 bytes packet, I receive that 80 bytes will come, 20 from the next packet is added/append, and the rest of the second, i.e. 60, does not arrive .. .. So it looks like when my buffer is set to 100, I have to send 100 bytes too, right?

User avatar
jimmo
Posts: 2236
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

Re: BLE data - buffer.

Post by jimmo » Mon Nov 23, 2020 11:42 pm

prem111 wrote:
Mon Nov 23, 2020 2:06 pm
Yes apeend is set to True, but there is one more thing. When I have the buffer set to len = 100, I will send a packet which has e.g. 80 bytes, then it will send another 80 bytes packet, I receive that 80 bytes will come, 20 from the next packet is added/append, and the rest of the second, i.e. 60, does not arrive .. .. So it looks like when my buffer is set to 100, I have to send 100 bytes too, right?
If the buffer is set to len=100, then two 80 byte payloads arrive, then the last 60 bytes of the second payload will be lost.

You can send as many bytes as you like, but you need to rate limit how quickly you're sending (and/or read the data faster out of the buffer using gatts_read).

You might also be interested in the L2CAP channel support which was just merged upstream last night. (But unfortunately only for STM32 for now, ESP32 support might come later)

Post Reply