BLE.scanForUuid() : Cannot connect to the UUID of another Arduino Nano 33 IoT

Hello,

I cannot find the solution to my problem, so I decide to post it here. Hope it is the correct place.

I have two Arduino nanos I want to connect together to communicate via bluetooth low energy. Here is the code coming from https://docs.arduino.cc/tutorials/nano-33-ble-sense/ble-device-to-device

For the central device :

/*
  Based on the example from https://docs.arduino.cc/tutorials/nano-33-ble-sense/ble-device-to-device

  Receive data from BLE and apply keyboard keys combination from it.
*/

#include <ArduinoBLE.h>
#include "Keyboard.h"

enum {
  GESTURE_NONE  = -1,
  GESTURE_UP    = 0,
  GESTURE_DOWN  = 1,
  GESTURE_LEFT  = 2,
  GESTURE_RIGHT = 3
};

const char* deviceServiceUuid = "19b10000-e8f2-537e-4f6c-d104768a1214";
const char* deviceServiceCharacteristicUuid = "19b10001-e8f2-537e-4f6c-d104768a1214";

int gesture = -1;

BLEService gestureService(deviceServiceUuid);
BLEByteCharacteristic gestureCharacteristic(deviceServiceCharacteristicUuid, BLERead | BLEWrite);


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

  Keyboard.begin();

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

  BLE.setLocalName("Keyboard master");
  BLE.setAdvertisedService(gestureService);
  gestureService.addCharacteristic(gestureCharacteristic);
  BLE.addService(gestureService);
  gestureCharacteristic.writeValue(-1);
  BLE.advertise();

  Serial.println("Nano 33 BLE (Peripheral Device)");
  Serial.println(" ");
}

void loop() {
  BLEDevice central = BLE.central();
  Serial.println("- Discovering central device...");
  delay(500);

  if (central) {
    Serial.println("* Connected to central device!");
    Serial.print("* Device MAC address: ");
    Serial.println(central.address());
    Serial.println(" ");

    while (central.connected()) {
      if (gestureCharacteristic.written()) {
        gesture = gestureCharacteristic.value();
        writeGesture(gesture);
      }
    }

    Serial.println("* Disconnected to central device!");
  }
}

void writeGesture(int gesture) {
  Serial.println("- Characteristic <gesture_type> has changed!");

  switch (gesture) {
    case GESTURE_UP:
      Keyboard.press(KEY_LEFT_GUI);
      Keyboard.press(KEY_UP_ARROW);
      Keyboard.releaseAll();
      break;
    case GESTURE_DOWN:
      Keyboard.press(KEY_LEFT_GUI);
      Keyboard.press(KEY_DOWN_ARROW);
      Keyboard.releaseAll();
      break;
    case GESTURE_LEFT:
      Keyboard.press(KEY_LEFT_GUI);
      Keyboard.press(KEY_LEFT_ARROW);
      Keyboard.releaseAll();
      break;
    case GESTURE_RIGHT:
      Keyboard.press(KEY_LEFT_GUI);
      Keyboard.press(KEY_RIGHT_ARROW);
      Keyboard.releaseAll();
      break;
    default:
      break;
  }
}

For the peripheral device

/*
  Based on the example from https://docs.arduino.cc/tutorials/nano-33-ble-sense/ble-device-to-device

  Listens to IMU onboard Nano 33 IoT and send movements to BLE.
*/

#include <ArduinoBLE.h>
#include <Arduino_LSM6DS3.h>

// BLE settings
const char* deviceServiceUuid = "19b10000-e8f2-537e-4f6c-d104768a1214";
const char* deviceServiceCharacteristicUuid = "19b10001-e8f2-537e-4f6c-d104768a1214";

// IMU settings
float x, y, z;
int degreesX = 0;
int degreesY = 0;
int degreesZ = 0;

int gesture = -1;
int oldGestureValue = -1;

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

  if (!IMU.begin()) {
    Serial.println("Failed to initialize IMU!");
    while (1);
  }
  Serial.print("Accelerometer sample rate = ");
  Serial.print(IMU.accelerationSampleRate());
  Serial.println("Hz");

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

  BLE.setLocalName("Nano chair IMU");
  BLE.advertise();

  Serial.println("Nano chair IMU");
  Serial.println(" ");
}

void loop() {
  connectToPeripheral();
}

void connectToPeripheral() {
  BLEDevice peripheral;

  Serial.print("- Discovering peripheral device with UUID ");
  Serial.println(deviceServiceUuid);

  do
  {
    BLE.scanForUuid(deviceServiceUuid);
    //      BLE.scanForName("Keyboard master");
    peripheral = BLE.available();
  } while (!peripheral);

  if (peripheral) {
    Serial.println("* Peripheral device found!");
    Serial.print("* Device MAC address: ");
    Serial.println(peripheral.address());
    Serial.print("* Device name: ");
    Serial.println(peripheral.localName());
    Serial.print("* Advertised service UUID: ");
    Serial.println(peripheral.advertisedServiceUuid());
    Serial.println(" ");
    BLE.stopScan();
    controlPeripheral(peripheral);
  }
}

void controlPeripheral(BLEDevice peripheral) {
  Serial.println("- Connecting to peripheral device...");

  if (peripheral.connect()) {
    Serial.println("* Connected to peripheral device!");
    Serial.println(" ");
  } else {
    Serial.println("* Connection to peripheral device failed!");
    Serial.println(" ");
    return;
  }

  Serial.println("- Discovering peripheral device attributes...");
  if (peripheral.discoverAttributes()) {
    Serial.println("* Peripheral device attributes discovered!");
    Serial.println(" ");
  } else {
    Serial.println("* Peripheral device attributes discovery failed!");
    Serial.println(" ");
    peripheral.disconnect();
    return;
  }

  BLECharacteristic gestureCharacteristic = peripheral.characteristic(deviceServiceCharacteristicUuid);

  if (!gestureCharacteristic) {
    Serial.println("* Peripheral device does not have gesture_type characteristic!");
    peripheral.disconnect();
    return;
  } else if (!gestureCharacteristic.canWrite()) {
    Serial.println("* Peripheral does not have a writable gesture_type characteristic!");
    peripheral.disconnect();
    return;
  }

  while (peripheral.connected()) {
    gesture = gestureDetectection();

    if (oldGestureValue != gesture) {
      oldGestureValue = gesture;
      Serial.print("* Writing value to gesture_type characteristic: ");
      Serial.println(gesture);
      gestureCharacteristic.writeValue((byte)gesture);
      Serial.println("* Writing value to gesture_type characteristic done!");
      Serial.println(" ");
    }

  }
  Serial.println("- Peripheral device disconnected!");
}

int gestureDetectection() { // uses IMU to detect movements

  if (IMU.gyroscopeAvailable()) {
    IMU.readGyroscope(x, y, z);
  }

  //  if (x > 0.5) {
  //    x = 100 * x;
  //    degreesX = map(x, 0, 97, 0, 90);
  //    Serial.print("Tilting up ");
  //    Serial.print(degreesX);
  //    Serial.println("  degrees");
  //  }
  //  if (x < -0.5) {
  //    x = 100 * x;
  //    degreesX = map(x, 0, -100, 0, 90);
  //    Serial.print("Tilting down ");
  //    Serial.print(degreesX);
  //    Serial.println("  degrees");
  //  }
  //  if (y > 0.5) {
  //    y = 100 * y;
  //    degreesY = map(y, 0, 97, 0, 90);
  //    Serial.print("Tilting left ");
  //    Serial.print(degreesY);
  //    Serial.println("  degrees");
  //  }
  //  if (y < -0.5) {
  //    y = 100 * y;
  //    degreesY = map(y, 0, -100, 0, 90);
  //    Serial.print("Tilting right ");
  //    Serial.print(degreesY);
  //    Serial.println("  degrees");
  //  }
  if (abs(x) > 30) {
    Serial.print("x ");
    Serial.println(x);
  }
  if (abs(y) > 30) {
    Serial.print("y ");
    Serial.println(y);
  }
  delay(300);

  return -1;
}

The serial monitor shows this:

Nano chair IMU
 
- Discovering peripheral device with UUID 19b10000-e8f2-537e-4f6c-d104768a1214

And then nothing happens. It then does not detect the UUID. I tried on my phone with the nRF Connect app and I can connect to the UUID. I tried the BLE.scan() function on the peripheral, and it also detects the UUID in the detected devices.

Can anyone please help me here? :cry:

Thanks for your help !

So I found a way around, I feel like people deserve to know. I added a delay to the BLE.scanForUuid() loop and it detects the ID now.

...
do
  {
    BLE.scanForUuid(deviceServiceUuid);
    peripheral = BLE.available();
    Serial.print(".");
    delay(100);
  } while (!peripheral);
...

thank, same problem.