I'm able to connect to server & find the characteristic.
If I don't read the characteristic, I'm able to connect & then disconnect without any issue.
If I read the characteristic value & then try to disconnect, I get the following error:
Code: Select all
00:02:33.902 -> CORRUPT HEAP: Bad head at 0x3ffdec90. Expected 0xabba1234 got 0x3ffdef3c
00:02:33.902 -> assertion "head != NULL" failed: file "/home/runner/work/esp32-arduino-lib-builder/esp32-arduino-lib-builder/esp-idf/components/heap/multi_heap_poisoning.c", line 214, function: multi_heap_free
00:02:33.937 -> abort() was called at PC 0x400e6de3 on core 0
00:02:33.937 ->
00:02:33.937 -> Backtrace: 0x40091448:0x3ffd15d0 0x40091679:0x3ffd15f0 0x400e6de3:0x3ffd1610 0x400910d5:0x3ffd1640 0x40084b26:0x3ffd1660 0x400850e1:0x3ffd1680 0x4000bec7:0x3ffd16a0 0x400fd0d9:0x3ffd16c0 0x400fcc1a:0x3ffd16e0 0x400fccdc:0x3ffd1700 0x40136943:0x3ffd1720 0x40138242:0x3ffd1740 0x40135fe6:0x3ffd19d0 0x40136125:0x3ffd19f0 0x4013e662:0x3ffd1a10 0x401136ca:0x3ffd1a30 0x4008e089:0x3ffd1a60
00:02:33.971 ->
00:02:33.971 -> Rebooting...
00:02:33.971 -> ets Jun 8 2016 00:22:57
I've got 2 lines below indicated with comments starting with "// ***" that show where my problem specifically lies.
Full code below:
Code: Select all
#include "BLEDevice.h"
// The remote service we wish to connect to.
#define SERVICE_UUID "59f30011-a621-11e1-802a-5aa538984bd1"
static BLEUUID serviceUUID(SERVICE_UUID);
// The characteristic of the remote service we are interested in.
#define CHARACTERISTIC_UUID "98e38b21-a622-11e1-802a-5aa538984bd1"
static BLEUUID charUUID(CHARACTERISTIC_UUID);
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");
}
};
bool connectToServer() {
Serial.print("Forming a connection to ");
Serial.println(myDevice->getAddress().toString().c_str());
BLEClient* pClient = BLEDevice::createClient();
Serial.println(" - Created client");
MyClientCallback* tmpMyClientCallback = new MyClientCallback();
pClient->setClientCallbacks(tmpMyClientCallback);
// Connect to the remote 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(); // *** if I comment this out, disconnects correctly.
// Serial.print("The characteristic value was: ");
// Serial.println(value.c_str());
}
pClient->disconnect(); // *** if I comment this out, does not fail, but eventually runs out of memory/stops looping
connected = false; //true;
return false; //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 Client");
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(1349);
pBLEScan->setWindow(449);
pBLEScan->setActiveScan(true);
pBLEScan->start(5, false);
} // End of setup.
void loop() {
Serial.println("Begin loop...");
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 (connected) {
std::string oldValue = pRemoteCharacteristic->readValue();
Serial.print("Old characteristic value \"");
Serial.print(oldValue.c_str());
Serial.println("\"");
String newValue = "Time since boot: " + String(millis()/1000);
Serial.println("Setting new characteristic value to \"" + newValue + "\"");
}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.
Serial.println("End loop...");
} // End of loop