diff --git a/fastpair/BUILD b/fastpair/BUILD index b1c3f39e..f4285b79 100644 --- a/fastpair/BUILD +++ b/fastpair/BUILD @@ -100,6 +100,7 @@ cc_library( "//fastpair/repository:device_repository", "//fastpair/server_access", "//internal/flags:nearby_flags", + "//internal/platform:base", "//internal/platform:types", "//internal/platform/flags:platform_flags", "@com_google_absl//absl/container:flat_hash_map", diff --git a/fastpair/fast_pair_service.cc b/fastpair/fast_pair_service.cc index 3482c78e..c5783192 100644 --- a/fastpair/fast_pair_service.cc +++ b/fastpair/fast_pair_service.cc @@ -25,6 +25,7 @@ #include "fastpair/internal/fast_pair_seeker_impl.h" #include "fastpair/server_access/fast_pair_repository_impl.h" #include "internal/flags/nearby_flags.h" +#include "internal/platform/feature_flags.h" #include "internal/platform/flags/nearby_platform_feature_flags.h" #include "internal/platform/logging.h" @@ -33,6 +34,9 @@ namespace fastpair { namespace { constexpr absl::Duration kTimeout = absl::Seconds(3); +constexpr FeatureFlags::Flags fast_pair_feature_flags = FeatureFlags::Flags{ + .enable_scan_for_fast_pair_advertisement = true, +}; } FastPairService::FastPairService() @@ -46,6 +50,8 @@ FastPairService::FastPairService(std::unique_ptr repository) platform::config_package_nearby::nearby_platform_feature:: kEnableBleV2Gatt, true); + const_cast(FeatureFlags::GetInstance()) + .SetFlags(fast_pair_feature_flags); devices_.AddObserver(&on_device_destroyed_callback_); seeker_ = std::make_unique( FastPairSeekerImpl::ServiceCallbacks{ diff --git a/fastpair/keyed_service/BUILD b/fastpair/keyed_service/BUILD index 21e38764..269af047 100644 --- a/fastpair/keyed_service/BUILD +++ b/fastpair/keyed_service/BUILD @@ -37,6 +37,7 @@ cc_library( "//fastpair/server_access", "//fastpair/ui:fast_pair_ui", "//internal/flags:nearby_flags", + "//internal/platform:base", "//internal/platform:logging", "//internal/platform:types", "//internal/platform/flags:platform_flags", diff --git a/fastpair/keyed_service/fast_pair_mediator.cc b/fastpair/keyed_service/fast_pair_mediator.cc index 2b93118b..c698963f 100644 --- a/fastpair/keyed_service/fast_pair_mediator.cc +++ b/fastpair/keyed_service/fast_pair_mediator.cc @@ -30,12 +30,18 @@ #include "fastpair/ui/ui_broker_impl.h" #include "internal/flags/nearby_flags.h" #include "internal/platform/device_info_impl.h" +#include "internal/platform/feature_flags.h" #include "internal/platform/flags/nearby_platform_feature_flags.h" #include "internal/platform/logging.h" #include "internal/platform/single_thread_executor.h" namespace nearby { namespace fastpair { +namespace { +constexpr FeatureFlags::Flags fast_pair_feature_flags = FeatureFlags::Flags{ + .enable_scan_for_fast_pair_advertisement = true, +}; +} Mediator::Mediator( std::unique_ptr mediums, std::unique_ptr ui_broker, @@ -51,6 +57,9 @@ Mediator::Mediator( platform::config_package_nearby::nearby_platform_feature:: kEnableBleV2Gatt, true); + const_cast(FeatureFlags::GetInstance()) + .SetFlags(fast_pair_feature_flags); + devices_ = std::make_unique(executor_.get()); scanner_broker_ = std::make_unique( *mediums_, executor_.get(), devices_.get()); diff --git a/internal/platform/feature_flags.h b/internal/platform/feature_flags.h index 8ff66d12..92d0e532 100644 --- a/internal/platform/feature_flags.h +++ b/internal/platform/feature_flags.h @@ -50,9 +50,12 @@ class FeatureFlags { // If the feature is enabled, medium connection will timeout when cannot // create connection with remote device in a duration. bool enable_connection_timeout = true; - // Controls to enable or disable to track the status of Bluetooth classic + // Controls enable or disable to track the status of Bluetooth classic // conncetion. bool enable_bluetooth_connection_status_track = true; + // Controls enable or disable BLE scan advertisement for fast pair + // service uuid 0x2cfe + bool enable_scan_for_fast_pair_advertisement = false; }; static const FeatureFlags& GetInstance() { @@ -69,16 +72,14 @@ class FeatureFlags { return const_cast(GetInstance()).flags_; } - private: - FeatureFlags() = default; - - // MediumEnvironment is testing util class. Use friend class here to enable - // SetFlags for feature controlling need in test environment. - friend class MediumEnvironment; + // SetFlags for feature controlling void SetFlags(const Flags& flags) ABSL_LOCKS_EXCLUDED(mutex_) { absl::MutexLock lock(&mutex_); flags_ = flags; } + + private: + FeatureFlags() = default; Flags flags_ ABSL_GUARDED_BY(mutex_); mutable absl::Mutex mutex_; }; diff --git a/internal/platform/implementation/windows/ble_medium.cc b/internal/platform/implementation/windows/ble_medium.cc index d7273504..796afd98 100644 --- a/internal/platform/implementation/windows/ble_medium.cc +++ b/internal/platform/implementation/windows/ble_medium.cc @@ -26,6 +26,7 @@ #include "absl/synchronization/mutex.h" #include "absl/synchronization/notification.h" #include "absl/time/time.h" +#include "internal/platform/feature_flags.h" #include "internal/platform/implementation/windows/ble_peripheral.h" #include "internal/platform/implementation/windows/bluetooth_adapter.h" #include "internal/platform/implementation/windows/utils.h" @@ -129,6 +130,12 @@ using IVector = winrt::Windows::Foundation::Collections::IVector; // Copresence Service UUID 0xfef3 (little-endian) constexpr uint16_t kCopresenceServiceUuid = 0xf3fe; + +bool IsFastPairScanner() { + return FeatureFlags::GetInstance() + .GetFlags() + .enable_scan_for_fast_pair_advertisement; +} } // namespace BleMedium::BleMedium(api::BluetoothAdapter& adapter) @@ -579,10 +586,11 @@ void BleMedium::AdvertisementReceivedHandler( DataReader data_reader = DataReader::FromBuffer(service_data.Data()); // Discard the first 2 bytes of Service Uuid in Service Data - uint8_t first_byte = data_reader.ReadByte(); // 0xf3 - uint8_t second_byte = data_reader.ReadByte(); // 0xfe + uint8_t first_byte = data_reader.ReadByte(); + uint8_t second_byte = data_reader.ReadByte(); - if (first_byte == 0xf3 && second_byte == 0xfe) { + if ((IsFastPairScanner() && first_byte == 0x2c && second_byte == 0xfe) || + (!IsFastPairScanner() && first_byte == 0xf3 && second_byte == 0xfe)) { std::string data; uint8_t unconsumed_buffer_length = data_reader.UnconsumedBufferLength(); @@ -592,11 +600,11 @@ void BleMedium::AdvertisementReceivedHandler( ByteArray advertisement_data(data); - NEARBY_LOGS(VERBOSE) - << "Nearby BLE Medium 0xFEF3 Advertisement discovered. " - "0x16 Service data: advertisement bytes= 0x" - << absl::BytesToHexString(advertisement_data.AsStringView()) << "(" - << advertisement_data.size() << ")"; + NEARBY_LOGS(VERBOSE) << "Nearby BLE Medium Advertisement discovered. " + "0x16 Service data: advertisement bytes= 0x" + << absl::BytesToHexString( + advertisement_data.AsStringView()) + << "(" << advertisement_data.size() << ")"; std::string peripheral_name = uint64_to_mac_address_string(args.BluetoothAddress());