Xiao Seeed nRF52840 Sense: BLE - Transfers data back and forth between two devices

Hi everyone. I’m having problem with transfering data between 2 devices by using ArduinoBLE. Basically, I can send a value from devices A to devices B and I succeeded. At B, I put a condition to send back A a value but I can’t send it. Till now, I haven’t solved it. Please guide me. Thank you.

Code of Central (Device A - Xiao Seeed nRF52840 Sense):

#include <ArduinoBLE.h>

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

  // initialize the Bluetooth® Low Energy hardware
  BLE.begin();
  Serial.println("Bluetooth® Low Energy Central - Transmit 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) {
    Serial.print("Found ");
    Serial.print(peripheral.address());
    Serial.print(" '");
    Serial.print(peripheral.localName());
    Serial.print("' ");
    Serial.print(peripheral.advertisedServiceUuid());
    Serial.println();

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

    // stop scanning
    BLE.stopScan();
    functionExecute(peripheral);

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

void functionExecute(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 characteristic
  BLECharacteristic commandCharacteristic = peripheral.characteristic("19b10001-e8f2-537e-4f6c-d104768a1214");

  if (!commandCharacteristic) {
    Serial.println("Peripheral does not have characteristic!");
    peripheral.disconnect();
    return;
  }

  if (!commandCharacteristic.canWrite()) {
    Serial.println("Peripheral does not have a writable characteristic!");
    peripheral.disconnect();
    return;
  }
  // Retrieve the response characteristic
  BLECharacteristic responseCharacteristic = peripheral.characteristic("19b10002-e8f2-537e-4f6c-d104768a1214");

  if (!responseCharacteristic) {  
    Serial.println("Peripheral does not have response characteristic!");
    peripheral.disconnect();
    return;
  }

  if (!responseCharacteristic.canRead()) {
    Serial.println("Cannot read from the response characteristic!");
    peripheral.disconnect();
    return;
  }

  // Write and read operations loop
  while (peripheral.connected()) {
    int randomNumber = random(1, 5);
    Serial.print("Your Command: ");
    Serial.println(randomNumber);
    commandCharacteristic.writeValue((byte)randomNumber);
    
    if (responseCharacteristic.valueUpdated()) {
      // Use a byte array to hold the read value
      byte value[2]; // Adjust the size based on the expected value size
      int length = responseCharacteristic.readValue(value, sizeof(value));
      if (length > 0) {
        // Convert the byte array to int if needed
        int responseValue = value[0]; // Simplified, assuming we know the data structure
        Serial.print("Received response: ");
        Serial.println(responseValue);
      }
    }

    delay(1000);
  }

  Serial.println("Peripheral disconnected");
}

Code of Peripheral (Devices B - ESP32):

  #include <ArduinoBLE.h>

  // Define a BLE Service
  BLEService receivedValueService("19B10000-E8F2-537E-4F6C-D104768A1214"); // BLE LED Service
  BLEIntCharacteristic responseCharacteristic("19B10002-E8F2-537E-4F6C-D104768A1214", BLERead | BLEWrite);
  BLEIntCharacteristic valueCharacteristic("19B10001-E8F2-537E-4F6C-D104768A1214", BLERead | BLEWrite);

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

    // Initialize BLE
    if (!BLE.begin()) {
      Serial.println("starting Bluetooth® Low Energy module failed!");
      while (1);
    }

    BLE.setLocalName("ESP32");
    BLE.setAdvertisedService(receivedValueService);
    receivedValueService.addCharacteristic(valueCharacteristic);
    receivedValueService.addCharacteristic(responseCharacteristic);  // Add response characteristic
    BLE.addService(receivedValueService);
    valueCharacteristic.writeValue(0);
    BLE.advertise();

    Serial.println("BLE LED Peripheral - Now with int support");
  }

  void loop() {
    BLEDevice central = BLE.central();

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

      while (central.connected()) {
        if (valueCharacteristic.written()) {
          int receivedValue = valueCharacteristic.value();
          Serial.print("Received Command: ");
          Serial.println(receivedValue);

          if (receivedValue == 3) {
            int responseValue = 10;
            responseCharacteristic.writeValue((byte)responseValue);
            Serial.print("Task finished, sent response: ");
            Serial.println(responseValue);
          }
        }
      }

      Serial.print("Disconnected from central: ");
      Serial.println(central.address());
    }
  }

Hi Calax, at Central ( A), try adding

responseCharacteristic.subscribe()

after get a true responseCharacteristic. Hope this could help.