Skip to content

Commit

Permalink
more reconnect stability improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
doudar committed Mar 5, 2024
1 parent 8b8dfb8 commit 0e3b8de
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 19 deletions.
2 changes: 1 addition & 1 deletion include/settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@
#define DEFAULT_SCAN_DURATION 5

// BLE automatic reconnect duration. Set this low to avoid interruption.
#define BLE_RECONNECT_SCAN_DURATION 15
#define BLE_RECONNECT_SCAN_DURATION 5

//Task Stack Sizes
#define MAIN_STACK 4500
Expand Down
13 changes: 4 additions & 9 deletions src/BLE_Client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ void MyClientCallback::onConnect(NimBLEClient *pClient) {
}

void MyClientCallback::onDisconnect(NimBLEClient *pClient) {
NimBLEDevice::getScan()->stop();
// NimBLEDevice::getScan()->stop();
// NimBLEDevice::getScan()->clearResults();
// NimBLEDevice::getScan()->clearDuplicateCache();
if (!pClient->isConnected()) {
Expand All @@ -338,20 +338,15 @@ void MyClientCallback::onDisconnect(NimBLEClient *pClient) {
(spinBLEClient.myBLEDevices[i].charUUID == CYCLINGPOWERSERVICE_UUID)) {
SS2K_LOG(BLE_CLIENT_LOG_TAG, "Deregistered PM on Disconnect");
rtConfig->pm_batt.setValue(0);
spinBLEClient.connectedPM = false;
break;
}
if ((spinBLEClient.myBLEDevices[i].charUUID == HEARTCHARACTERISTIC_UUID)) {
SS2K_LOG(BLE_CLIENT_LOG_TAG, "Deregistered HR on Disconnect");
rtConfig->hr_batt.setValue(0);
spinBLEClient.connectedHRM = false;
break;
}
if ((spinBLEClient.myBLEDevices[i].charUUID == HID_REPORT_DATA_UUID)) {
SS2K_LOG(BLE_CLIENT_LOG_TAG, "Deregistered Remote on Disconnect");
spinBLEClient.connectedRemote = false;
break;
}
spinBLEClient.myBLEDevices[i].reset();
}
}
return;
Expand Down Expand Up @@ -439,6 +434,8 @@ void SpinBLEClient::scanProcess(int duration) {
SS2K_LOGW(BLE_CLIENT_LOG_TAG, "Scanning for BLE servers and putting them into a list...");

BLEScan *pBLEScan = BLEDevice::getScan();
pBLEScan->clearDuplicateCache();
pBLEScan->clearResults();
pBLEScan->setAdvertisedDeviceCallbacks(&myAdvertisedDeviceCallbacks);
pBLEScan->setInterval(49); // 97
pBLEScan->setWindow(33); // 67
Expand Down Expand Up @@ -480,9 +477,7 @@ void SpinBLEClient::scanProcess(int duration) {

String output;
serializeJson(devices, output);
if (duration > BLE_RECONNECT_SCAN_DURATION) {
SS2K_LOG(BLE_CLIENT_LOG_TAG, "Bluetooth Client Found Devices: %s", output.c_str());
}
#ifdef USE_TELEGRAM
SEND_TO_TELEGRAM("Bluetooth Client Found Devices: " + output);
#endif
Expand Down
18 changes: 9 additions & 9 deletions src/BLE_Common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ TaskHandle_t BLECommunicationTask;

void BLECommunications(void *pvParameters) {
for (;;) {
if (!spinBLEClient.dontBlockScan) {
NimBLEDevice::getScan()->stop(); // stop routine scans
}
// if (!spinBLEClient.dontBlockScan) {
// NimBLEDevice::getScan()->stop(); // stop routine scans
// }
// **********************************Client***************************************
for (size_t x = 0; x < NUM_BLE_DEVICES; x++) { // loop through discovered devices
if (spinBLEClient.myBLEDevices[x].connectedClientID != BLE_HS_CONN_HANDLE_NONE) {
Expand Down Expand Up @@ -68,12 +68,12 @@ void BLECommunications(void *pvParameters) {

} else if (!pClient->isConnected()) { // This shouldn't ever be
// called...
if (pClient->disconnect() == 0) { // 0 is a successful disconnect
BLEDevice::deleteClient(pClient);
vTaskDelay(100 / portTICK_PERIOD_MS);
SS2K_LOG(BLE_COMMON_LOG_TAG, "Workaround connect");
myAdvertisedDevice.doConnect = true;
}
// if (pClient->disconnect() == 0) { // 0 is a successful disconnect
// BLEDevice::deleteClient(pClient);
// vTaskDelay(100 / portTICK_PERIOD_MS);
SS2K_LOG(BLE_COMMON_LOG_TAG, "Workaround connect");
myAdvertisedDevice.doConnect = true;
//}
}
}
}
Expand Down

0 comments on commit 0e3b8de

Please sign in to comment.