Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

BLE device is not showing up in 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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

It looks like because your Arduino is waiting for serial (USB) connection at while (!Serial);.

An example of fix is adding timeout there.

unsigned long startTime = millis();
while (!Serial && millis() - startTime < 1000);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading