Skip to content

BLE.scan(true) stopped after 10-15 minutes #202

Open
@murali-dotworld

Description

@murali-dotworld

Hello, I have used nano ble 33 module. I am connect to the peripheral device and getting data, while allows a scan with duplicates.
But the scan stops automatically 10-15 minutes. Can you suggest way for continous scan

#include<ArduinoBLE.h>

String compare = "fd05a570-274a-4b1f-a5a3-eb52e5e02b8b";
void setup() {
  Serial.begin(115200);
  while (!Serial);
  // begin initialization
  if (!BLE.begin()) {
    Serial.println("starting BLE failed!");
    while (1);
  }
  //BLE.debug(Serial);
  Serial.println("BLE Central - Peripheral Explorer");
  // start scanning for peripherals
  //BLE.debug(Serial1);
  BLE.scan(true);
}
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.print("' ");
    Serial.print("RSSI  :");
    Serial.println(peripheral.rssi());
    String compare = "fd05a570-274a-4b1f-a5a3-eb52e5e02b8b";
    if (peripheral.rssi() > -55)
    {
      String compare = "fd05a570-274a-4b1f-a5a3-eb52e5e02b8b";
      if (peripheral.advertisedServiceUuid() == compare)
      {
        BLE.stopScan();
        explorerPeripheral(peripheral);
        BLE.scan(true);
      }
    }
  }
}
void explorerPeripheral(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;
  }
  // read and print device name of peripheral
  Serial.println();
  Serial.print("Device name: ");
  Serial.println(peripheral.deviceName());
  Serial.print("Appearance: 0x");
  Serial.println(peripheral.appearance(), HEX);
  Serial.println();
  // loop the services of the peripheral and explore each
  for (int i = 0; i < peripheral.serviceCount(); i++)
  {
    BLEService service = peripheral.service(i);
    exploreService(service);
  }
  Serial.println();
  // we are done exploring, disconnect
  Serial.println("Disconnecting ...");
  peripheral.disconnect();
  Serial.println("Disconnected");
}

void exploreService(BLEService service) {
  // print the UUID of the service
  Serial.print("Service ");
  Serial.println(service.uuid());
  String comp = "fd05a570-274a-4b1f-a5a3-eb52e5e02b8b";
  if (service.uuid() == comp)
  {
    // loop the characteristics of the service and explore each
    for (int i = 0; i < service.characteristicCount(); i++) {
      BLECharacteristic characteristic = service.characteristic(i);
      exploreCharacteristic(characteristic);
    }
  }
}
void exploreCharacteristic(BLECharacteristic characteristic) {
  // print the UUID and properties of the characteristic
  Serial.print("\tCharacteristic ");
  Serial.print(characteristic.uuid());
  Serial.print(", properties 0x");
  Serial.print(characteristic.properties(), HEX);
  // check if the characteristic is readable
  if (characteristic.canRead()) {
    // read the characteristic value
    characteristic.read();
    if (characteristic.valueLength() > 0) {
      // print out the value of the characteristic
      Serial.print(", value 0x");
      printData(characteristic.value(), characteristic.valueLength());
      characteristic.writeValue((byte)0x01);
    }
  }
  Serial.println();
}

void exploreDescriptor(BLECharacteristic characteristic) {
  if (characteristic.canRead()) {
    characteristic.read();
    if (characteristic.valueLength() > 0) {
      Serial.print("Value: ");
      printData(characteristic.value(), characteristic.valueLength());
      characteristic.writeValue((byte)0x01);
    }
  }
  Serial.println();
  //  // print the UUID of the descriptor
  //  Serial.print("\t\tDescriptor ");
  //  Serial.println(descriptor.uuid());
  //  // read the descriptor value
  //  descriptor.read();
  //  // print out the value of the descriptor
  //  Serial.print("value:");
  //  printData(descriptor.value(), descriptor.valueLength());
  //  Serial.println();
}
void printData(const unsigned char data[], int length)
{
  for (int i = 0; i < length; i++)
  {
    unsigned char b = data[i];
    if (b < 16) {
      Serial.print("0");
    }
    Serial.print(String((char)b));
  }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    status: waiting for informationMore information must be provided before work can proceedtopic: codeRelated to content of the project itselftype: imperfectionPerceived defect in any part of project

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions