BLE device not showing up on Bluetooth scan when Arduino IDE is not open

I have an Arduino sketch uploaded to my Arduino Nano BLE:

#include <ArduinoBLE.h>

BLEService ledService("07694453-2dd6-4a4c-8c1e-1e3466a6c5734");
BLECharCharacteristic switchChar("2cf11b06-ce75-4d8e-ad1a-be116a432fa2", BLEWrite | BLERead); // create switch characteristic

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

  if (!BLE.begin()) {
    Serial.println("Failed to initialize BLE.");
    while (1);
  }

  BLE.setLocalName("Arduino Nano BLE");
  BLE.setAdvertisedService(ledService);
  ledService.addCharacteristic(switchChar);
  BLE.addService(ledService);

  BLE.advertise();
  Serial.println("BLE Peripheral device started advertising.");
}

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

  if (central) {
    Serial.print("Connected to central MAC: ");
    Serial.println(central.address());
    digitalWrite(LED_BUILTIN, HIGH);
   

    while (central.connected()){
      switchChar.writeValue('H');
      delay(1000);
    }
    digitalWrite(LED_BUILTIN, LOW);
  }
}

When Arduino IDE is open, I can see that the Arduino is advertising and get's connected to the computer through Bluetooth. I can verify that from the computer. However, when I plug the Arduino to a pc that is not running Arduino IDE the device is not discoverable by a Bluetooth scanner. Am I doing something the wrong way in this code, or is this expectable? If so, why?

Thank you.

The problem occurred in while (!Serial); line! I just have to remove it so it doesn't wait for a serial output to exist.

@billthekid2 is right, I hope you will test his solution.
When using 33BLE, I use to substitute the while(!Serial); by delay(1500); wich give time for the serial to be set (andeventually allows you to see the "failed to initialize" text.

Please note, with or without BLE, the arduino 33BLE will be stuck in the while(!Serial) loop till you connect it to your PC! very usefull when debugging, then blocking when you have non connected config!

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.