Skip to content

Arduino BLE (micro)freezes on weak Bluetooth connection. #315

Open
@pyciko

Description

@pyciko

I noticed that my code execution starts to freeze on weak BLE connection. This is totally unacceptable in my case, since the device should just "throw data into the air" and go on to the next sensor reading cycle. I wrote a simple program to test things out:

#include <ArduinoBLE.h>

BLEService primaryService("66ec126a-bae4-4647-9f1d-8188d638b9b7");
//using pure BLECharacteristic cuz it allows control over "withResponse" argument
BLECharacteristic testChar("48297668-4f22-43c6-bcf5-d10c21388055", BLERead | BLENotify, 20, true);

unsigned long last = 0;

char buf[20];

void setup () {
  Serial.begin(9600);
  BLE.begin();

  BLE.setLocalName("Test 1");
  
  primaryService.addCharacteristic(testChar);

  BLE.addService(primaryService);
  
  BLE.setEventHandler(BLEConnected, [](BLEDevice central){BLE.stopAdvertise();});
  BLE.setEventHandler(BLEDisconnected, [](BLEDevice central){BLE.advertise();});

  BLE.advertise();
}

void loop() {
  Serial.println(millis() - last);
  last = millis();
  snprintf(buf, sizeof(buf), "%d", random(10000000, 99999999));

  BLE.poll();
  
  testChar.writeValue(buf, 20, false);
}

When Bluetooth is in advertising state, the loop execution delay is 0-1 ms. When smartphone is connected to the board, the delay is STILL 0-1 ms. But once I subscribe to the characteristic, it raises to ~50 ms (which is fine for me) with spikes of up to 800ms (which is not fine) on long distances.
It seems to me that the board is awaiting confirmations from the smartphone for every packet it sends. But wait, what? The "withResponse" argument is set to false. I tried (just out of curiosity) setting it to true - same results.

I guess there are 2 options: either I'm doing something completely wrong (or understanding something wrong), or the argument does not do anything?

Testing was conducted on original NANO 33 BLE as well as SEEED XIAO nRF52840.

Metadata

Metadata

Assignees

No one assigned

    Labels

    type: imperfectionPerceived defect in any part of project

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions