Skip to content

Adding a second characteristic stops the service to be reachable #309

Open
@MicheleMas

Description

@MicheleMas

Hello,

I am trying to create a service with two characteristics using an Arduino uno r4 wifi. With one service and one characteristic everything works fine, if I define a second characteristic the service is not available anymore (checked with nRF Connect).

Here a code to reproduce the issue:

#include <ArduinoBLE.h>

BLEService ledService = BLEService("19B10010-E8F2-537E-4F6C-D104768A1214");

// create switch characteristic and allow remote device to read and write
BLEByteCharacteristic characteristicTest1("19B10011-E8F2-537E-4F6C-D104768A1214", BLERead | BLEWrite);
BLEByteCharacteristic characteristicTest2("19B10012-E8F2-537E-4F6C-D104768A1214", BLERead | BLEWrite);

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

  if (!BLE.begin()) {
    Serial.println("starting Bluetooth® Low Energy module failed!");

    while (1);
  }

  BLE.setLocalName("TestBLE");
  BLE.setAdvertisedService(ledService);

  ledService.addCharacteristic(characteristicTest1);
  // ledService.addCharacteristic(characteristicTest2);

  // add service
  BLE.addService(ledService);

  BLE.setEventHandler(BLEConnected, blePeripheralConnectHandler);
  BLE.setEventHandler(BLEDisconnected, blePeripheralDisconnectHandler);

  characteristicTest1.setEventHandler(BLEWritten, testCharacteristicWritten);
  characteristicTest1.setValue(0);
  // characteristicTest2.setEventHandler(BLEWritten, testCharacteristicWritten);
  // characteristicTest2.setValue(0);

  BLE.advertise();
  Serial.println(("Bluetooth® device active, waiting for connections..."));
}

void loop() {
  BLE.poll();
}

void blePeripheralConnectHandler(BLEDevice central) {
  // central connected event handler
  Serial.print("Connected event, central: ");
  Serial.println(central.address());
}

void blePeripheralDisconnectHandler(BLEDevice central) {
  // central disconnected event handler
  Serial.print("Disconnected event, central: ");
  Serial.println(central.address());
}

void testCharacteristicWritten(BLEDevice central, BLECharacteristic characteristic) {
  Serial.print("Characteristic event, written: ");
  Serial.println(characteristicTest1.value());
}

As you can see I commented everything related to the second characteristic, only the definition of the variable is not commented. If I comment the line BLEByteCharacteristic characteristicTest2("19B10012-E8F2-537E-4F6C-D104768A1214", BLERead | BLEWrite); everything starts working again.

Here what I see when the second characteristic is commented:
Screenshot 2023-07-16 at 17 11 52

Just uncommenting the second characteristic this happens:
Screenshot 2023-07-16 at 17 12 29
As you can see the service and the first characteristic is not visible anymore.

I am using the ArduinoBLE version 1.3.5, with this PR to have the support for the Arduino uno r4 wifi.

Any idea of what the issue could be? Sorry in advance if this is a trivial issue given by my code, I am not an expert with C++ and BLE.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions