Nicla Sense ME (Bug?)

I really need help. I am a noob in Sensors and I do not understand why my code won't work. I try to receive integers but it dosen't work. I always receive a 0. Please help :c

Central - Protenta H7:

#include "ArduinoBLE.h"

#define UUID_SENSOR_SERVICE "83e88034-23a5-488f-a143-a6874d1c64d3"
#define UUID_SENSOR_DATA "1476706d-b81b-4f90-bfce-7b11df5010fb"

BLEService sensorDataService(UUID_SENSOR_SERVICE);
BLEIntCharacteristic sensorCharacteristic(UUID_SENSOR_DATA, BLERead | BLEWrite);
BLEDevice peripheral;
uint8_t  data = 0;

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

  Serial.println("Starting setUP");

  if(!BLE.begin()){
    Serial.println("Bluetooth failed!");
  }

  Serial.print("Scanning for ");
  Serial.println(UUID_SENSOR_SERVICE);
  BLE.scan();
}

void loop() {
  peripheral = BLE.available();
  
  if(peripheral.localName() != "NICLA"){
    Serial.print("Terminating bluetooth connection with ");
    Serial.println(peripheral.localName());
    delay(1000);
    return;
  }

  BLE.stopScan();

  explore();

  BLE.scan();
}

void explore(){
  Serial.println("Connecting...");
  if(!peripheral.connect()){
    Serial.print("Peripheral connection failed with ");
    Serial.println(peripheral.localName());
    return;
  }
  else
  {
    Serial.println("Connected to "+peripheral.localName()+" Service UUID: "+peripheral.advertisedServiceUuid());
  }

  Serial.print("Has Characteristic: ");
  Serial.println(peripheral.hasCharacteristic(UUID_SENSOR_DATA));

  read();
}

void read(){
  Serial.println("Reading values");
  while(true)
  {
    sensorCharacteristic.readValue(data);
    Serial.println(data);
    delay(1000);
  }
}

Peripheral - Nicla Sense ME:

#include "ArduinoBLE.h"
#include "Arduino_BHY2.h"
#include "Nicla_System.h"

#define UUID_SENSOR_SERVICE "83e88034-23a5-488f-a143-a6874d1c64d3"
#define UUID_SENSOR_DATA "1476706d-b81b-4f90-bfce-7b11df5010fb"

SensorXYZ accelerometer(SENSOR_ID_ACC);
SensorXYZ gyro(SENSOR_ID_GYRO);

BLEService sensorDataService("83e88034-23a5-488f-a143-a6874d1c64d3");
BLEIntCharacteristic sensorCharacteristic("1476706d-b81b-4f90-bfce-7b11df5010fb", BLERead);
BLEDevice central;

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

  if(!setupBLE()){
    Serial.println("BLE failed!");
  }

  setupBHY();
}

void loop() {
  sensorCharacteristic.writeValue(101);

  central = BLE.central();

  if(central){
    Serial.print("Connected to: ");
    Serial.println(central.address());
    central.connect();

    int i = 0;
    while(central.connected()){
      Serial.println(i);
      i = i + 1;
      sensorCharacteristic.writeValue(101 + i);
      delay(1000);
    }
  }
}

bool setupBLE()
{
  Serial.println("SetUp BLE");
  if(!BLE.begin())
  {
    return false;
  }

  Serial.println("Set BLE configurtions!");
  BLE.setLocalName("Sensor");
  BLE.setDeviceName("Nicla Sense ME");
  BLE.setAdvertisedService(sensorDataService);

  sensorDataService.addCharacteristic(sensorCharacteristic);

  BLE.addService(sensorDataService);

  sensorCharacteristic.writeValue(101);

  BLE.advertise();

  return true;
}

void setupBHY(){
  Serial.println("SetUP BHY2");
  BHY2.begin();
  accelerometer.begin();
  gyro.begin();
}

Sorry for the messy code. I am currently trying to make it work.

Fixed it. I am just dumb ;p

Hello!
I guess it was the serial blocking the runtime, if you encounter any issue let us know!

Hi, I am having a similar issue with only receiving zeros, could you share your fix? I am using a nano 33 as my central device but maybe your solution will help me debug.

Thanks!