ArduinoBLE icon indicating copy to clipboard operation
ArduinoBLE copied to clipboard

peripheral.characteristic() doesn't find specific characteristic

Open pitdagosti opened this issue 3 years ago • 0 comments

Steps to reproduce the behavior:

OS: Windows Version: 21H1 (build SO 19043.1320)

  • Open 'Arduino IDE 2.0.0-beta.12'
  • Connect the Arduino Portenta Machine Control
  • Connect Nicla Sense ME
  • Upload the code below on the PMC and the example LED of the ArduinoBLE library on the Nicla Sense ME
  • Run sketches
  • See the output on the PMC

The characteristich and the service of the example LED are both 19b10000-e8f2-537e-4f6c-d104768a1214.

Actually the LED example doesn't work on Nicla Sense ME, so I made some modifications edited in the code below

Expected behavior

14:22:45.394 -> Connected
14:22:45.395 -> Discovering attributes ...
14:22:45.600 -> Attributes discovered
14:22:45.600 -> Peripheral has mesh service
14:22:45.600 -> 4 characteristic discovered
14:22:45.600 -> Discovering characteristic attributes ...
14:22:45.600 -> Peripheral has battery level characteristic

Actual behavior

14:22:45.394 -> Connected
14:22:45.395 -> Discovering attributes ...
14:22:45.600 -> Attributes discovered
14:22:45.600 -> Peripheral has mesh service
14:22:45.600 -> 4 characteristic discovered
14:22:45.600 -> Discovering characteristic attributes ...
14:22:45.600 -> No led characteristic

CODE FOR PMC (CENTRAL)

#include <ArduinoBLE.h>

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

  // initialize the BLE hardware
  BLE.begin();

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

  // start scanning for peripherals
  BLE.scanForUuid("19b10000-e8f2-537e-4f6c-d104768a1214");
}

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() != "LED") {
      return;
    }

    // stop scanning
    BLE.stopScan();

    controlLed(peripheral);

    // peripheral disconnected, start scanning again
    BLE.scanForUuid("19b10000-e8f2-537e-4f6c-d104768a1214");
  }
}

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;
  }

  BLEService meshService;

  if (peripheral.hasService("19b10000-e8f2-537e-4f6c-d104768a1214")) {
    Serial.println("Peripheral has mesh service");
    meshService = peripheral.service("19b10000-e8f2-537e-4f6c-d104768a1214");

    if (meshService) {
      int characteristicCount = peripheral.characteristicCount();
      Serial.print(characteristicCount);
      Serial.println(" characteristic discovered");

      Serial.println("Discovering characteristic attributes ...");

      if (peripheral.hasCharacteristic("19b10001-e8f2-537e-4f6c-d104768a1214")) {
        Serial.println("Peripheral has battery level characteristic");
        return;
      } else {
        Serial.println("No led characteristic");
        return;
      }
    }
  }
}

CODE FOR NICLA SENSE ME (PERIPHERAL)

pitdagosti avatar Mar 23 '22 13:03 pitdagosti