[BLE Arduino Nano 33 BLE] Performance issue

Hello,

I have a problem with BLE communication with Nano33 over BLE.
The issue is that each time I connect to Nano performance drops, let me explain with fallowing short steps:

  1. plug-in Nano 33 into USB
  2. using my own application connect to device and start reading characteristics, (on Nano now I just increment int variable using string characteristics and send it)
  3. read changed characteristic on PC using application
  4. close application (this will automatically disconnect from BLE on Nano)
  5. repeat from 2

Issue:
I do have performance monitoring functionality written, with basically count amount of reply received from BLE within 1 sec, and here are results:

  • on first connection to Nano I do receive around 51 messages / sec
  • on second connection to Nano I do receive around 32-33 messages / sec
  • on third connection to Nano I do receive around 16-17 messages / sec
  • on forth connection I'm unable to connect as Nano is hung - led indicate that connection is still present

My questions:

  1. obviously why this happens ?
  2. why maximum only 51 updates ? This is only for sending string characteristics so I would assume it should be much faster (note code below should output around 100 messages, I tested without delay of course that hung loop() so did delay and below 10ms it will hang faster).

Could be that I did missed something obvious so I'm grateful for any advice.

Code for Nano:

#include <ArduinoBLE.h>
static const char* localName = "Nano_33_BLE";
BLEService bleService("28a72d52-35ca-4c57-8b21-b831066983a8");  // User defined service
BLEStringCharacteristic bleCharacteristic("c7f50871-a316-4926-b58a-26e7b58b78e4", BLERead | BLENotify, 128);

int enumerator = 0;

void setup() {
  pinMode(LED_BUILTIN, OUTPUT); // initialize the built-in LED pin

  if (!BLE.begin()) {   // initialize BLE
    Serial.println("starting BLE failed!");
    while (1);
  }

  BLE.setLocalName(localName);  // Set name for connection
  BLE.setAdvertisedService(bleService); // Advertise service
  bleService.addCharacteristic(bleCharacteristic); // Add characteristic to service
  BLE.addService(bleService); // Add service

  bleCharacteristic.setValue("0"); // Set greeting string
  BLE.advertise();  // Start advertising
}

long previousMillis = 0;

void loop()
{
  BLEDevice central = BLE.central();
  if (central)
  {
    digitalWrite(LED_BUILTIN, HIGH);
    while (central.connected())
    {
//      BLE.poll(); // <- doesn't change anything
      long currentMillis = millis();
      if (currentMillis - previousMillis >= 10) {
        previousMillis = currentMillis;
        bleCharacteristic.writeValue(String(enumerator));
        enumerator++;
      }
    }
    digitalWrite(LED_BUILTIN, LOW);
    enumerator = 0;
  }
}

Thanks for reply
Best regards

Welcome to the forum

I would try to read the data with a smartphone app e.g. BLE Scanner on iOS to confirm whether the issue comes from the Arduino or the PC application.

Why are you using a String characteristic instead of a BLEIntCharacteristic?

You are defining a 128byte characteristic. A BLE data packet is limited to ~27bytes.

Note, millis() returns unsigned long. I have a hunch that casting it to long is not a good idea.

Klaus_K:
You are defining a 128byte characteristic. A BLE data packet is limited to ~27bytes.

Not necessarily. The MTU for BLE packets is negotiated when connecting. IIRC, the minimum value is 23 bytes, but it can be much larger. iOS supports up to 185 bytes, and the ESP32 BLE stack up to 517 bytes. I'm not familiar with the ArduinoBLE library for the Nano 33 BLE, there's a good chance that it uses 23 bytes by default.
The underlying layers of the Bluetooth stack may well use smaller packets, but that should be transparent to you when using BLE.

If your MTU is set to the default of 23 bytes, reading and writing 128 bytes each time could indeed take a long time, depending on the connection interval you're using.

Wireshark is an invaluable tool for debugging Bluetooth problems (I don't know if it's supported on Windows, but it works great on Linux).

Pieter

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