diff --git a/internal/platform/ble_v2.cc b/internal/platform/ble_v2.cc index dca18d2c..666a9d67 100644 --- a/internal/platform/ble_v2.cc +++ b/internal/platform/ble_v2.cc @@ -97,8 +97,7 @@ bool BleV2Medium::StartScanning(const Uuid& service_uuid, bool BleV2Medium::StartMultipleServicesScanning( const std::vector& service_uuids, - api::ble_v2::TxPowerLevel tx_power_level, - MultipleServicesScanCallback callback) { + api::ble_v2::TxPowerLevel tx_power_level, ScanCallback callback) { MutexLock lock(&mutex_); if (scanning_enabled_) { NEARBY_LOGS(INFO) << "Ble Scanning already enabled; impl=" << GetImpl(); @@ -106,11 +105,10 @@ bool BleV2Medium::StartMultipleServicesScanning( } bool success = impl_->StartMultipleServicesScanning( service_uuids, tx_power_level, - api::ble_v2::BleMedium::MultipleServicesScanCallback{ + api::ble_v2::BleMedium::ScanCallback{ .advertisement_found_cb = - [this](const Uuid& service_uuid, - api::ble_v2::BlePeripheral& peripheral, - const BleAdvertisementData& advertisement_data) { + [this](api::ble_v2::BlePeripheral& peripheral, + BleAdvertisementData advertisement_data) { MutexLock lock(&mutex_); if (!peripherals_.contains(&peripheral)) { NEARBY_LOGS(INFO) << "Peripheral impl=" << &peripheral @@ -120,12 +118,12 @@ bool BleV2Medium::StartMultipleServicesScanning( BleV2Peripheral proxy(*this, peripheral); if (!scanning_enabled_) return; - multiple_services_scan_callback_.advertisement_found_cb( - service_uuid, std::move(proxy), advertisement_data); + scan_callback_.advertisement_found_cb(std::move(proxy), + advertisement_data); }, }); if (success) { - multiple_services_scan_callback_ = std::move(callback); + scan_callback_ = std::move(callback); peripherals_.clear(); scanning_enabled_ = true; NEARBY_LOGS(INFO) << "Ble Scanning enabled; impl=" << GetImpl(); diff --git a/internal/platform/ble_v2.h b/internal/platform/ble_v2.h index dc198616..248322ce 100644 --- a/internal/platform/ble_v2.h +++ b/internal/platform/ble_v2.h @@ -354,16 +354,6 @@ class BleV2Medium final { const api::ble_v2::BleAdvertisementData&>(); }; - // A wrapper callback for BLE scan results targeting multiple services. - struct MultipleServicesScanCallback { - absl::AnyInvocable - advertisement_found_cb = - nearby::DefaultCallback(); - }; - struct ServerGattConnectionCallback { using BlePeripheral = api::ble_v2::BlePeripheral; using GattCharacteristic = api::ble_v2::GattCharacteristic; @@ -425,7 +415,7 @@ class BleV2Medium final { // Returns true once the BLE multiple services scan has been initiated. bool StartMultipleServicesScanning(const std::vector& service_uuids, api::ble_v2::TxPowerLevel tx_power_level, - MultipleServicesScanCallback callback); + ScanCallback callback); // This interface will be deprecated soon. // TODO(b/271305977) remove this function. @@ -476,8 +466,6 @@ class BleV2Medium final { absl::flat_hash_set peripherals_ ABSL_GUARDED_BY(mutex_); ScanCallback scan_callback_ ABSL_GUARDED_BY(mutex_); - MultipleServicesScanCallback multiple_services_scan_callback_ - ABSL_GUARDED_BY(mutex_); bool scanning_enabled_ ABSL_GUARDED_BY(mutex_) = false; }; diff --git a/internal/platform/ble_v2_test.cc b/internal/platform/ble_v2_test.cc index 5139539f..0f21f816 100644 --- a/internal/platform/ble_v2_test.cc +++ b/internal/platform/ble_v2_test.cc @@ -167,9 +167,8 @@ TEST_P(BleV2MediumTest, CanConnectToServiceWithMultipleServices) { { .advertisement_found_cb = [&found_latch, &discovered_peripheral]( - const Uuid& service_uuid, BleV2Peripheral peripheral, + BleV2Peripheral peripheral, const BleAdvertisementData& advertisement_data) { - EXPECT_EQ(service_uuid, Uuid(1234, 5678)); discovered_peripheral = std::move(peripheral); found_latch.CountDown(); }, @@ -242,14 +241,13 @@ TEST_P(BleV2MediumTest, CanDiscoverMultipleServices) { std::vector{service_uuid_a, service_uuid_b}, kTxPowerLevel, {.advertisement_found_cb = [&found_latch, &found_service_a, &found_service_b, &service_uuid_a, - &service_uuid_b](const Uuid& service_uuid, - BleV2Peripheral peripheral, + &service_uuid_b](BleV2Peripheral peripheral, const BleAdvertisementData& advertisement_data) { - if (service_uuid == service_uuid_a) { + if (advertisement_data.service_data.contains(service_uuid_a)) { found_service_a = true; } - if (service_uuid == service_uuid_b) { + if (advertisement_data.service_data.contains(service_uuid_b)) { found_service_b = true; } if (found_service_a && found_service_b) { diff --git a/internal/platform/implementation/ble_v2.h b/internal/platform/implementation/ble_v2.h index f8fc9fe7..d9a12ffe 100644 --- a/internal/platform/implementation/ble_v2.h +++ b/internal/platform/implementation/ble_v2.h @@ -445,24 +445,6 @@ class BleMedium { TxPowerLevel tx_power_level, ScanCallback callback) = 0; - // Callback for BLE scans targeting multiple services. - // - // service_uuid: The UUID of the service discovered in the scan result. - // peripheral: A platform-owned object representing the discovered - // peripheral. This object's lifetime extends for the duration of any - // connection to the device. - // advertisement_data: A combination of advertisement and scan response data. - // - // This callback is invoked for every discovered advertisement , even if the - // same advertisement has been seen previously. - // - struct MultipleServicesScanCallback { - absl::AnyInvocable - advertisement_found_cb = - [](const Uuid&, BlePeripheral&, const BleAdvertisementData&) {}; - }; - // Starts multiple services scanning and returns whether or not it was // successful. // @@ -473,7 +455,7 @@ class BleMedium { virtual bool StartMultipleServicesScanning( const std::vector& service_uuids, TxPowerLevel tx_power_level, - MultipleServicesScanCallback callback) { + ScanCallback callback) { return false; } diff --git a/internal/platform/implementation/g3/ble_v2.cc b/internal/platform/implementation/g3/ble_v2.cc index 9a4532df..4b5196ad 100644 --- a/internal/platform/implementation/g3/ble_v2.cc +++ b/internal/platform/implementation/g3/ble_v2.cc @@ -253,26 +253,24 @@ bool BleV2Medium::StartScanning(const Uuid& service_uuid, bool BleV2Medium::StartMultipleServicesScanning( const std::vector& service_uuids, - api::ble_v2::TxPowerLevel tx_power_level, - MultipleServicesScanCallback callback) { + api::ble_v2::TxPowerLevel tx_power_level, ScanCallback callback) { NEARBY_LOGS(INFO) << "G3 Ble StartMultipleServicesScanning"; absl::MutexLock lock(&mutex_); - multiple_services_scan_callback_ = std::move(callback); + scan_callback_ = std::move(callback); for (const auto& service_uuid : service_uuids) { auto internal_session_id = Prng().NextUint32(); - ScanCallback scan_callback = { - .advertisement_found_cb = [this, service_uuid]( + ScanCallback multiple_scan_callback = { + .advertisement_found_cb = [this]( api::ble_v2::BlePeripheral& peripheral, BleAdvertisementData advertisement_data) { - multiple_services_scan_callback_.advertisement_found_cb( - service_uuid, peripheral, advertisement_data); + scan_callback_.advertisement_found_cb(peripheral, advertisement_data); }}; MediumEnvironment::Instance().UpdateBleV2MediumForScanning( /*enabled=*/true, service_uuid, internal_session_id, {.advertisement_found_cb = - std::move(scan_callback.advertisement_found_cb)}, + std::move(multiple_scan_callback.advertisement_found_cb)}, *this); scanning_internal_session_ids_.insert({service_uuid, internal_session_id}); } diff --git a/internal/platform/implementation/g3/ble_v2.h b/internal/platform/implementation/g3/ble_v2.h index fb0a49af..bffe812b 100644 --- a/internal/platform/implementation/g3/ble_v2.h +++ b/internal/platform/implementation/g3/ble_v2.h @@ -166,7 +166,7 @@ class BleV2Medium : public api::ble_v2::BleMedium { ABSL_LOCKS_EXCLUDED(mutex_); bool StartMultipleServicesScanning(const std::vector& service_uuids, api::ble_v2::TxPowerLevel tx_power_level, - MultipleServicesScanCallback callback) + ScanCallback callback) override ABSL_LOCKS_EXCLUDED(mutex_); bool StopScanning() override ABSL_LOCKS_EXCLUDED(mutex_); std::unique_ptr StartScanning( @@ -337,7 +337,7 @@ class BleV2Medium : public api::ble_v2::BleMedium { absl::flat_hash_set> scanning_internal_session_ids_ ABSL_GUARDED_BY(mutex_); bool is_extended_advertisements_available_ = false; - MultipleServicesScanCallback multiple_services_scan_callback_; + ScanCallback scan_callback_; }; } // namespace g3