Hello,
I am attempting to calculate the Binary Error Rate (BER) of the Bluetooth Low Energy (BLE) protocol. When I send 20KB of data in chunks of 20 bytes, all data is received correctly (BER == 0). However, when I try to send data sizes between 21KB and 22KB (e.g., 21KB, 21.5KB), the central device fails to connect to my board, and the Arduino blocks. Interestingly, when I send 25KB, everything works fine again.
#include <ArduinoBLE.h>
#define LENGTH 21000 // Adjust LENGTH for testing
const char * deviceServiceUuid = "1523";
const char * deviceServiceRequestCharacteristicUuid = "1526";
const char * deviceServiceResponseCharacteristicUuid = "1527";
BLEService ArduinoMLXService(deviceServiceUuid);
BLECharacteristic ArduinoMLXRequestCharacteristic(deviceServiceRequestCharacteristicUuid, BLEWrite, 4);
BLECharacteristic ArduinoMLXResponseCharacteristic(deviceServiceResponseCharacteristicUuid, BLENotify, 20);
uint8_t *testSequence;
void setup() {
Serial.begin(9600);
while (!Serial);
Serial.println("Starting BLE setup...");
testSequence = (uint8_t*)malloc(LENGTH * sizeof(uint8_t));
if (!testSequence) {
Serial.println("Memory allocation failed!");
while (1);
}
for(int i = 0; i < LENGTH; i++) {
testSequence[i] = i % 256;
}
BLE.setDeviceName("ArduinoMLX");
BLE.setLocalName("ArduinoMLX");
if (!BLE.begin()) {
Serial.println(F("Starting BLE module failed!"));
while (1);
}
BLE.setAdvertisedService(ArduinoMLXService);
ArduinoMLXService.addCharacteristic(ArduinoMLXRequestCharacteristic);
ArduinoMLXService.addCharacteristic(ArduinoMLXResponseCharacteristic);
BLE.addService(ArduinoMLXService);
BLE.advertise();
Serial.println("BLE setup done.");
}
void loop() {
BLEDevice central = BLE.central();
Serial.println("searching..");
if (central) {
Serial.println("CENTRAL..");
Serial.print("Connected to ");
Serial.println(central.address());
while (central.connected()) {
BLE.poll();
sendLargeDataOverBLE(LENGTH, testSequence);
delay(3000);
}
Serial.print("Disconnected from ");
Serial.println(central.address());
}
}
void sendLargeDataOverBLE(size_t size, uint8_t *data_to_send) {
for (int i = 0; i < size; i += 20) {
uint8_t chunk[20];
int chunkSize = min(20, size - i);
memcpy(chunk, data_to_send + i, chunkSize);
ArduinoMLXResponseCharacteristic.writeValue(chunk, chunkSize);
// Delay is commented out, uncomment if needed for stability
// delay(10);
}
Serial.println("All data is sent");
}