Page 1 of 1

BLE consistent connection

Posted: Fri Aug 16, 2019 8:31 am
by puddletowntom
I have been using the Arduino IDE for testing the Bluetooth on the ESP32. I'm using the BLE_Client and BLE_Scan examples from the ESP32 package in Arduino. I have been able to successfully pair with my device. However on some occasions when I try to pair it fails and freezes the program.

eg a successful connection will look like the following,

Forming a connection to 66:12:0d:a9:03:34
- Created client
- Connected to server
- Found our service
- Found our characteristic

an unsuccessful connection will look like,

Forming a connection to 66:12:0d:a9:03:34
- Created client
onDisconnect

When it does fail to connect, it seems to be on the line pClient->connect(myDevice);

If I could trap this fail it would prevent the code from locking up. I'm not sure why it can't continue from here but I will continue to look into it. Hopefully someone has advice for speeding this fix along.

Code: Select all

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

Re: BLE consistent connection

Posted: Fri Aug 16, 2019 1:25 pm
by chegewara
Hi.
What version of arduino-esp32 are you using?
Please check if you have this patch included:
https://github.com/espressif/arduino-es ... 2974/files

Re: BLE consistent connection

Posted: Mon Aug 19, 2019 1:51 pm
by puddletowntom
So I actually seemed to be able to work around this problem with the help of the link below.

I haven't updated the version yet but I have replaced the BLE files from the BLE folder with the files from the cpp_util folder from esp32-snippets this morning. So now whenever there is a failed connection the program is able to continue on after without hanging.

https://github.com/nkolban/esp32-snippets/issues/786

I have just realised the solution is coming from the same user name as you ha. I assume you are the same person? If you are the same chegewara that helped me get this going would you recommend the newer arduino-esp32 version then? I was using v1.0.1 from a while back. I would have downloaded it back around last January/Feburary.

Is the solution from the link above enough to cover the semaphore issue?

I might just go ahead and update the version anyway.

Re: BLE consistent connection

Posted: Mon Aug 19, 2019 5:38 pm
by chegewara
Well, it seems that i am indeed the same chegewara, i hope at least.

I think the issue with semaphores, at least most of it, should be fixed with PR from above link.

Re: BLE consistent connection

Posted: Tue Aug 20, 2019 3:27 pm
by puddletowntom
Ok cool. I think I will leave it alone for now anyway "If it aint broke don't fix it".

Thank you for your help.