mirror of
https://github.com/Laxilef/OTGateway.git
synced 2025-12-10 18:24:27 +05:00
refactor: trying to improve connection to BLE devices
This commit is contained in:
@@ -445,6 +445,14 @@ protected:
|
||||
}
|
||||
|
||||
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->setSelfDelete(false, true);
|
||||
}
|
||||
@@ -495,13 +503,16 @@ protected:
|
||||
NimBLEUUID charUuid((uint16_t) 0x2A6E);
|
||||
pChar = pService->getCharacteristic(charUuid);
|
||||
|
||||
if (pChar && pChar->canNotify()) {
|
||||
if (pChar && (pChar->canNotify() || pChar->canIndicate())) {
|
||||
Log.straceln(
|
||||
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()
|
||||
);
|
||||
|
||||
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) {
|
||||
return;
|
||||
}
|
||||
@@ -543,7 +554,8 @@ protected:
|
||||
|
||||
// update rssi
|
||||
Sensors::setValueById(sensorId, pClient->getRssi(), Sensors::ValueType::RSSI, false, false);
|
||||
});
|
||||
}
|
||||
);
|
||||
|
||||
if (tempNotifyCreated) {
|
||||
Log.straceln(
|
||||
@@ -568,13 +580,16 @@ protected:
|
||||
NimBLEUUID charUuid((uint16_t) 0x2A1F);
|
||||
pChar = pService->getCharacteristic(charUuid);
|
||||
|
||||
if (pChar && pChar->canNotify()) {
|
||||
if (pChar && (pChar->canNotify() || pChar->canIndicate())) {
|
||||
Log.straceln(
|
||||
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()
|
||||
);
|
||||
|
||||
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) {
|
||||
return;
|
||||
}
|
||||
@@ -616,7 +631,8 @@ protected:
|
||||
|
||||
// update rssi
|
||||
Sensors::setValueById(sensorId, pClient->getRssi(), Sensors::ValueType::RSSI, false, false);
|
||||
});
|
||||
}
|
||||
);
|
||||
|
||||
if (tempNotifyCreated) {
|
||||
Log.straceln(
|
||||
@@ -653,13 +669,16 @@ protected:
|
||||
NimBLEUUID charUuid((uint16_t) 0x2A6F);
|
||||
pChar = pService->getCharacteristic(charUuid);
|
||||
|
||||
if (pChar && pChar->canNotify()) {
|
||||
if (pChar && (pChar->canNotify() || pChar->canIndicate())) {
|
||||
Log.straceln(
|
||||
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()
|
||||
);
|
||||
|
||||
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) {
|
||||
return;
|
||||
}
|
||||
@@ -701,7 +720,8 @@ protected:
|
||||
|
||||
// update rssi
|
||||
Sensors::setValueById(sensorId, pClient->getRssi(), Sensors::ValueType::RSSI, false, false);
|
||||
});
|
||||
}
|
||||
);
|
||||
|
||||
if (humidityNotifyCreated) {
|
||||
Log.straceln(
|
||||
@@ -752,13 +772,16 @@ protected:
|
||||
NimBLEUUID charUuid((uint16_t) 0x2A19);
|
||||
pChar = pService->getCharacteristic(charUuid);
|
||||
|
||||
if (pChar && pChar->canNotify()) {
|
||||
if (pChar && (pChar->canNotify() || pChar->canIndicate())) {
|
||||
Log.straceln(
|
||||
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()
|
||||
);
|
||||
|
||||
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) {
|
||||
return;
|
||||
}
|
||||
@@ -791,7 +814,7 @@ protected:
|
||||
auto rawBattery = pChar->getValue<uint8_t>();
|
||||
Log.straceln(
|
||||
FPSTR(L_SENSORS_BLE),
|
||||
F("Sensor #%hhu '%s': received battery: %.2f"),
|
||||
F("Sensor #%hhu '%s': received battery: %hhu"),
|
||||
sensorId, sSensor.name, rawBattery
|
||||
);
|
||||
|
||||
@@ -800,7 +823,8 @@ protected:
|
||||
|
||||
// update rssi
|
||||
Sensors::setValueById(sensorId, pClient->getRssi(), Sensors::ValueType::RSSI, false, false);
|
||||
});
|
||||
}
|
||||
);
|
||||
|
||||
if (batteryNotifyCreated) {
|
||||
Log.straceln(
|
||||
|
||||
Reference in New Issue
Block a user