TannerGilbert / Arduino-Nano-33-BLE-Sense-Code-Collection

Arduino Nano 33 BLE Sense Code Collection

Home Page:https://gilberttanner.com/blog/arduino-nano-33-ble-sense-overview

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

BLE.scan stops automatically after 10-15 minutes.

murali-dotworld opened this issue · comments

hello, My application is to connect to the android phone via GATT server and get data from the specific Service UUID. I am using Arduino nano 33 ble, but ble stops scanning after 10-15 minutes. Can you any solution for continous scan without any stop in the scan.

#include <ArduinoBLE.h>
String compare = "fd05a570-274a-4b1f-a5a3-eb52e5e02b8b";
void setup() {
  Serial.begin(115200);
  while (!Serial);
  if (!BLE.begin()) {
    Serial.println("starting BLE failed!");
    while (1);
  }
  Serial.println("BLE Central - Peripheral Explorer");
  BLE.scanForUuid("fd05a570-274a-4b1f-a5a3-eb52e5e02b8b", true);
}
void loop() {
  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.scanForUuid("fd05a570-274a-4b1f-a5a3-eb52e5e02b8b", 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();
  BLECharacteristic ledCharacteristic = peripheral.characteristic("53447ca9-1e5b-448e-ab7b-bd9438c048af");
  if (!ledCharacteristic)
  {
    Serial.println("Peripheral does not have characterstic");
    peripheral.disconnect();
    return;
  }
  if (ledCharacteristic.canRead())
  {
    ledCharacteristic.read();
    if (ledCharacteristic.valueLength() > 0) {
      Serial.print(", value 0x");
      printData(ledCharacteristic.value(), ledCharacteristic.valueLength());
    }
    peripheral.disconnect();
    return;
  }
  else
  {
    peripheral.disconnect();
    return;
  }
  peripheral.disconnect();
  return;
}

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));
  }
}