Skip to content

Commit

Permalink
BLE Library onRead Callback fix and Client disconnect feature (#2913)
Browse files Browse the repository at this point in the history
* BLEServer::disconnect()

* Invoke Read callback first, to make data update possible before read
  • Loading branch information
romansavrulin authored and me-no-dev committed Jul 9, 2019
1 parent e1548e9 commit 05de017
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 4 deletions.
8 changes: 4 additions & 4 deletions libraries/BLE/src/BLECharacteristic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,10 @@ void BLECharacteristic::handleGATTServerEvent(
}
} else { // read.is_long == false

if (m_pCallbacks != nullptr) { // If is.long is false then this is the first (or only) request to read data, so invoke the callback
m_pCallbacks->onRead(this); // Invoke the read callback.
}

std::string value = m_value.getValue();

if (value.length() + 1 > maxOffset) {
Expand All @@ -392,10 +396,6 @@ void BLECharacteristic::handleGATTServerEvent(
rsp.attr_value.offset = 0;
memcpy(rsp.attr_value.value, value.data(), rsp.attr_value.len);
}

if (m_pCallbacks != nullptr) { // If is.long is false then this is the first (or only) request to read data, so invoke the callback
m_pCallbacks->onRead(this); // Invoke the read callback.
}
}
rsp.attr_value.handle = param->read.handle;
rsp.attr_value.auth_req = ESP_GATT_AUTH_REQ_NONE;
Expand Down
5 changes: 5 additions & 0 deletions libraries/BLE/src/BLEServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -412,4 +412,9 @@ void BLEServer::updateConnParams(esp_bd_addr_t remote_bda, uint16_t minInterval,
conn_params.timeout = timeout; // timeout = 400*10ms = 4000ms
esp_ble_gap_update_conn_params(&conn_params);
}

void BLEServer::disconnect(uint16_t connId) {
esp_ble_gatts_close(m_gatts_if, connId);
}

#endif // CONFIG_BT_ENABLED
1 change: 1 addition & 0 deletions libraries/BLE/src/BLEServer.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ class BLEServer {
BLEService* getServiceByUUID(const char* uuid);
BLEService* getServiceByUUID(BLEUUID uuid);
bool connect(BLEAddress address);
void disconnect(uint16_t connId);
uint16_t m_appId;
void updateConnParams(esp_bd_addr_t remote_bda, uint16_t minInterval, uint16_t maxInterval, uint16_t latency, uint16_t timeout);

Expand Down

0 comments on commit 05de017

Please sign in to comment.