diff --git a/libs/BLEKit/include/CoreGap.h b/libs/BLEKit/include/CoreGap.h index 5b0f0896e1..87d5fc4344 100644 --- a/libs/BLEKit/include/CoreGap.h +++ b/libs/BLEKit/include/CoreGap.h @@ -40,6 +40,8 @@ class CoreGap CoreGapEventHandler _gap_event_handler; ble::Gap &_gap; + + std::function _on_connection_callback {}; }; } // namespace leka diff --git a/libs/BLEKit/include/CoreGapEventHandler.h b/libs/BLEKit/include/CoreGapEventHandler.h index a4700dc46b..c34941d405 100644 --- a/libs/BLEKit/include/CoreGapEventHandler.h +++ b/libs/BLEKit/include/CoreGapEventHandler.h @@ -24,7 +24,7 @@ class CoreGapEventHandler : public ble::Gap::EventHandler void onDisconnectionComplete(ble::DisconnectionCompleteEvent const &event) override; void onAdvertisingEnd(ble::AdvertisingEndEvent const &event) override; - void onConnectionCallback(const std::function &callback); + void onConnectionCallback(const std::function &callback); void onDisconnectionCallback(const std::function &callback); [[nodiscard]] auto isConnected() const -> bool; @@ -33,7 +33,7 @@ class CoreGapEventHandler : public ble::Gap::EventHandler std::function _start_advertising {}; - std::function _on_connection_callback {}; + std::function _on_connection_callback {}; std::function _on_disconnection_callback {}; }; diff --git a/libs/BLEKit/source/CoreGap.cpp b/libs/BLEKit/source/CoreGap.cpp index 14680fd219..020f73c543 100644 --- a/libs/BLEKit/source/CoreGap.cpp +++ b/libs/BLEKit/source/CoreGap.cpp @@ -60,7 +60,19 @@ void CoreGap::setAdvertising(AdvertisingData advertising_data) void CoreGap::onConnectionCallback(const std::function &callback) { - _gap_event_handler.onConnectionCallback(callback); + _on_connection_callback = [&, callback](connection_handle_t handle) { + // ? : See mbed-os/connectivity/FEATURE_BLE/include/ble/Gap.h for definitions + // ? : Apple guidelines https://developer.apple.com/accessories/Accessory-Design-Guidelines.pdf#page=221 + auto min_connection_interval = conn_interval_t {12}; // Min: 15ms = 12*1,25 + auto max_connection_interval = min_connection_interval; + auto slave_latency = slave_latency_t::min(); + auto supervision_timeout = supervision_timeout_t {500}; + _gap.updateConnectionParameters(handle, min_connection_interval, max_connection_interval, slave_latency, + supervision_timeout); + + callback(); + }; + _gap_event_handler.onConnectionCallback(_on_connection_callback); } void CoreGap::onDisconnectionCallback(const std::function &callback) diff --git a/libs/BLEKit/source/CoreGapEventHandler.cpp b/libs/BLEKit/source/CoreGapEventHandler.cpp index 5b172a1155..c9f7c73d65 100644 --- a/libs/BLEKit/source/CoreGapEventHandler.cpp +++ b/libs/BLEKit/source/CoreGapEventHandler.cpp @@ -28,7 +28,8 @@ void CoreGapEventHandler::onConnectionComplete(ConnectionCompleteEvent const &ev } if (_on_connection_callback != nullptr) { - _on_connection_callback(); + auto handle = event.getConnectionHandle(); // CAUTION: check still exist + _on_connection_callback(handle); } is_connected = true; } @@ -48,7 +49,7 @@ void CoreGapEventHandler::onAdvertisingEnd(AdvertisingEndEvent const &event) _start_advertising(); } -void CoreGapEventHandler::onConnectionCallback(const std::function &callback) +void CoreGapEventHandler::onConnectionCallback(const std::function &callback) { _on_connection_callback = callback; }