BluetoothSerial locks up after receiving 512 bytes

Gilbert
Posts: 39
Joined: Wed Sep 16, 2020 2:58 pm

BluetoothSerial locks up after receiving 512 bytes

Postby Gilbert » Tue Jul 13, 2021 12:53 pm

I'm not sure where to ask for h elp: Arduino or ESP forum. So I post this on both.

I'm using ESP32 and bluetooth to communicate with my Android phone. The program on the phone is created using MIT App Inventor.
When I'm sending long strings (containing JSON) fom ESP32 to the phone, all goes well, regardless how long the string is. When I'm sending long strings from the phone to ESP32, the Bluetooth Serial reception locks up after receiving 512 bytes.
Whenever the ESP_SPP_DATA_IND_EVT event occurs, I'm reading the entire input buffer using

Code: Select all

while (SerialBT.available()) { // do this until all chars have been consumed
c = SerialBT.read();
if(btIncomingIndex < (SIZE_BT_INCOMING - 2)) { // if char buffer not full (leave room for terminating '\0')
btIncomingChars[btIncomingIndex++] = c; // store the char and increment index
... more code ...
}
My BT code contains a supervision timer to detect that BT has stopped receiving data for at least 5 seconds before the message is complete. Message completion is detected by counting the opening and closing curly brackets (since all messages are JSON, the message is complete when the count for opening and closing curly brackets is matching). Everything is fine for short messages, but when the message length exceeds 512 bytes BT locks up and my supervision timer fires.
Is this a bug or do I need to take extra steps to receive very long messages?

PS: I'm using the SPP profile on BT Classic

Gilbert
Posts: 39
Joined: Wed Sep 16, 2020 2:58 pm

Re: BluetoothSerial locks up after receiving 512 bytes

Postby Gilbert » Tue Jul 13, 2021 3:56 pm

Problem solved by member https://forum.arduino.cc/u/cattledog on the Arduino forum.
The RX queue size is 512 bytes (defined in BluetoothSerial.cpp). I increased the size to 2048 and now all my messages pass.

I'm not a BT expert, but I would expect that the BT sink signals to the BT source to suspend transmission when the RX buffer gets full and to resume sending when the buffer has been read and thus freed for reception of more data. But that doesn't seem to happen. IMHO this is a design flaw in BluetoothSerial.

Who is online

Users browsing this forum: Baidu [Spider] and 151 guests