diff --git a/connections/implementation/mediums/ble_test.cc b/connections/implementation/mediums/ble_test.cc index 03a4b7e4..c054e911 100644 --- a/connections/implementation/mediums/ble_test.cc +++ b/connections/implementation/mediums/ble_test.cc @@ -247,6 +247,48 @@ TEST_F(BleTest, CanStartDiscovery) { env_.Stop(); } +TEST_F(BleTest, HandleDupeFindingsFromDiscovery) { + env_.Start(); + BluetoothRadio radio_a; + BluetoothRadio radio_b; + Ble ble_a{radio_a}; + Ble ble_b{radio_b}; + radio_a.Enable(); + radio_b.Enable(); + std::string service_id(kServiceID); + ByteArray advertisement_bytes{std::string(kAdvertisementString)}; + std::string fast_advertisement_service_uuid(kFastAdvertisementServiceUuid); + // expecting two discoveries from the same peripheral. + CountDownLatch discovery_latch(2); + // Expecting the peripheral lost will trigger lost_cb. + CountDownLatch lost_latch(1); + + ble_b.StartAdvertising(service_id, advertisement_bytes, + fast_advertisement_service_uuid); + + EXPECT_TRUE(ble_a.StartScanning( + service_id, fast_advertisement_service_uuid, + DiscoveredPeripheralCallback{ + .peripheral_discovered_cb = + [&discovery_latch]( + BlePeripheral& peripheral, const std::string& service_id, + const ByteArray& advertisement_bytes, + bool fast_advertisement) { discovery_latch.CountDown(); }, + .peripheral_lost_cb = + [&lost_latch](BlePeripheral& peripheral, + const std::string& service_id) { + lost_latch.CountDown(); + }, + })); + ble_b.StartAdvertising(service_id, advertisement_bytes, + fast_advertisement_service_uuid); + EXPECT_TRUE(discovery_latch.Await(kWaitDuration).result()); + ble_b.StopAdvertising(service_id); + EXPECT_TRUE(lost_latch.Await(kWaitDuration).result()); + EXPECT_TRUE(ble_a.StopScanning(service_id)); + env_.Stop(); +} + TEST_F(BleTest, CanStartAndStopLegacyAdvertising) { env_.Start(); BluetoothRadio radio_a; diff --git a/internal/platform/ble.cc b/internal/platform/ble.cc index d6792c22..e653c7ae 100644 --- a/internal/platform/ble.cc +++ b/internal/platform/ble.cc @@ -49,13 +49,11 @@ bool BleMedium::StartScanning( auto pair = peripherals_.emplace( &peripheral, absl::make_unique()); auto& context = *pair.first->second; - if (pair.second) { - context.peripheral = BlePeripheral(&peripheral); - discovered_peripheral_callback_.peripheral_discovered_cb( - context.peripheral, service_id, - context.peripheral.GetAdvertisementBytes(service_id), - fast_advertisement); - } + context.peripheral = BlePeripheral(&peripheral); + discovered_peripheral_callback_.peripheral_discovered_cb( + context.peripheral, service_id, + context.peripheral.GetAdvertisementBytes(service_id), + fast_advertisement); }, .peripheral_lost_cb = [this](api::BlePeripheral& peripheral,