Enable ble_medium scanning for advertisement with Fast Pair Service UUID

PiperOrigin-RevId: 544158039
This commit is contained in:
Qin Wang
2023-06-28 14:17:36 -07:00
committed by Copybara-Service
parent b350d3f980
commit d55caf0c4b
6 changed files with 41 additions and 15 deletions
+1
View File
@@ -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",
+6
View File
@@ -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<FastPairRepository> repository)
platform::config_package_nearby::nearby_platform_feature::
kEnableBleV2Gatt,
true);
const_cast<FeatureFlags&>(FeatureFlags::GetInstance())
.SetFlags(fast_pair_feature_flags);
devices_.AddObserver(&on_device_destroyed_callback_);
seeker_ = std::make_unique<FastPairSeekerImpl>(
FastPairSeekerImpl::ServiceCallbacks{
+1
View File
@@ -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",
@@ -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> mediums, std::unique_ptr<UIBroker> ui_broker,
@@ -51,6 +57,9 @@ Mediator::Mediator(
platform::config_package_nearby::nearby_platform_feature::
kEnableBleV2Gatt,
true);
const_cast<FeatureFlags&>(FeatureFlags::GetInstance())
.SetFlags(fast_pair_feature_flags);
devices_ = std::make_unique<FastPairDeviceRepository>(executor_.get());
scanner_broker_ = std::make_unique<ScannerBrokerImpl>(
*mediums_, executor_.get(), devices_.get());
+8 -7
View File
@@ -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<FeatureFlags&>(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_;
};
@@ -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<T>;
// 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());