BLE consistent connection
Posted: Fri Aug 16, 2019 8:31 am
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.
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;
}