nkolban / esp32-snippets

Sample ESP32 snippets and code fragments
https://leanpub.com/kolban-ESP32
Apache License 2.0
2.33k stars 709 forks source link

Scan does not stop/is restarting although serviceUUID is found #745

Open an-erd opened 5 years ago

an-erd commented 5 years ago

BLE_Client.ino finds correct serviceUUID but does not stop/is restarting scanning

Used version: arduino-esp32 1.0.1-rc incl. the 13 commits since that realease (until #af7e489) Debug logging level configured Device used: M5Stack-fire PSRAM disabled BLE Devices are awake.

Sketch BLE_Client.ino with changes in pBLEScan->setInterval(2000); pBLEScan->setWindow(1500); and cb functions added:

/**
 * A BLE client example that is rich in capabilities.
 * There is a lot new capabilities implemented.
 * author unknown
 * updated by chegewara
 */

#include "BLEDevice.h"
//#include "BLEScan.h"

// The remote service we wish to connect to.
static BLEUUID serviceUUID("00001814-0000-1000-8000-00805f9b34fb");
// The characteristic of the remote service we are interested in.
static BLEUUID    charUUID("0000ff00-0000-1000-8000-00805f9b34fb");

static boolean doConnect = false;
static boolean connected = false;
static boolean doScan = false;
static BLERemoteCharacteristic* pRemoteCharacteristic;
static BLEAdvertisedDevice* myDevice;

static void notifyCallback(
  BLERemoteCharacteristic* pBLERemoteCharacteristic,
  uint8_t* pData,
  size_t length,
  bool isNotify) {
    Serial.print("Notify callback for characteristic ");
    Serial.print(pBLERemoteCharacteristic->getUUID().toString().c_str());
    Serial.print(" of data length ");
    Serial.println(length);
    Serial.print("data: ");
    Serial.println((char*)pData);
}

class MyClientCallback : public BLEClientCallbacks {
  void onConnect(BLEClient* pclient) {
  }

  void onDisconnect(BLEClient* pclient) {
    connected = false;
    Serial.println("onDisconnect");
  }
};

static void my_gattc_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if, esp_ble_gattc_cb_param_t* param) {
    ESP_LOGW(LOG_TAG, "custom gattc event handler, event: %d", (uint8_t)event);
        if(event == ESP_GATTC_DISCONNECT_EVT) {
                Serial.print("Disconnect reason: "); 
                Serial.println((int)param->disconnect.reason);
        }
}

static void my_gatts_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_t gattc_if, esp_ble_gatts_cb_param_t* param) {
    ESP_LOGI(LOG_TAG, "custom gatts event handler, event: %d", (uint8_t)event);
}

static void my_gap_event_handler(esp_gap_ble_cb_event_t  event, esp_ble_gap_cb_param_t* param) {
    ESP_LOGI(LOG_TAG, "custom gap event handler, event: %d", (uint8_t)event);
}

bool connectToServer() {
    Serial.print("Forming a connection to ");
    Serial.println(myDevice->getAddress().toString().c_str());

    BLEClient*  pClient  = BLEDevice::createClient();
    Serial.println(" - Created client");

    pClient->setClientCallbacks(new MyClientCallback());

    // Connect to the remove BLE Server.
    pClient->connect(myDevice);  // if you pass BLEAdvertisedDevice instead of address, it will be recognized type of peer device address (public or private)
    Serial.println(" - Connected to server");

    // Obtain a reference to the service we are after in the remote BLE server.
    BLERemoteService* pRemoteService = pClient->getService(serviceUUID);
    if (pRemoteService == nullptr) {
      Serial.print("Failed to find our service UUID: ");
      Serial.println(serviceUUID.toString().c_str());
      pClient->disconnect();
      return false;
    }
    Serial.println(" - Found our service");

    // Obtain a reference to the characteristic in the service of the remote BLE server.
    pRemoteCharacteristic = pRemoteService->getCharacteristic(charUUID);
    if (pRemoteCharacteristic == nullptr) {
      Serial.print("Failed to find our characteristic UUID: ");
      Serial.println(charUUID.toString().c_str());
      pClient->disconnect();
      return false;
    }
    Serial.println(" - Found our characteristic");

    // Read the value of the characteristic.
    if(pRemoteCharacteristic->canRead()) {
      std::string value = pRemoteCharacteristic->readValue();
      Serial.print("The characteristic value was: ");
      Serial.println(value.c_str());
    }

    //if(pRemoteCharacteristic->canNotify())
    //  pRemoteCharacteristic->registerForNotify(notifyCallback);

    connected = true;

    return true;
}
/**
 * Scan for BLE servers and find the first one that advertises the service we are looking for.
 */
class MyAdvertisedDeviceCallbacks: public BLEAdvertisedDeviceCallbacks {
 /**
   * Called for each advertising BLE server.
   */
  void onResult(BLEAdvertisedDevice advertisedDevice) {
    Serial.print("BLE Advertised Device found: ");
    Serial.println(advertisedDevice.toString().c_str());

    // We have found a device, let us now see if it contains the service we are looking for.
    if (advertisedDevice.haveServiceUUID() && advertisedDevice.isAdvertisingService(serviceUUID)) {

      BLEDevice::getScan()->stop();
      myDevice = new BLEAdvertisedDevice(advertisedDevice);
      doConnect = true;
      doScan = true;

    } // Found our server
  } // onResult
}; // MyAdvertisedDeviceCallbacks

void setup() {
  Serial.begin(115200);
  Serial.println("Starting Arduino BLE Client application...");
  BLEDevice::setCustomGapHandler(my_gap_event_handler);
  BLEDevice::setCustomGattsHandler(my_gatts_event_handler);
  BLEDevice::setCustomGattcHandler(my_gattc_event_handler);

  BLEDevice::init("");

  // Retrieve a Scanner and set the callback we want to use to be informed when we
  // have detected a new device.  Specify that we want active scanning and start the
  // scan to run for 5 seconds.
  BLEScan* pBLEScan = BLEDevice::getScan();
  pBLEScan->setAdvertisedDeviceCallbacks(new MyAdvertisedDeviceCallbacks());
  pBLEScan->setInterval(2000);
  pBLEScan->setWindow(1500);
  pBLEScan->setActiveScan(true);
  pBLEScan->start(10, false);
} // End of setup.

// This is the Arduino main loop function.
void loop() {

  // If the flag "doConnect" is true then we have scanned for and found the desired
  // BLE Server with which we wish to connect.  Now we connect to it.  Once we are 
  // connected we set the connected flag to be true.
  if (doConnect == true) {
    if (connectToServer()) {
      Serial.println("We are now connected to the BLE Server.");
    } else {
      Serial.println("We have failed to connect to the server; there is nothin more we will do.");
    }
    doConnect = false;
  }

  delay(100);

  // If we are connected to a peer BLE Server, update the characteristic each time we are reached
  // with the current time since boot.
  if (connected) {
    String newValue = "Time since boot: " + String(millis()/1000);
    Serial.println("Setting new characteristic value to \"" + newValue + "\"");

    // Set the characteristic's value to be the array of bytes that is actually a string.
    pRemoteCharacteristic->writeValue(newValue.c_str(), newValue.length());
  }else if(doScan){
    BLEDevice::getScan()->start(0);  // this is just eample to start scan after disconnect, most likely there is better way to do it in arduino
  }

  delay(1000); // Delay a second between loops.
} // End of loop
Opening port
Port open
Starting Arduino BLE Client application...
[D][BLEScan.cpp:204] start(): >> start(duration=10)
[D][FreeRTOS.cpp:165] take(): Semaphore taking: name: ScanEnd (0x3ffdf3ec), owner: <N/A> for start
[D][FreeRTOS.cpp:174] take(): Semaphore taken:  name: ScanEnd (0x3ffdf3ec), owner: start
[D][BLEScan.cpp:236] start(): << start()
[I][BLE_client.ino:59] my_gap_event_handler(): custom gap event handler, event: 2
[I][BLE_client.ino:59] my_gap_event_handler(): custom gap event handler, event: 7
[D][BLEAdvertisedDevice.cpp:424] setRSSI(): - setRSSI(): rssi: -66
[D][BLEAdvertisedDevice.cpp:253] parseAdvertisement(): Type: 0x01 (), length: 1, data: 1a
[D][BLEAdvertisedDevice.cpp:253] parseAdvertisement(): Type: 0xff (), length: 9, data: 4c001005031c8bb496
[D][BLEAdvertisedDevice.cpp:401] setManufacturerData(): - manufacturer data: 4c001005031c8bb496
BLE Advertised Device found: Name: , Address: 5f:93:95:3c:d5:a6, manufacturer data: 4c001005031c8bb496
[I][BLE_client.ino:59] my_gap_event_handler(): custom gap event handler, event: 3
[D][BLEAdvertisedDevice.cpp:424] setRSSI(): - setRSSI(): rssi: -52
[D][BLEAdvertisedDevice.cpp:253] parseAdvertisement(): Type: 0x01 (), length: 1, data: 1a
[D][BLEAdvertisedDevice.cpp:253] parseAdvertisement(): Type: 0xff (), length: 9, data: 4c001005031c0ba1c9
[D][BLEAdvertisedDevice.cpp:401] setManufacturerData(): - manufacturer data: 4c001005031c0ba1c9
BLE Advertised Device found: Name: , Address: 79:3a:d0:27:3f:1e, manufacturer data: 4c001005031c0ba1c9
[I][BLE_client.ino:59] my_gap_event_handler(): custom gap event handler, event: 3
[D][BLEScan.cpp:106] handleGAPEvent(): Ignoring 5f:93:95:3c:d5:a6, already seen it.
[I][BLE_client.ino:59] my_gap_event_handler(): custom gap event handler, event: 3
[D][BLEScan.cpp:106] handleGAPEvent(): Ignoring 79:3a:d0:27:3f:1e, already seen it.
[I][BLE_client.ino:59] my_gap_event_handler(): custom gap event handler, event: 3
[D][BLEScan.cpp:106] handleGAPEvent(): Ignoring 5f:93:95:3c:d5:a6, already seen it.
[I][BLE_client.ino:59] my_gap_event_handler(): custom gap event handler, event: 3
[D][BLEScan.cpp:106] handleGAPEvent(): Ignoring 79:3a:d0:27:3f:1e, already seen it.
[I][BLE_client.ino:59] my_gap_event_handler(): custom gap event handler, event: 3
[D][BLEScan.cpp:106] handleGAPEvent(): Ignoring 5f:93:95:3c:d5:a6, already seen it.
[I][BLE_client.ino:59] my_gap_event_handler(): custom gap event handler, event: 3
[D][BLEScan.cpp:106] handleGAPEvent(): Ignoring 5f:93:95:3c:d5:a6, already seen it.
[I][BLE_client.ino:59] my_gap_event_handler(): custom gap event handler, event: 3
[D][BLEScan.cpp:106] handleGAPEvent(): Ignoring 79:3a:d0:27:3f:1e, already seen it.
[I][BLE_client.ino:59] my_gap_event_handler(): custom gap event handler, event: 3
[D][BLEScan.cpp:106] handleGAPEvent(): Ignoring 5f:93:95:3c:d5:a6, already seen it.
[I][BLE_client.ino:59] my_gap_event_handler(): custom gap event handler, event: 3
[D][BLEScan.cpp:106] handleGAPEvent(): Ignoring 5f:93:95:3c:d5:a6, already seen it.
[I][BLE_client.ino:59] my_gap_event_handler(): custom gap event handler, event: 3
[D][BLEScan.cpp:106] handleGAPEvent(): Ignoring 5f:93:95:3c:d5:a6, already seen it.
[I][BLE_client.ino:59] my_gap_event_handler(): custom gap event handler, event: 3
[D][BLEScan.cpp:106] handleGAPEvent(): Ignoring 5f:93:95:3c:d5:a6, already seen it.
[I][BLE_client.ino:59] my_gap_event_handler(): custom gap event handler, event: 3
[D][BLEScan.cpp:106] handleGAPEvent(): Ignoring 79:3a:d0:27:3f:1e, already seen it.
[I][BLE_client.ino:59] my_gap_event_handler(): custom gap event handler, event: 3
[D][BLEScan.cpp:106] handleGAPEvent(): Ignoring 5f:93:95:3c:d5:a6, already seen it.
[I][BLE_client.ino:59] my_gap_event_handler(): custom gap event handler, event: 3
[D][BLEAdvertisedDevice.cpp:424] setRSSI(): - setRSSI(): rssi: -64
[D][BLEAdvertisedDevice.cpp:253] parseAdvertisement(): Type: 0x01 (), length: 1, data: 06
[D][BLEAdvertisedDevice.cpp:253] parseAdvertisement(): Type: 0x02 (), length: 4, data: 1418f0ff
[D][BLEAdvertisedDevice.cpp:453] setServiceUUID(): - addServiceUUID(): serviceUUID: 00001814-0000-1000-8000-00805f9b34fb
[D][BLEAdvertisedDevice.cpp:453] setServiceUUID(): - addServiceUUID(): serviceUUID: 0000fff0-0000-1000-8000-00805f9b34fb
[D][BLEAdvertisedDevice.cpp:253] parseAdvertisement(): Type: 0x09 (), length: 15, data: 4d696c6573746f6e65506f64204636
[D][BLEAdvertisedDevice.cpp:413] setName(): - setName(): name: MilestonePod F6
[D][BLEAdvertisedDevice.cpp:253] parseAdvertisement(): Type: 0x12 (), length: 4, data: 10002000
[D][BLEAdvertisedDevice.cpp:349] parseAdvertisement(): Unhandled type: adType: 18 - 0x12
[D][BLEAdvertisedDevice.cpp:253] parseAdvertisement(): Type: 0x0a (), length: 1, data: 00
[D][BLEAdvertisedDevice.cpp:484] setTXPower(): - txPower: 0
BLE Advertised Device found: Name: MilestonePod F6, Address: 50:33:8b:1c:ef:f6, serviceUUID: 00001814-0000-1000-8000-00805f9b34fb, txPower: 0
[D][BLEScan.cpp:259] stop(): >> stop()
[D][BLEScan.cpp:271] stop(): << stop()
[I][BLE_client.ino:59] my_gap_event_handler(): custom gap event handler, event: 3
[I][BLE_client.ino:59] my_gap_event_handler(): custom gap event handler, event: 3
[I][BLE_client.ino:59] my_gap_event_handler(): custom gap event handler, event: 18
[D][BLEScan.cpp:204] start(): >> start(duration=0)
[D][FreeRTOS.cpp:165] take(): Semaphore taking: name: ScanEnd (0x3ffdf3ec), owner: <N/A> for start
[D][FreeRTOS.cpp:174] take(): Semaphore taken:  name: ScanEnd (0x3ffdf3ec), owner: start
[D][BLEScan.cpp:236] start(): << start()
[I][BLE_client.ino:59] my_gap_event_handler(): custom gap event handler, event: 2
[I][BLE_client.ino:59] my_gap_event_handler(): custom gap event handler, event: 7
[D][BLEAdvertisedDevice.cpp:424] setRSSI(): - setRSSI(): rssi: -60
[D][BLEAdvertisedDevice.cpp:253] parseAdvertisement(): Type: 0x01 (), length: 1, data: 1a
[D][BLEAdvertisedDevice.cpp:253] parseAdvertisement(): Type: 0xff (), length: 9, data: 4c001005031c8bb496
[D][BLEAdvertisedDevice.cpp:401] setManufacturerData(): - manufacturer data: 4c001005031c8bb496
BLE Advertised Device found: Name: , Address: 5f:93:95:3c:d5:a6, manufacturer data: 4c001005031c8bb496
[I][BLE_client.ino:59] my_gap_event_handler(): custom gap event handler, event: 3
[D][BLEAdvertisedDevice.cpp:424] setRSSI(): - setRSSI(): rssi: -60
[D][BLEAdvertisedDevice.cpp:253] parseAdvertisement(): Type: 0x01 (), length: 1, data: 1a
[D][BLEAdvertisedDevice.cpp:253] parseAdvertisement(): Type: 0xff (), length: 9, data: 4c001005031c0ba1c9
[D][BLEAdvertisedDevice.cpp:401] setManufacturerData(): - manufacturer data: 4c001005031c0ba1c9
BLE Advertised Device found: Name: , Address: 79:3a:d0:27:3f:1e, manufacturer data: 4c001005031c0ba1c9
[I][BLE_client.ino:59] my_gap_event_handler(): custom gap event handler, event: 3
[D][BLEScan.cpp:106] handleGAPEvent(): Ignoring 5f:93:95:3c:d5:a6, already seen it.
[I][BLE_client.ino:59] my_gap_event_handler(): custom gap event handler, event: 3
[D][BLEScan.cpp:106] handleGAPEvent(): Ignoring 79:3a:d0:27:3f:1e, already seen it.
[I][BLE_client.ino:59] my_gap_event_handler(): custom gap event handler, event: 3
[D][BLEScan.cpp:106] handleGAPEvent(): Ignoring 5f:93:95:3c:d5:a6, already seen it.
[I][BLE_client.ino:59] my_gap_event_handler(): custom gap event handler, event: 3
[D][BLEScan.cpp:106] handleGAPEvent(): Ignoring 79:3a:d0:27:3f:1e, already seen it.
[I][BLE_client.ino:59] my_gap_event_handler(): custom gap event handler, event: 3
[D][BLEScan.cpp:106] handleGAPEvent(): Ignoring 5f:93:95:3c:d5:a6, already seen it.
[I][BLE_client.ino:59] my_gap_event_handler(): custom gap event handler, event: 3
[D][BLEScan.cpp:106] handleGAPEvent(): Ignoring 79:3a:d0:27:3f:1e, already seen it.
[I][BLE_client.ino:59] my_gap_event_handler(): custom gap event handler, event: 3
[D][BLEScan.cpp:106] handleGAPEvent(): Ignoring 79:3a:d0:27:3f:1e, already seen it.
[I][BLE_client.ino:59] my_gap_event_handler(): custom gap event handler, event: 3
[D][BLEScan.cpp:106] handleGAPEvent(): Ignoring 79:3a:d0:27:3f:1e, already seen it.
[I][BLE_client.ino:59] my_gap_event_handler(): custom gap event handler, event: 3
[D][BLEScan.cpp:106] handleGAPEvent(): Ignoring 79:3a:d0:27:3f:1e, already seen it.
[I][BLE_client.ino:59] my_gap_event_handler(): custom gap event handler, event: 3
[D][BLEScan.cpp:106] handleGAPEvent(): Ignoring 5f:93:95:3c:d5:a6, already seen it.
[I][BLE_client.ino:59] my_gap_event_handler(): custom gap event handler, event: 3
[D][BLEScan.cpp:106] handleGAPEvent(): Ignoring 79:3a:d0:27:3f:1e, already seen it.
[I][BLE_client.ino:59] my_gap_event_handler(): custom gap event handler, event: 3
[D][BLEScan.cpp:106] handleGAPEvent(): Ignoring 5f:93:95:3c:d5:a6, already seen it.
[I][BLE_client.ino:59] my_gap_event_handler(): custom gap event handler, event: 3
[D][BLEScan.cpp:106] handleGAPEvent(): Ignoring 79:3a:d0:27:3f:1e, already seen it.
[I][BLE_client.ino:59] my_gap_event_handler(): custom gap event handler, event: 3
[D][BLEScan.cpp:106] handleGAPEvent(): Ignoring 5f:93:95:3c:d5:a6, already seen it.
[I][BLE_client.ino:59] my_gap_event_handler(): custom gap event handler, event: 3
[D][BLEScan.cpp:106] handleGAPEvent(): Ignoring 79:3a:d0:27:3f:1e, already seen it.
[I][BLE_client.ino:59] my_gap_event_handler(): custom gap event handler, event: 3
[D][BLEScan.cpp:106] handleGAPEvent(): Ignoring 5f:93:95:3c:d5:a6, already seen it.
[I][BLE_client.ino:59] my_gap_event_handler(): custom gap event handler, event: 3
[D][BLEScan.cpp:106] handleGAPEvent(): Ignoring 79:3a:d0:27:3f:1e, already seen it.
[I][BLE_client.ino:59] my_gap_event_handler(): custom gap event handler, event: 3
[D][BLEScan.cpp:106] handleGAPEvent(): Ignoring 5f:93:95:3c:d5:a6, already seen it.
[I][BLE_client.ino:59] my_gap_event_handler(): custom gap event handler, event: 3
[D][BLEScan.cpp:106] handleGAPEvent(): Ignoring 79:3a:d0:27:3f:1e, already seen it.
[I][BLE_client.ino:59] my_gap_event_handler(): custom gap event handler, event: 3
[D][BLEScan.cpp:106] handleGAPEvent(): Ignoring 5f:93:95:3c:d5:a6, already seen it.
[I][BLE_client.ino:59] my_gap_event_handler(): custom gap event handler, event: 3
[D][BLEScan.cpp:106] handleGAPEvent(): Ignoring 79:3a:d0:27:3f:1e, already seen it.
[I][BLE_client.ino:59] my_gap_event_handler(): custom gap event handler, event: 3
[D][BLEScan.cpp:106] handleGAPEvent(): Ignoring 5f:93:95:3c:d5:a6, already seen it.
[I][BLE_client.ino:59] my_gap_event_handler(): custom gap event handler, event: 3
[D][BLEScan.cpp:106] handleGAPEvent(): Ignoring 79:3a:d0:27:3f:1e, already seen it.
[I][BLE_client.ino:59] my_gap_event_handler(): custom gap event handler, event: 3
[D][BLEScan.cpp:106] handleGAPEvent(): Ignoring 5f:93:95:3c:d5:a6, already seen it.
[I][BLE_client.ino:59] my_gap_event_handler(): custom gap event handler, event: 3
[D][BLEScan.cpp:106] handleGAPEvent(): Ignoring 79:3a:d0:27:3f:1e, already seen it.
[I][BLE_client.ino:59] my_gap_event_handler(): custom gap event handler, event: 3
[D][BLEAdvertisedDevice.cpp:424] setRSSI(): - setRSSI(): rssi: -67
[D][BLEAdvertisedDevice.cpp:253] parseAdvertisement(): Type: 0x01 (), length: 1, data: 06
[D][BLEAdvertisedDevice.cpp:253] parseAdvertisement(): Type: 0xff (), length: 25, data: 4c000215e523fdf24c9111e5885d50338b1ceff600000000b0
[D][BLEAdvertisedDevice.cpp:401] setManufacturerData(): - manufacturer data: 4c000215e523fdf24c9111e5885d50338b1ceff600000000b0
[D][BLEAdvertisedDevice.cpp:253] parseAdvertisement(): Type: 0x09 (), length: 15, data: 4d696c6573746f6e65506f64204636
[D][BLEAdvertisedDevice.cpp:413] setName(): - setName(): name: MilestonePod F6
[D][BLEAdvertisedDevice.cpp:253] parseAdvertisement(): Type: 0x12 (), length: 4, data: 10002000
[D][BLEAdvertisedDevice.cpp:349] parseAdvertisement(): Unhandled type: adType: 18 - 0x12
[D][BLEAdvertisedDevice.cpp:253] parseAdvertisement(): Type: 0x0a (), length: 1, data: 00
[D][BLEAdvertisedDevice.cpp:484] setTXPower(): - txPower: 0
BLE Advertised Device found: Name: MilestonePod F6, Address: 50:33:8b:1c:ef:f6, manufacturer data: 4c000215e523fdf24c9111e5885d50338b1ceff600000000b0, txPower: 0
[I][BLE_client.ino:59] my_gap_event_handler(): custom gap event handler, event: 3
[D][BLEScan.cpp:106] handleGAPEvent(): Ignoring 5f:93:95:3c:d5:a6, already seen it.
[I][BLE_client.ino:59] my_gap_event_handler(): custom gap event handler, event: 3
[D][BLEScan.cpp:106] handleGAPEvent(): Ignoring 79:3a:d0:27:3f:1e, already seen it.
[I][BLE_client.ino:59] my_gap_event_handler(): custom gap event handler, event: 3
[D][BLEScan.cpp:106] handleGAPEvent(): Ignoring 5f:93:95:3c:d5:a6, already seen it.
[I][BLE_client.ino:59] my_gap_event_handler(): custom gap event handler, event: 3
[D][BLEScan.cpp:106] handleGAPEvent(): Ignoring 79:3a:d0:27:3f:1e, already seen it.
[I][BLE_client.ino:59] my_gap_event_handler(): custom gap event handler, event: 3
[D][BLEScan.cpp:106] handleGAPEvent(): Ignoring 5f:93:95:3c:d5:a6, already seen it.
[I][BLE_client.ino:59] my_gap_event_handler(): custom gap event handler, event: 3
[D][BLEScan.cpp:106] handleGAPEvent(): Ignoring 79:3a:d0:27:3f:1e, already seen it.
[I][BLE_client.ino:59] my_gap_event_handler(): custom gap event handler, event: 3
[D][BLEScan.cpp:106] handleGAPEvent(): Ignoring 79:3a:d0:27:3f:1e, already seen it.
[I][BLE_client.ino:59] my_gap_event_handler(): custom gap event handler, event: 3
[D][BLEScan.cpp:106] handleGAPEvent(): Ignoring 79:3a:d0:27:3f:1e, already seen it.
[I][BLE_client.ino:59] my_gap_event_handler(): custom gap event handler, event: 3
[D][BLEScan.cpp:106] handleGAPEvent(): Ignoring 79:3a:d0:27:3f:1e, already seen it.
[I][BLE_client.ino:59] my_gap_event_handler(): custom gap event handler, event: 3
[D][BLEScan.cpp:106] handleGAPEvent(): Ignoring 79:3a:d0:27:3f:1e, already seen it.
[I][BLE_client.ino:59] my_gap_event_handler(): custom gap event handler, event: 3
[D][BLEScan.cpp:106] handleGAPEvent(): Ignoring 5f:93:95:3c:d5:a6, already seen it.
[I][BLE_client.ino:59] my_gap_event_handler(): custom gap event handler, event: 3
[D][BLEScan.cpp:106] handleGAPEvent(): Ignoring 79:3a:d0:27:3f:1e, already seen it.
[I][BLE_client.ino:59] my_gap_event_handler(): custom gap event handler, event: 3
[D][BLEScan.cpp:106] handleGAPEvent(): Ignoring 5f:93:95:3c:d5:a6, already seen it.
[I][BLE_client.ino:59] my_gap_event_handler(): custom gap event handler, event: 3
[D][BLEScan.cpp:106] handleGAPEvent(): Ignoring 79:3a:d0:27:3f:1e, already seen it.
[I][BLE_client.ino:59] my_gap_event_handler(): custom gap event handler, event: 3
[D][BLEScan.cpp:106] handleGAPEvent(): Ignoring 5f:93:95:3c:d5:a6, already seen it.
[I][BLE_client.ino:59] my_gap_event_handler(): custom gap event handler, event: 3
[D][BLEScan.cpp:106] handleGAPEvent(): Ignoring 79:3a:d0:27:3f:1e, already seen it.
[I][BLE_client.ino:59] my_gap_event_handler(): custom gap event handler, event: 3
[D][BLEScan.cpp:106] handleGAPEvent(): Ignoring 5f:93:95:3c:d5:a6, already seen it.
[I][BLE_client.ino:59] my_gap_event_handler(): custom gap event handler, event: 3
[D][BLEScan.cpp:106] handleGAPEvent(): Ignoring 79:3a:d0:27:3f:1e, already seen it.
[I][BLE_client.ino:59] my_gap_event_handler(): custom gap event handler, event: 3
[D][BLEScan.cpp:106] handleGAPEvent(): Ignoring 5f:93:95:3c:d5:a6, already seen it.
[I][BLE_client.ino:59] my_gap_event_handler(): custom gap event handler, event: 3
[D][BLEScan.cpp:106] handleGAPEvent(): Ignoring 79:3a:d0:27:3f:1e, already seen it.
[I][BLE_client.ino:59] my_gap_event_handler(): custom gap event handler, event: 3
[D][BLEScan.cpp:106] handleGAPEvent(): Ignoring 5f:93:95:3c:d5:a6, already seen it.
[I][BLE_client.ino:59] my_gap_event_handler(): custom gap event handler, event: 3
[D][BLEScan.cpp:106] handleGAPEvent(): Ignoring 79:3a:d0:27:3f:1e, already seen it.
[I][BLE_client.ino:59] my_gap_event_handler(): custom gap event handler, event: 3
[D][BLEScan.cpp:106] handleGAPEvent(): Ignoring 5f:93:95:3c:d5:a6, already seen it.
[I][BLE_client.ino:59] my_gap_event_handler(): custom gap event handler, event: 3
[D][BLEScan.cpp:106] handleGAPEvent(): Ignoring 79:3a:d0:27:3f:1e, already seen it.
[I][BLE_client.ino:59] my_gap_event_handler(): custom gap event handler, event: 3
[D][BLEScan.cpp:106] handleGAPEvent(): Ignoring 5f:93:95:3c:d5:a6, already seen it.
[I][BLE_client.ino:59] my_gap_event_handler(): custom gap event handler, event: 3
[D][BLEScan.cpp:106] handleGAPEvent(): Ignoring 79:3a:d0:27:3f:1e, already seen it.
[I][BLE_client.ino:59] my_gap_event_handler(): custom gap event handler, event: 3
[D][BLEScan.cpp:106] handleGAPEvent(): Ignoring 50:33:8b:1c:ef:f6, already seen it.
[I][BLE_client.ino:59] my_gap_event_handler(): custom gap event handler, event: 3
[D][BLEScan.cpp:106] handleGAPEvent(): Ignoring 5f:93:95:3c:d5:a6, already seen it.
[I][BLE_client.ino:59] my_gap_event_handler(): custom gap event handler, event: 3
[D][BLEScan.cpp:106] handleGAPEvent(): Ignoring 79:3a:d0:27:3f:1e, already seen it.
[I][BLE_client.ino:59] my_gap_event_handler(): custom gap event handler, event: 3
[D][BLEScan.cpp:106] handleGAPEvent(): Ignoring 5f:93:95:3c:d5:a6, already seen it.
[I][BLE_client.ino:59] my_gap_event_handler(): custom gap event handler, event: 3
[D][BLEScan.cpp:106] handleGAPEvent(): Ignoring 79:3a:d0:27:3f:1e, already seen it.
[I][BLE_client.ino:59] my_gap_event_handler(): custom gap event handler, event: 3
[D][BLEScan.cpp:106] handleGAPEvent(): Ignoring 5f:93:95:3c:d5:a6, already seen it.
[I][BLE_client.ino:59] my_gap_event_handler(): custom gap event handler, event: 3
[D][BLEScan.cpp:106] handleGAPEvent(): Ignoring 79:3a:d0:27:3f:1e, already seen it.
[I][BLE_client.ino:59] my_gap_event_handler(): custom gap event handler, event: 3
[D][BLEScan.cpp:106] handleGAPEvent(): Ignoring 79:3a:d0:27:3f:1e, already seen it.
[I][BLE_client.ino:59] my_gap_event_handler(): custom gap event handler, event: 3
[D][BLEScan.cpp:106] handleGAPEvent(): Ignoring 79:3a:d0:27:3f:1e, already seen it.
[I][BLE_client.ino:59] my_gap_event_handler(): custom gap event handler, event: 3
[D][BLEScan.cpp:106] handleGAPEvent(): Ignoring 79:3a:d0:27:3f:1e, already seen it.
[I][BLE_client.ino:59] my_gap_event_handler(): custom gap event handler, event: 3
[D][BLEScan.cpp:106] handleGAPEvent(): Ignoring 79:3a:d0:27:3f:1e, already seen it.
[I][BLE_client.ino:59] my_gap_event_handler(): custom gap event handler, event: 3
...

The device found provides the correct serviceUUID, so scan should stop. In deed, it stops, but didn't report that the correct device was found, and then it starts again with a duration of 0. The log does not end (run for 10min) always reporting


[I][BLE_client.ino:59] my_gap_event_handler(): custom gap event handler, event: 3
[D][BLEScan.cpp:106] handleGAPEvent(): Ignoring 79:3a:d0:27:3f:1e, already seen it.
chegewara commented 5 years ago

Your logs:

BLE Advertised Device found: Name: MilestonePod F6, Address: 50:33:8b:1c:ef:f6, serviceUUID: 00001814-0000-1000-8000-00805f9b34fb, txPower: 0
[D][BLEScan.cpp:259] stop(): >> stop()
[D][BLEScan.cpp:271] stop(): << stop()
[I][BLE_client.ino:59] my_gap_event_handler(): custom gap event handler, event: 3
[I][BLE_client.ino:59] my_gap_event_handler(): custom gap event handler, event: 3
[I][BLE_client.ino:59] my_gap_event_handler(): custom gap event handler, event: 18
[D][BLEScan.cpp:204] start(): >> start(duration=0)

Scan stopped, but then it started again with duration 0 (infinity).

Its from this line:

  }else if(doScan){
    BLEDevice::getScan()->start(0);  // this is just eample to start scan after disconnect, most likely there is better way to do it in arduino
  }

It is called when device get disconnected.

an-erd commented 5 years ago

Ok, then I think the example provided by the BLE library needs to be reworked. The flag doScan is set in the cb function when we found our device, but before we're connected. Depending where we're in loop() it may be the case that we jump in the second block ... }else if(doScan){...

void loop() {

  // If the flag "doConnect" is true then we have scanned for and found the desired
  // BLE Server with which we wish to connect.  Now we connect to it.  Once we are 
  // connected we set the connected flag to be true.
  if (doConnect == true) {
    if (connectToServer()) {
      Serial.println("We are now connected to the BLE Server.");
    } else {
      Serial.println("We have failed to connect to the server; there is nothin more we will do.");
    }
    doConnect = false;
  }

  // If we are connected to a peer BLE Server, update the characteristic each time we are reached
  // with the current time since boot.
  if (connected) {
    String newValue = "Time since boot: " + String(millis()/1000);
    Serial.println("Setting new characteristic value to \"" + newValue + "\"");

    // Set the characteristic's value to be the array of bytes that is actually a string.
    pRemoteCharacteristic->writeValue(newValue.c_str(), newValue.length());
  }else if(doScan){
    BLEDevice::getScan()->start(0);  // this is just eample to start scan after disconnect, most likely there is better way to do it in arduino
  }

  delay(1000); // Delay a second between loops.
} // End of loop

Quick fix could be to change this block in loop() to

 // If the flag "doConnect" is true then we have scanned for and found the desired
  // BLE Server with which we wish to connect.  Now we connect to it.  Once we are 
  // connected we set the connected flag to be true.
 if (doConnect == true) {
    if (connectToServer()) {
      Serial.println("We are now connected to the BLE Server.");
    } else {
      Serial.println("We have failed to connect to the server; there is nothin more we will do.");
    }
    doConnect = false;
  } else {
    // If we are connected to a peer BLE Server, update the characteristic each time we are reached
    // with the current time since boot.
    if (connected) {
      String newValue = "Time since boot: " + String(millis()/1000);
      Serial.println("Setting new characteristic value to \"" + newValue + "\"");

      // Set the characteristic's value to be the array of bytes that is actually a string.
      pRemoteCharacteristic->writeValue(newValue.c_str(), newValue.length());
    }else if(doScan){
      BLEDevice::getScan()->start(0);  // this is just eample to start scan after disconnect, most likely there is better way to do it in arduino
    }
  }
an-erd commented 5 years ago

After the correction of the code as shown above, I got this results:

Sketch


/**
 * A BLE client example that is rich in capabilities.
 * There is a lot new capabilities implemented.
 * author unknown
 * updated by chegewara
 */

#include "BLEDevice.h"
//#include "BLEScan.h"

// The remote service we wish to connect to.
static BLEUUID serviceUUID("00001814-0000-1000-8000-00805f9b34fb");
// The characteristic of the remote service we are interested in.
static BLEUUID    charUUID("0000ff00-0000-1000-8000-00805f9b34fb");

static boolean doConnect = false;
static boolean connected = false;
static boolean doScan = false;
static BLERemoteCharacteristic* pRemoteCharacteristic;
static BLEAdvertisedDevice* myDevice;

static void notifyCallback(
  BLERemoteCharacteristic* pBLERemoteCharacteristic,
  uint8_t* pData,
  size_t length,
  bool isNotify) {
    Serial.print("Notify callback for characteristic ");
    Serial.print(pBLERemoteCharacteristic->getUUID().toString().c_str());
    Serial.print(" of data length ");
    Serial.println(length);
    Serial.print("data: ");
    Serial.println((char*)pData);
}

class MyClientCallback : public BLEClientCallbacks {
  void onConnect(BLEClient* pclient) {
  }

  void onDisconnect(BLEClient* pclient) {
    connected = false;
    Serial.println("onDisconnect");
  }
};

static void my_gattc_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if, esp_ble_gattc_cb_param_t* param) {
    ESP_LOGW(LOG_TAG, "custom gattc event handler, event: %d", (uint8_t)event);
        if(event == ESP_GATTC_DISCONNECT_EVT) {
                Serial.print("Disconnect reason: "); 
                Serial.println((int)param->disconnect.reason);
        }
}

static void my_gatts_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_t gattc_if, esp_ble_gatts_cb_param_t* param) {
    ESP_LOGI(LOG_TAG, "custom gatts event handler, event: %d", (uint8_t)event);
}

static void my_gap_event_handler(esp_gap_ble_cb_event_t  event, esp_ble_gap_cb_param_t* param) {
    ESP_LOGI(LOG_TAG, "custom gap event handler, event: %d", (uint8_t)event);
}

bool connectToServer() {
    Serial.print("Forming a connection to ");
    Serial.println(myDevice->getAddress().toString().c_str());

    BLEClient*  pClient  = BLEDevice::createClient();
    Serial.println(" - Created client");

    pClient->setClientCallbacks(new MyClientCallback());

    // Connect to the remove BLE Server.
    pClient->connect(myDevice);  // if you pass BLEAdvertisedDevice instead of address, it will be recognized type of peer device address (public or private)
    Serial.println(" - Connected to server");

    // Obtain a reference to the service we are after in the remote BLE server.
    BLERemoteService* pRemoteService = pClient->getService(serviceUUID);
    if (pRemoteService == nullptr) {
      Serial.print("Failed to find our service UUID: ");
      Serial.println(serviceUUID.toString().c_str());
      pClient->disconnect();
      return false;
    }
    Serial.println(" - Found our service");

    // Obtain a reference to the characteristic in the service of the remote BLE server.
    pRemoteCharacteristic = pRemoteService->getCharacteristic(charUUID);
    if (pRemoteCharacteristic == nullptr) {
      Serial.print("Failed to find our characteristic UUID: ");
      Serial.println(charUUID.toString().c_str());
      pClient->disconnect();
      return false;
    }
    Serial.println(" - Found our characteristic");

    // Read the value of the characteristic.
    //if(pRemoteCharacteristic->canRead()) {
    //  std::string value = pRemoteCharacteristic->readValue();
    //  Serial.print("The characteristic value was: ");
    //  Serial.println(value.c_str());
    //}

    if(pRemoteCharacteristic->canNotify())
      pRemoteCharacteristic->registerForNotify(notifyCallback);

    connected = true;

    return true;
}
/**
 * Scan for BLE servers and find the first one that advertises the service we are looking for.
 */
class MyAdvertisedDeviceCallbacks: public BLEAdvertisedDeviceCallbacks {
 /**
   * Called for each advertising BLE server.
   */
  void onResult(BLEAdvertisedDevice advertisedDevice) {
    Serial.print("BLE Advertised Device found: ");
    Serial.println(advertisedDevice.toString().c_str());

    // We have found a device, let us now see if it contains the service we are looking for.
    if (advertisedDevice.haveServiceUUID() && advertisedDevice.isAdvertisingService(serviceUUID)) {

      BLEDevice::getScan()->stop();
      myDevice = new BLEAdvertisedDevice(advertisedDevice);
      doConnect = true;
      doScan = true;

    } // Found our server
  } // onResult
}; // MyAdvertisedDeviceCallbacks

void setup() {
  Serial.begin(115200);
  Serial.println("Starting Arduino BLE Client application...");
  BLEDevice::setCustomGapHandler(my_gap_event_handler);
  BLEDevice::setCustomGattsHandler(my_gatts_event_handler);
  BLEDevice::setCustomGattcHandler(my_gattc_event_handler);

  BLEDevice::init("");

  // Retrieve a Scanner and set the callback we want to use to be informed when we
  // have detected a new device.  Specify that we want active scanning and start the
  // scan to run for 5 seconds.
  BLEScan* pBLEScan = BLEDevice::getScan();
  pBLEScan->setAdvertisedDeviceCallbacks(new MyAdvertisedDeviceCallbacks());
  pBLEScan->setInterval(2000);
  pBLEScan->setWindow(1500);
  pBLEScan->setActiveScan(true);
  pBLEScan->start(15, false);
} // End of setup.

// This is the Arduino main loop function.
void loop() {

  // If the flag "doConnect" is true then we have scanned for and found the desired
  // BLE Server with which we wish to connect.  Now we connect to it.  Once we are 
  // connected we set the connected flag to be true.
  if (doConnect == true) {
    if (connectToServer()) {
      Serial.println("We are now connected to the BLE Server.");
    } else {
      Serial.println("We have failed to connect to the server; there is nothin more we will do.");
    }
    doConnect = false;
  } else {
    // If we are connected to a peer BLE Server, update the characteristic each time we are reached
    // with the current time since boot.
    if (connected) {
      String newValue = "Time since boot: " + String(millis()/1000);
      Serial.println("Setting new characteristic value to \"" + newValue + "\"");

      // Set the characteristic's value to be the array of bytes that is actually a string.
      pRemoteCharacteristic->writeValue(newValue.c_str(), newValue.length());
    }else if(doScan){
      BLEDevice::getScan()->start(0);  // this is just eample to start scan after disconnect, most likely there is better way to do it in arduino
    }
  }

  delay(1000); // Delay a second between loops.
} // End of loop

Log

rst:0x1 (POWERON_RESET),boot:0x17 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:1
load:0x3fff0018,len:4
load:0x3fff001c,len:1100
load:0x40078000,len:9220
load:0x40080400,len:6300
entry 0x400806a4
Starting Arduino BLE Client application...
[D][BLEScan.cpp:204] start(): >> start(duration=15)
[D][FreeRTOS.cpp:165] take(): Semaphore taking: name: ScanEnd (0x3ffdf3ec), owner: <N/A> for start
[D][FreeRTOS.cpp:174] take(): Semaphore taken:  name: ScanEnd (0x3ffdf3ec), owner: start
[D][BLEScan.cpp:236] start(): << start()
[I][BLE_client.ino:59] my_gap_event_handler(): custom gap event handler, event: 2
[I][BLE_client.ino:59] my_gap_event_handler(): custom gap event handler, event: 7
[D][BLEAdvertisedDevice.cpp:424] setRSSI(): - setRSSI(): rssi: -52
[D][BLEAdvertisedDevice.cpp:253] parseAdvertisement(): Type: 0x01 (), length: 1, data: 1a
[D][BLEAdvertisedDevice.cpp:253] parseAdvertisement(): Type: 0xff (), length: 9, data: 4c001005031cad14dd
[D][BLEAdvertisedDevice.cpp:401] setManufacturerData(): - manufacturer data: 4c001005031cad14dd
BLE Advertised Device found: Name: , Address: 72:44:5f:9e:23:96, manufacturer data: 4c001005031cad14dd
[I][BLE_client.ino:59] my_gap_event_handler(): custom gap event handler, event: 3
[D][BLEScan.cpp:106] handleGAPEvent(): Ignoring 72:44:5f:9e:23:96, already seen it.
[I][BLE_client.ino:59] my_gap_event_handler(): custom gap event handler, event: 3
[D][BLEScan.cpp:106] handleGAPEvent(): Ignoring 72:44:5f:9e:23:96, already seen it.
[I][BLE_client.ino:59] my_gap_event_handler(): custom gap event handler, event: 3
[D][BLEAdvertisedDevice.cpp:424] setRSSI(): - setRSSI(): rssi: -59
[D][BLEAdvertisedDevice.cpp:253] parseAdvertisement(): Type: 0x01 (), length: 1, data: 06
[D][BLEAdvertisedDevice.cpp:253] parseAdvertisement(): Type: 0x02 (), length: 4, data: 1418f0ff
[D][BLEAdvertisedDevice.cpp:453] setServiceUUID(): - addServiceUUID(): serviceUUID: 00001814-0000-1000-8000-00805f9b34fb
[D][BLEAdvertisedDevice.cpp:453] setServiceUUID(): - addServiceUUID(): serviceUUID: 0000fff0-0000-1000-8000-00805f9b34fb
[D][BLEAdvertisedDevice.cpp:253] parseAdvertisement(): Type: 0x09 (), length: 15, data: 4d696c6573746f6e65506f64204636
[D][BLEAdvertisedDevice.cpp:413] setName(): - setName(): name: MilestonePod F6
[D][BLEAdvertisedDevice.cpp:253] parseAdvertisement(): Type: 0x12 (), length: 4, data: 10002000
[D][BLEAdvertisedDevice.cpp:349] parseAdvertisement(): Unhandled type: adType: 18 - 0x12
[D][BLEAdvertisedDevice.cpp:253] parseAdvertisement(): Type: 0x0a (), length: 1, data: 00
[D][BLEAdvertisedDevice.cpp:484] setTXPower(): - txPower: 0
BLE Advertised Device found: Name: MilestonePod F6, Address: 50:33:8b:1c:ef:f6, serviceUUID: 00001814-0000-1000-8000-00805f9b34fb, txPower: 0
[D][BLEScan.cpp:259] stop(): >> stop()
[D][BLEScan.cpp:271] stop(): << stop()
[I][BLE_client.ino:59] my_gap_event_handler(): custom gap event handler, event: 3
[I][BLE_client.ino:59] my_gap_event_handler(): custom gap event handler, event: 3
[I][BLE_client.ino:59] my_gap_event_handler(): custom gap event handler, event: 18
Forming a connection to 50:33:8b:1c:ef:f6
[D][BLEDevice.cpp:62] createClient(): >> createClient
[D][BLEDevice.cpp:68] createClient(): << createClient
 - Created client
[D][BLEClient.cpp:103] connect(): >> connect(50:33:8b:1c:ef:f6)
[I][BLEDevice.cpp:596] addPeerDevice(): add conn_id: 0, GATT role: client
[D][FreeRTOS.cpp:165] take(): Semaphore taking: name: RegEvt (0x3ffdf7dc), owner: <N/A> for connect
[D][FreeRTOS.cpp:174] take(): Semaphore taken:  name: RegEvt (0x3ffdf7dc), owner: connect
[D][BLEDevice.cpp:150] gattClientEventHandler(): gattClientEventHandler [esp_gatt_if: 4] ... Unknown
[D][BLEClient.cpp:165] gattClientEventHandler(): gattClientEventHandler [esp_gatt_if: 4] ... Unknown
[W][BLE_client.ino:47] my_gattc_event_handler(): custom gattc event handler, event: 0
[D][FreeRTOS.cpp:165] take(): Semaphore taking: name: OpenEvt (0x3ffdf934), owner: <N/A> for connect
[D][FreeRTOS.cpp:174] take(): Semaphore taken:  name: OpenEvt (0x3ffdf934), owner: connect
[D][BLEDevice.cpp:150] gattClientEventHandler(): gattClientEventHandler [esp_gatt_if: 4] ... Unknown
[D][BLEClient.cpp:165] gattClientEventHandler(): gattClientEventHandler [esp_gatt_if: 4] ... Unknown
[D][BLEDevice.cpp:580] updatePeerDevice(): update conn_id: 4, GATT role: client
[W][BLE_client.ino:47] my_gattc_event_handler(): custom gattc event handler, event: 40
[D][BLEDevice.cpp:150] gattClientEventHandler(): gattClientEventHandler [esp_gatt_if: 4] ... Unknown
[D][BLEClient.cpp:165] gattClientEventHandler(): gattClientEventHandler [esp_gatt_if: 4] ... Unknown
[D][BLEClient.cpp:136] connect(): << connect(), rc=1
 - Connected to server
[W][BLE_client.ino:47] my_gattc_event_handler(): custom gattc event handler, event: 2
[D][BLEClient.cpp:383] getService(): >> getService: uuid: 00001814-0000-1000-8000-00805f9b34fb
[D][BLEClient.cpp:419] getServices(): >> getServices
[D][BLEClient.cpp:78] clearServices(): >> clearServices
[D][BLEClient.cpp:85] clearServices(): << clearServices
[D][FreeRTOS.cpp:165] take(): Semaphore taking: name: SearchCmplEvt (0x3ffdf994), owner: <N/A> for getServices
[D][FreeRTOS.cpp:174] take(): Semaphore taken:  name: SearchCmplEvt (0x3ffdf994), owner: getServices
[D][BLEDevice.cpp:150] gattClientEventHandler(): gattClientEventHandler [esp_gatt_if: 4] ... Unknown
[D][BLEClient.cpp:165] gattClientEventHandler(): gattClientEventHandler [esp_gatt_if: 4] ... Unknown
[D][BLERemoteService.cpp:32] BLERemoteService(): >> BLERemoteService()
[D][BLERemoteService.cpp:40] BLERemoteService(): << BLERemoteService()
[W][BLE_client.ino:47] my_gattc_event_handler(): custom gattc event handler, event: 7
[D][BLEDevice.cpp:150] gattClientEventHandler(): gattClientEventHandler [esp_gatt_if: 4] ... Unknown
[D][BLEClient.cpp:165] gattClientEventHandler(): gattClientEventHandler [esp_gatt_if: 4] ... Unknown
[D][BLERemoteService.cpp:32] BLERemoteService(): >> BLERemoteService()
[D][BLERemoteService.cpp:40] BLERemoteService(): << BLERemoteService()
[W][BLE_client.ino:47] my_gattc_event_handler(): custom gattc event handler, event: 7
[D][BLEDevice.cpp:150] gattClientEventHandler(): gattClientEventHandler [esp_gatt_if: 4] ... Unknown
[D][BLEClient.cpp:165] gattClientEventHandler(): gattClientEventHandler [esp_gatt_if: 4] ... Unknown
[D][BLERemoteService.cpp:32] BLERemoteService(): >> BLERemoteService()
[D][BLERemoteService.cpp:40] BLERemoteService(): << BLERemoteService()
[W][BLE_client.ino:47] my_gattc_event_handler(): custom gattc event handler, event: 7
[D][BLEDevice.cpp:150] gattClientEventHandler(): gattClientEventHandler [esp_gatt_if: 4] ... Unknown
[D][BLEClient.cpp:165] gattClientEventHandler(): gattClientEventHandler [esp_gatt_if: 4] ... Unknown
[D][BLERemoteService.cpp:32] BLERemoteService(): >> BLERemoteService()
[D][BLERemoteService.cpp:40] BLERemoteService(): << BLERemoteService()
[W][BLE_client.ino:47] my_gattc_event_handler(): custom gattc event handler, event: 7
[D][BLEDevice.cpp:150] gattClientEventHandler(): gattClientEventHandler [esp_gatt_if: 4] ... Unknown
[D][BLEClient.cpp:165] gattClientEventHandler(): gattClientEventHandler [esp_gatt_if: 4] ... Unknown
[D][BLERemoteService.cpp:32] BLERemoteService(): >> BLERemoteService()
[D][BLERemoteService.cpp:40] BLERemoteService(): << BLERemoteService()
[W][BLE_client.ino:47] my_gattc_event_handler(): custom gattc event handler, event: 7
[D][BLEDevice.cpp:150] gattClientEventHandler(): gattClientEventHandler [esp_gatt_if: 4] ... Unknown
[D][BLEClient.cpp:165] gattClientEventHandler(): gattClientEventHandler [esp_gatt_if: 4] ... Unknown
[D][BLERemoteService.cpp:32] BLERemoteService(): >> BLERemoteService()
[D][BLERemoteService.cpp:40] BLERemoteService(): << BLERemoteService()
[W][BLE_client.ino:47] my_gattc_event_handler(): custom gattc event handler, event: 7
[D][BLEDevice.cpp:150] gattClientEventHandler(): gattClientEventHandler [esp_gatt_if: 4] ... Unknown
[D][BLEClient.cpp:165] gattClientEventHandler(): gattClientEventHandler [esp_gatt_if: 4] ... Unknown
[D][BLEClient.cpp:436] getServices(): << getServices
[W][BLE_client.ino:47] my_gattc_event_handler(): custom gattc event handler, event: 6
[D][BLEClient.cpp:396] getService(): << getService: found the service with uuid: 00001814-0000-1000-8000-00805f9b34fb
[D][BLEDevice.cpp:150] gattClientEventHandler(): gattClientEventHandler [esp_gatt_if: 4] ... Unknown
 - Found our service
[D][BLEClient.cpp:165] gattClientEventHandler(): gattClientEventHandler [esp_gatt_if: 4] ... Unknown
[D][BLERemoteService.cpp:168] retrieveCharacteristics(): >> getCharacteristics() for service: 00001814-0000-1000-8000-00805f9b34fb
[W][BLE_client.ino:47] my_gattc_event_handler(): custom gattc event handler, event: 18
[D][BLERemoteService.cpp:199] retrieveCharacteristics(): Found a characteristic: Handle: 18, UUID: 00002a53-0000-1000-8000-00805f9b34fb
[D][BLERemoteCharacteristic.cpp:43] BLERemoteCharacteristic(): >> BLERemoteCharacteristic: handle: 18 0x18, uuid: 00002a53-0000-1000-8000-00805f9b34fb
[D][BLERemoteCharacteristic.cpp:256] retrieveDescriptors(): >> retrieveDescriptors() for characteristic: 00002a53-0000-1000-8000-00805f9b34fb
[D][BLERemoteCharacteristic.cpp:286] retrieveDescriptors(): Found a descriptor: Handle: 19, UUID: 00002902-0000-1000-8000-00805f9b34fb
[D][BLERemoteCharacteristic.cpp:300] retrieveDescriptors(): << retrieveDescriptors(): Found 1 descriptors.
[D][BLERemoteCharacteristic.cpp:51] BLERemoteCharacteristic(): << BLERemoteCharacteristic
[D][BLERemoteService.cpp:199] retrieveCharacteristics(): Found a characteristic: Handle: 21, UUID: 00002a54-0000-1000-8000-00805f9b34fb
[D][BLERemoteCharacteristic.cpp:43] BLERemoteCharacteristic(): >> BLERemoteCharacteristic: handle: 21 0x21, uuid: 00002a54-0000-1000-8000-00805f9b34fb
[D][BLERemoteCharacteristic.cpp:256] retrieveDescriptors(): >> retrieveDescriptors() for characteristic: 00002a54-0000-1000-8000-00805f9b34fb
[E][BLERemoteCharacteristic.cpp:280] retrieveDescriptors(): esp_ble_gattc_get_all_descr: Unknown
[D][BLERemoteCharacteristic.cpp:300] retrieveDescriptors(): << retrieveDescriptors(): Found 0 descriptors.
[D][BLERemoteCharacteristic.cpp:51] BLERemoteCharacteristic(): << BLERemoteCharacteristic
[D][BLERemoteService.cpp:199] retrieveCharacteristics(): Found a characteristic: Handle: 23, UUID: 00002a5d-0000-1000-8000-00805f9b34fb
[D][BLERemoteCharacteristic.cpp:43] BLERemoteCharacteristic(): >> BLERemoteCharacteristic: handle: 23 0x23, uuid: 00002a5d-0000-1000-8000-00805f9b34fb
[D][BLERemoteCharacteristic.cpp:256] retrieveDescriptors(): >> retrieveDescriptors() for characteristic: 00002a5d-0000-1000-8000-00805f9b34fb
[E][BLERemoteCharacteristic.cpp:280] retrieveDescriptors(): esp_ble_gattc_get_all_descr: Unknown
[D][BLERemoteCharacteristic.cpp:300] retrieveDescriptors(): << retrieveDescriptors(): Found 0 descriptors.
[D][BLERemoteCharacteristic.cpp:51] BLERemoteCharacteristic(): << BLERemoteCharacteristic
[D][BLERemoteService.cpp:199] retrieveCharacteristics(): Found a characteristic: Handle: 25, UUID: 00002a55-0000-1000-8000-00805f9b34fb
[D][BLERemoteCharacteristic.cpp:43] BLERemoteCharacteristic(): >> BLERemoteCharacteristic: handle: 25 0x25, uuid: 00002a55-0000-1000-8000-00805f9b34fb
[D][BLERemoteCharacteristic.cpp:256] retrieveDescriptors(): >> retrieveDescriptors() for characteristic: 00002a55-0000-1000-8000-00805f9b34fb
[D][BLERemoteCharacteristic.cpp:286] retrieveDescriptors(): Found a descriptor: Handle: 26, UUID: 00002902-0000-1000-8000-00805f9b34fb
[D][BLERemoteCharacteristic.cpp:300] retrieveDescriptors(): << retrieveDescriptors(): Found 1 descriptors.
[D][BLERemoteCharacteristic.cpp:51] BLERemoteCharacteristic(): << BLERemoteCharacteristic
[D][BLERemoteService.cpp:199] retrieveCharacteristics(): Found a characteristic: Handle: 28, UUID: 0000ff00-0000-1000-8000-00805f9b34fb
[D][BLERemoteCharacteristic.cpp:43] BLERemoteCharacteristic(): >> BLERemoteCharacteristic: handle: 28 0x28, uuid: 0000ff00-0000-1000-8000-00805f9b34fb
[D][BLERemoteCharacteristic.cpp:256] retrieveDescriptors(): >> retrieveDescriptors() for characteristic: 0000ff00-0000-1000-8000-00805f9b34fb
[D][BLERemoteCharacteristic.cpp:286] retrieveDescriptors(): Found a descriptor: Handle: 29, UUID: 00002902-0000-1000-8000-00805f9b34fb
[D][BLERemoteCharacteristic.cpp:300] retrieveDescriptors(): << retrieveDescriptors(): Found 1 descriptors.
[D][BLERemoteCharacteristic.cpp:51] BLERemoteCharacteristic(): << BLERemoteCharacteristic
[D][BLERemoteService.cpp:215] retrieveCharacteristics(): << getCharacteristics()
 - Found our characteristic
[D][BLERemoteCharacteristic.cpp:444] registerForNotify(): >> registerForNotify(): Characteristic: uuid: 0000ff00-0000-1000-8000-00805f9b34fb, handle: 28 0x1c, props: broadcast: 0, read: 0, write_nr: 0, write: 0, notify: 1, indicate: 0, auth: 0
[D][FreeRTOS.cpp:165] take(): Semaphore taking: name: RegForNotifyEvt (0x3ffe383c), owner: <N/A> for registerForNotify
[D][FreeRTOS.cpp:174] take(): Semaphore taken:  name: RegForNotifyEvt (0x3ffe383c), owner: registerForNotify
[D][BLEDevice.cpp:150] gattClientEventHandler(): gattClientEventHandler [esp_gatt_if: 4] ... Unknown
[D][BLERemoteCharacteristic.cpp:329] getDescriptor(): >> getDescriptor: uuid: 00002902-0000-1000-8000-00805f9b34fb
[D][BLEClient.cpp:165] gattClientEventHandler(): gattClientEventHandler [esp_gatt_if: 4] ... Unknown
[D][BLERemoteCharacteristic.cpp:333] getDescriptor(): << getDescriptor: found
[W][BLE_client.ino:47] my_gattc_event_handler(): custom gattc event handler, event: 38
[D][BLERemoteDescriptor.cpp:138] writeValue(): >> writeValue: handle: 29, uuid: 00002902-0000-1000-8000-00805f9b34fb
[D][BLERemoteDescriptor.cpp:157] writeValue(): << writeValue
[D][BLEDevice.cpp:150] gattClientEventHandler(): gattClientEventHandler [esp_gatt_if: 4] ... Unknown
[D][BLERemoteCharacteristic.cpp:484] registerForNotify(): << registerForNotify()

Exception

Guru Meditation Error: Core  1 panic'ed (StoreProhibited). Exception was unhandled.
Core 1 register dump:
PC      : 0x400d1865  PS      : 0x00060130  A0      : 0x800d1f02  A1      : 0x3ffca580  
A2      : 0x00000001  A3      : 0x01024bc8  A4      : 0x00000009  A5      : 0x3ffc6314  
A6      : 0x3ffe2854  A7      : 0x00fb0010  A8      : 0x800d1dc4  A9      : 0x3ffca500  
A10     : 0x01024bc8  A11     : 0x01024bc8  A12     : 0x3ffc6314  A13     : 0x3f4025b9  
A14     : 0x3f402b07  A15     : 0x3ffe369c  SAR     : 0x00000004  EXCCAUSE: 0x0000001d  
EXCVADDR: 0x00000001  LBEG    : 0x400014fd  LEND    : 0x4000150d  LCOUNT  : 0xfffffffb  

Backtrace: 0x400d1865:0x3ffca580 0x400d1eff:0x3ffca5b0 0x401903f0:0x3ffca620 0x40092ded:0x3ffca640

Decoded

PC: 0x400d1865: std::__cxx11::basic_string  , std::allocator  >::basic_string(char const*, std::allocator  const&) at c:\users\akaem\documents\arduino\hardware\espressif\esp32\tools\xtensa-esp32-elf\xtensa-esp32-elf\include\c++\5.2.0\bits/basic_string.h line 109
EXCVADDR: 0x00000001

Decoding stack results
0x400d1865: std::__cxx11::basic_string  , std::allocator  >::basic_string(char const*, std::allocator  const&) at c:\users\akaem\documents\arduino\hardware\espressif\esp32\tools\xtensa-esp32-elf\xtensa-esp32-elf\include\c++\5.2.0\bits/basic_string.h line 109
0x400d1eff: loop() at C:\Users\AKAEM\Documents\Arduino\My Examples\BLE_client/BLE_client.ino line 177
0x401903f0: loopTask(void*) at C:\Users\AKAEM\Documents\Arduino\hardware\espressif\esp32\cores\esp32\main.cpp line 17
0x40092ded: vPortTaskWrapper at /Users/ficeto/Desktop/ESP32/ESP32/esp-idf-public/components/freertos/port.c line 143
chegewara commented 5 years ago

Sorry for all issues with library. Before i decided to merge all changes i tested it very intensive with connect/disconnect performance tests. But looks like my tests were not comprehensive and didnt catch some issues.

I will try to fix all issues asap, but it can take few days maybe even more, since i have day job, upwork job and not so much time. I appreciate all posted issues and logs with observations which gives me knowledge what is going on and what needs to be fixed, even if not always it is obvious.

Thanks again and sorry.