Go Down

Topic: Writing to Characteristic via BLE gets stuck periodically (Read 175 times) previous topic - next topic

hurrycane90

Hi Everyone,

I'm trying to put together a simple sketch that connects to a given BLE service UUID and sends data to a specific characteristic (also given by UUID). What I've observed is that most often the ArduinoBLE client get stuck periodically or enters a bad state. This only happens on the Nano 33 BLE Sense board but it doesn't happen on the Portenta H7 board.

Here's the sketch (heavily based on the LedControl example sketch)
Code: [Select]
#include <Arduino.h>
#include <ArduinoBLE.h>

String deviceUuid = "795090c7-420d-4048-a24e-18e60180e23c";
char const * characteristicUuid = "7069e4d8-0ab2-11eb-adc1-0242ac120002";

void setup() {
  Serial.begin(115200);
  while (!Serial);

  BLE.debug(Serial);

  // initialize the BLE hardware
  if (!BLE.begin()) {
    while (1);
  }

  Serial.println("BLE Central - LED control");

  // start scanning for peripherals
  BLE.scanForUuid(deviceUuid);
}

void loop() {
  // check if a peripheral has been discovered
  BLEDevice peripheral = BLE.available();

  if (peripheral) {
    // discovered a peripheral, print out address, local name, and advertised service
    Serial.print("Found ");
    Serial.print(peripheral.address());
    Serial.print(" '");
    Serial.print(peripheral.localName());
    Serial.print("' ");
    Serial.print(peripheral.advertisedServiceUuid());
    Serial.println();

    if (peripheral.localName() != "BV5900") {
      return;
    }

    // stop scanning
    BLE.stopScan();

    controlLed(peripheral);

    // peripheral disconnected, start scanning again
    BLE.scanForUuid(deviceUuid);
  }
}

void controlLed(BLEDevice peripheral) {
  // connect to the peripheral
  Serial.println("Connecting ...");

  if (peripheral.connect()) {
    Serial.println("Connected");
  } else {
    Serial.println("Failed to connect!");
    return;
  }

  // discover peripheral attributes
  Serial.println("Discovering attributes ...");
  if (peripheral.discoverAttributes()) {
    Serial.println("Attributes discovered");
  } else {
    Serial.println("Attribute discovery failed!");
    peripheral.disconnect();
    return;
  }

  // retrieve the LED characteristic
  BLECharacteristic ledCharacteristic = peripheral.characteristic(characteristicUuid);

  if (!ledCharacteristic) {
    Serial.println("Peripheral does not have LED characteristic!");
    peripheral.disconnect();
    return;
  } else if (!ledCharacteristic.canWrite()) {
    Serial.println("Peripheral does not have a writable LED characteristic!");
    peripheral.disconnect();
    return;
  }

  if (peripheral.connected()) {
    // while the peripheral is connected
    ledCharacteristic.writeValue((byte)0x01);
    peripheral.disconnect();
  }

  Serial.println("Peripheral disconnected");
}


The way this gets stuck is the following:
Code: [Select]
16:09:34.707 -> BLE Central - LED control
16:09:34.845 -> Found 77:d3:6f:9f:21:5d 'BV5900' 795090c7-420d-4048-a24e-18e60180e23c
16:09:34.845 -> Connecting ...
16:09:35.371 -> Connected
16:09:35.371 -> Discovering attributes ...
16:09:35.475 -> Attribute discovery failed!
16:09:35.510 -> Found 55:2c:ba:72:dd:93 'BV5900' 795090c7-420d-4048-a24e-18e60180e23c
16:09:35.510 -> Connecting ...
16:09:35.792 -> Connected
16:09:35.792 -> Discovering attributes ...
16:09:35.894 -> Attribute discovery failed!
16:09:36.067 -> Found 60:a0:ea:c1:18:56 'BV5900' 795090c7-420d-4048-a24e-18e60180e23c
16:09:36.067 -> Connecting ...
16:09:36.338 -> Connected
16:09:36.338 -> Discovering attributes ...
16:09:36.439 -> Attribute discovery failed!
16:09:36.475 -> Found 77:d3:6f:9f:21:5d 'BV5900' 795090c7-420d-4048-a24e-18e60180e23c
16:09:36.510 -> Connecting ...
16:09:37.026 -> Connected
16:09:37.026 -> Discovering attributes ...
16:09:37.127 -> Attribute discovery failed!
16:09:37.195 -> Found 67:a4:ec:39:e4:40 'BV5900' 795090c7-420d-4048-a24e-18e60180e23c
16:09:37.195 -> Connecting ...
16:09:37.715 -> Connected
16:09:37.715 -> Discovering attributes ...




I've tried multiple things including different Arduino Nano 33 BLE Sense boards. You can find attached the output I get from the H7 board and the output I get while enabling the debug mode for the BLE library.


Help is much appreciated!
Thanks!
B.

Klaus_K

There are a few issues with the Arduino Nano 33 BLE. I have experimented mostly with them as peripheral. Maybe these issues are affecting you. Here is a quick description of the ones I found.

When a Arduino Nano 33 BLE works as a peripheral and a disconnect happens by bad signal e.g. getting out of range or interference. The BLE cannot recover from that.

As a peripheral, when a central is connected and the rssi is 0 and you write to a characteristic the BLE stack will stop working. If you do not write to it the connection can recover.

As a central the Nano 33 BLE cannot connect to multiple peripherals while the Nano 33 IoT with the same code can.

What do you use as a peripheral?

hurrycane90

Thanks for the answer -- definitely helps to get a perspective on what's happening here.
 
I am using an Android phone with a custom app that exposes a simple service with one characteristic. Maybe what's happening is that somewhere I don't properly disconnect and when that happens it gets into this weird state?

Thanks again!

Klaus_K

I am using an Android phone with a custom app that exposes a simple service with one characteristic. Maybe what's happening is that somewhere I don't properly disconnect and when that happens it gets into this weird state?
I tested this only with iOS apps. Killing an app does not cause the issue. Maybe the iOS stack properly disconnects in the background. Walking away however causes the issue. But your custom app could be different.

Good luck with your project. Let us know when you find a fix for your issue.

Go Up