refactor: trying to improve connection to BLE devices

This commit is contained in:
Yurii
2025-01-24 03:17:14 +03:00
parent 666786fd65
commit c524abd959

View File

@@ -445,6 +445,14 @@ protected:
} }
pClient = NimBLEDevice::createClient(); pClient = NimBLEDevice::createClient();
/**
* Set initial connection parameters:
* These settings are safe for 3 clients to connect reliably, can go faster if you have less
* connections. Timeout should be a multiple of the interval, minimum is 100ms.
* Min interval: 12 * 1.25ms = 15, Max interval: 12 * 1.25ms = 15, 0 latency, 150 * 10ms = 1500ms timeout
*/
pClient->setConnectionParams(12, 12, 0, 150);
pClient->setConnectTimeout(5000); pClient->setConnectTimeout(5000);
pClient->setSelfDelete(false, true); pClient->setSelfDelete(false, true);
} }
@@ -495,13 +503,16 @@ protected:
NimBLEUUID charUuid((uint16_t) 0x2A6E); NimBLEUUID charUuid((uint16_t) 0x2A6E);
pChar = pService->getCharacteristic(charUuid); pChar = pService->getCharacteristic(charUuid);
if (pChar && pChar->canNotify()) { if (pChar && (pChar->canNotify() || pChar->canIndicate())) {
Log.straceln( Log.straceln(
FPSTR(L_SENSORS_BLE), F("Sensor #%hhu '%s': found temp char (%s) in env service on device %s"), FPSTR(L_SENSORS_BLE), F("Sensor #%hhu '%s': found temp char (%s) in env service on device %s"),
sensorId, sSensor.name, charUuid.toString().c_str(), address.toString().c_str() sensorId, sSensor.name, charUuid.toString().c_str(), address.toString().c_str()
); );
tempNotifyCreated = pChar->subscribe(true, [sensorId](NimBLERemoteCharacteristic* pChar, uint8_t* pData, size_t length, bool isNotify) { pChar->unsubscribe();
tempNotifyCreated = pChar->subscribe(
pChar->canNotify(),
[sensorId](NimBLERemoteCharacteristic* pChar, uint8_t* pData, size_t length, bool isNotify) {
if (pChar == nullptr) { if (pChar == nullptr) {
return; return;
} }
@@ -543,7 +554,8 @@ protected:
// update rssi // update rssi
Sensors::setValueById(sensorId, pClient->getRssi(), Sensors::ValueType::RSSI, false, false); Sensors::setValueById(sensorId, pClient->getRssi(), Sensors::ValueType::RSSI, false, false);
}); }
);
if (tempNotifyCreated) { if (tempNotifyCreated) {
Log.straceln( Log.straceln(
@@ -568,13 +580,16 @@ protected:
NimBLEUUID charUuid((uint16_t) 0x2A1F); NimBLEUUID charUuid((uint16_t) 0x2A1F);
pChar = pService->getCharacteristic(charUuid); pChar = pService->getCharacteristic(charUuid);
if (pChar && pChar->canNotify()) { if (pChar && (pChar->canNotify() || pChar->canIndicate())) {
Log.straceln( Log.straceln(
FPSTR(L_SENSORS_BLE), F("Sensor #%hhu '%s': found temp char (%s) in env service on device %s"), FPSTR(L_SENSORS_BLE), F("Sensor #%hhu '%s': found temp char (%s) in env service on device %s"),
sensorId, sSensor.name, charUuid.toString().c_str(), address.toString().c_str() sensorId, sSensor.name, charUuid.toString().c_str(), address.toString().c_str()
); );
tempNotifyCreated = pChar->subscribe(true, [sensorId](NimBLERemoteCharacteristic* pChar, uint8_t* pData, size_t length, bool isNotify) { pChar->unsubscribe();
tempNotifyCreated = pChar->subscribe(
pChar->canNotify(),
[sensorId](NimBLERemoteCharacteristic* pChar, uint8_t* pData, size_t length, bool isNotify) {
if (pChar == nullptr) { if (pChar == nullptr) {
return; return;
} }
@@ -616,7 +631,8 @@ protected:
// update rssi // update rssi
Sensors::setValueById(sensorId, pClient->getRssi(), Sensors::ValueType::RSSI, false, false); Sensors::setValueById(sensorId, pClient->getRssi(), Sensors::ValueType::RSSI, false, false);
}); }
);
if (tempNotifyCreated) { if (tempNotifyCreated) {
Log.straceln( Log.straceln(
@@ -653,13 +669,16 @@ protected:
NimBLEUUID charUuid((uint16_t) 0x2A6F); NimBLEUUID charUuid((uint16_t) 0x2A6F);
pChar = pService->getCharacteristic(charUuid); pChar = pService->getCharacteristic(charUuid);
if (pChar && pChar->canNotify()) { if (pChar && (pChar->canNotify() || pChar->canIndicate())) {
Log.straceln( Log.straceln(
FPSTR(L_SENSORS_BLE), F("Sensor #%hhu '%s': found humidity char (%s) in env service on device %s"), FPSTR(L_SENSORS_BLE), F("Sensor #%hhu '%s': found humidity char (%s) in env service on device %s"),
sensorId, sSensor.name, charUuid.toString().c_str(), address.toString().c_str() sensorId, sSensor.name, charUuid.toString().c_str(), address.toString().c_str()
); );
humidityNotifyCreated = pChar->subscribe(true, [sensorId](NimBLERemoteCharacteristic* pChar, uint8_t* pData, size_t length, bool isNotify) { pChar->unsubscribe();
humidityNotifyCreated = pChar->subscribe(
pChar->canNotify(),
[sensorId](NimBLERemoteCharacteristic* pChar, uint8_t* pData, size_t length, bool isNotify) {
if (pChar == nullptr) { if (pChar == nullptr) {
return; return;
} }
@@ -701,7 +720,8 @@ protected:
// update rssi // update rssi
Sensors::setValueById(sensorId, pClient->getRssi(), Sensors::ValueType::RSSI, false, false); Sensors::setValueById(sensorId, pClient->getRssi(), Sensors::ValueType::RSSI, false, false);
}); }
);
if (humidityNotifyCreated) { if (humidityNotifyCreated) {
Log.straceln( Log.straceln(
@@ -752,13 +772,16 @@ protected:
NimBLEUUID charUuid((uint16_t) 0x2A19); NimBLEUUID charUuid((uint16_t) 0x2A19);
pChar = pService->getCharacteristic(charUuid); pChar = pService->getCharacteristic(charUuid);
if (pChar && pChar->canNotify()) { if (pChar && (pChar->canNotify() || pChar->canIndicate())) {
Log.straceln( Log.straceln(
FPSTR(L_SENSORS_BLE), F("Sensor #%hhu '%s': found battery char (%s) in battery service on device %s"), FPSTR(L_SENSORS_BLE), F("Sensor #%hhu '%s': found battery char (%s) in battery service on device %s"),
sensorId, sSensor.name, charUuid.toString().c_str(), address.toString().c_str() sensorId, sSensor.name, charUuid.toString().c_str(), address.toString().c_str()
); );
batteryNotifyCreated = pChar->subscribe(true, [sensorId](NimBLERemoteCharacteristic* pChar, uint8_t* pData, size_t length, bool isNotify) { pChar->unsubscribe();
batteryNotifyCreated = pChar->subscribe(
pChar->canNotify(),
[sensorId](NimBLERemoteCharacteristic* pChar, uint8_t* pData, size_t length, bool isNotify) {
if (pChar == nullptr) { if (pChar == nullptr) {
return; return;
} }
@@ -791,7 +814,7 @@ protected:
auto rawBattery = pChar->getValue<uint8_t>(); auto rawBattery = pChar->getValue<uint8_t>();
Log.straceln( Log.straceln(
FPSTR(L_SENSORS_BLE), FPSTR(L_SENSORS_BLE),
F("Sensor #%hhu '%s': received battery: %.2f"), F("Sensor #%hhu '%s': received battery: %hhu"),
sensorId, sSensor.name, rawBattery sensorId, sSensor.name, rawBattery
); );
@@ -800,7 +823,8 @@ protected:
// update rssi // update rssi
Sensors::setValueById(sensorId, pClient->getRssi(), Sensors::ValueType::RSSI, false, false); Sensors::setValueById(sensorId, pClient->getRssi(), Sensors::ValueType::RSSI, false, false);
}); }
);
if (batteryNotifyCreated) { if (batteryNotifyCreated) {
Log.straceln( Log.straceln(