mirror of
https://github.com/kidfromjupiter/nearby.git
synced 2026-09-14 14:46:12 -04:00
[BLE Refactor] Removes fast advertisement uuid in StartAdvertising and StartScanning.
PiperOrigin-RevId: 448426759
This commit is contained in:
committed by
Copybara-Service
parent
080e964a4c
commit
eddf53ec29
@@ -37,6 +37,7 @@ struct AdvertisingOptions : public OptionsBase {
|
||||
|
||||
// Whether this is intended to be used in conjunction with InjectEndpoint().
|
||||
bool is_out_of_band_connection = false;
|
||||
// TODO(b/229927044): Replaces it as bool once Ble v1 is deprecated.
|
||||
std::string fast_advertisement_service_uuid;
|
||||
|
||||
// Returns a copy and normalizes allowed mediums:
|
||||
|
||||
@@ -36,6 +36,7 @@ struct DiscoveryOptions : OptionsBase {
|
||||
|
||||
// Whether this is intended to be used in conjunction with InjectEndpoint().
|
||||
bool is_out_of_band_connection = false;
|
||||
// TODO(b/229927044): Replaces it as bool once Ble v1 is deprecated.
|
||||
std::string fast_advertisement_service_uuid;
|
||||
|
||||
// Returns a copy and normalizes allowed mediums:
|
||||
|
||||
@@ -77,12 +77,10 @@ bool BleV2::IsAvailable() const {
|
||||
}
|
||||
|
||||
// TODO(edwinwu): Break down the function.
|
||||
// TODO(b/229927044): Use bool: is_fast_advertisement, not
|
||||
// fast_advertisement_service_uuid.
|
||||
bool BleV2::StartAdvertising(
|
||||
const std::string& service_id, const ByteArray& advertisement_bytes,
|
||||
PowerLevel power_level,
|
||||
const std::string& fast_advertisement_service_uuid) {
|
||||
bool BleV2::StartAdvertising(const std::string& service_id,
|
||||
const ByteArray& advertisement_bytes,
|
||||
PowerLevel power_level,
|
||||
bool is_fast_advertisement) {
|
||||
MutexLock lock(&mutex_);
|
||||
|
||||
if (advertisement_bytes.Empty()) {
|
||||
@@ -117,7 +115,6 @@ bool BleV2::StartAdvertising(
|
||||
}
|
||||
|
||||
// Wrap the connections advertisement to the medium advertisement.
|
||||
const bool is_fast_advertisement = !fast_advertisement_service_uuid.empty();
|
||||
ByteArray service_id_hash = mediums::bleutils::GenerateHash(
|
||||
service_id, mediums::BleAdvertisement::kServiceIdHashLength);
|
||||
ByteArray medium_advertisement_bytes{mediums::BleAdvertisement{
|
||||
@@ -139,13 +136,15 @@ bool BleV2::StartAdvertising(
|
||||
advertising_data.is_connectable = true;
|
||||
advertising_data.tx_power_level =
|
||||
BleAdvertisementData::kUnspecifiedTxPowerLevel;
|
||||
advertising_data.service_uuids.insert(fast_advertisement_service_uuid);
|
||||
advertising_data.service_uuids.insert(
|
||||
std::string(mediums::bleutils::kCopresenceServiceUuid));
|
||||
|
||||
scan_response_data.is_connectable = true;
|
||||
scan_response_data.tx_power_level =
|
||||
BleAdvertisementData::kUnspecifiedTxPowerLevel;
|
||||
scan_response_data.service_data.insert(
|
||||
{fast_advertisement_service_uuid, medium_advertisement_bytes});
|
||||
{std::string(mediums::bleutils::kCopresenceServiceUuid),
|
||||
medium_advertisement_bytes});
|
||||
} else {
|
||||
// Stop the current advertisement GATT server if there are no incoming
|
||||
// sockets connected to this device.
|
||||
@@ -209,10 +208,7 @@ bool BleV2::StartAdvertising(
|
||||
NEARBY_LOGS(ERROR)
|
||||
<< "Failed to turn on BLE advertising with advertisement bytes="
|
||||
<< absl::BytesToHexString(advertisement_bytes.data())
|
||||
<< ", is_fast_advertisement=" << is_fast_advertisement
|
||||
<< ", fast advertisement service uuid="
|
||||
<< (is_fast_advertisement ? fast_advertisement_service_uuid
|
||||
: "[empty]");
|
||||
<< ", is_fast_advertisement=" << is_fast_advertisement;
|
||||
|
||||
// If BLE advertising was not successful, stop the advertisement GATT
|
||||
// server.
|
||||
@@ -275,10 +271,8 @@ bool BleV2::IsAdvertising(const std::string& service_id) const {
|
||||
return IsAdvertisingLocked(service_id);
|
||||
}
|
||||
|
||||
// TODO(b/229927044): Remove param: fast_advertisement_service_uuid.
|
||||
bool BleV2::StartScanning(const std::string& service_id, PowerLevel power_level,
|
||||
DiscoveredPeripheralCallback callback,
|
||||
const std::string& fast_advertisement_service_uuid) {
|
||||
DiscoveredPeripheralCallback callback) {
|
||||
MutexLock lock(&mutex_);
|
||||
|
||||
if (service_id.empty()) {
|
||||
@@ -305,17 +299,9 @@ bool BleV2::StartScanning(const std::string& service_id, PowerLevel power_level,
|
||||
}
|
||||
|
||||
// Start to track the advertisement found for specific `service_id`.
|
||||
discovered_peripheral_tracker_.StartTracking(service_id, std::move(callback),
|
||||
fast_advertisement_service_uuid);
|
||||
|
||||
// Check if scan has been activated, if yes, no need to notify client
|
||||
// to scan again.
|
||||
if (!scanned_service_ids_.empty()) {
|
||||
scanned_service_ids_.insert(service_id);
|
||||
NEARBY_LOGS(INFO) << "Turned on BLE scanning with service id=" << service_id
|
||||
<< " without start client scanning";
|
||||
return true;
|
||||
}
|
||||
discovered_peripheral_tracker_.StartTracking(
|
||||
service_id, std::move(callback),
|
||||
std::string(mediums::bleutils::kCopresenceServiceUuid));
|
||||
|
||||
// Check if scan has been activated, if yes, no need to notify client
|
||||
// to scan again.
|
||||
@@ -329,15 +315,9 @@ bool BleV2::StartScanning(const std::string& service_id, PowerLevel power_level,
|
||||
scanned_service_ids_.insert(service_id);
|
||||
// TODO(b/213835576): We should re-start scanning once the power level is
|
||||
// changed.
|
||||
std::vector<std::string> scanning_service_uuids;
|
||||
if (!fast_advertisement_service_uuid.empty()) {
|
||||
scanning_service_uuids.push_back(fast_advertisement_service_uuid);
|
||||
} else {
|
||||
scanning_service_uuids.push_back(
|
||||
std::string(mediums::bleutils::kCopresenceServiceUuid));
|
||||
}
|
||||
if (!medium_.StartScanning(
|
||||
scanning_service_uuids, PowerLevelToPowerMode(power_level),
|
||||
std::string(mediums::bleutils::kCopresenceServiceUuid),
|
||||
PowerLevelToPowerMode(power_level),
|
||||
{
|
||||
.advertisement_found_cb =
|
||||
[this](BleV2Peripheral peripheral,
|
||||
|
||||
@@ -55,19 +55,16 @@ class BleV2 final {
|
||||
// Starts BLE advertising, delivering additional information if the platform
|
||||
// supports it.
|
||||
//
|
||||
// service_id - The service ID to track.
|
||||
// advertisement_bytes - The connections BLE Advertisement used in
|
||||
// advertising.
|
||||
// power_level - The power level to use for the advertisement.
|
||||
// fast_advertisement_service_uuid - The service UUID to look for fast
|
||||
// advertisements on.
|
||||
// Note: fast_advertisement_service_uuid can be an empty string to indicate
|
||||
// that `fast_advertisement_service_uuid` will be ignored for regular
|
||||
// advertisement.
|
||||
// service_id - The service ID to track.
|
||||
// advertisement_bytes - The connections BLE Advertisement used in
|
||||
// advertising.
|
||||
// power_level - The power level to use for the advertisement.
|
||||
// is_fast_advertisement - True to use fast advertisements, which are smaller
|
||||
// but much more efficient to discover.
|
||||
bool StartAdvertising(const std::string& service_id,
|
||||
const ByteArray& advertisement_bytes,
|
||||
PowerLevel power_level,
|
||||
const std::string& fast_advertisement_service_uuid)
|
||||
bool is_fast_advertisement)
|
||||
ABSL_LOCKS_EXCLUDED(mutex_);
|
||||
|
||||
// Disables BLE advertising.
|
||||
@@ -85,12 +82,8 @@ class BleV2 final {
|
||||
// power_level - The power level to use for the discovery.
|
||||
// discovered_peripheral_callback - The callback to invoke for discovery
|
||||
// events.
|
||||
// Note: fast_advertisement_service_uuid can be emptry string to indicate that
|
||||
// `fast_advertisement_service_uuid` will be ignored for regular
|
||||
// advertisement.
|
||||
bool StartScanning(const std::string& service_id, PowerLevel power_level,
|
||||
DiscoveredPeripheralCallback callback,
|
||||
const std::string& fast_advertisement_service_uuid)
|
||||
DiscoveredPeripheralCallback callback)
|
||||
ABSL_LOCKS_EXCLUDED(mutex_);
|
||||
|
||||
// Disables BLE scanning for a service ID.
|
||||
|
||||
@@ -33,8 +33,6 @@ constexpr absl::string_view kServiceIDA =
|
||||
constexpr absl::string_view kServiceIDB =
|
||||
"com.google.location.nearby.apps.test.b";
|
||||
constexpr absl::string_view kAdvertisementString = "\x0a\x0b\x0c\x0d";
|
||||
constexpr absl::string_view kFastAdvertisementServiceUuid =
|
||||
"0000FE2C-0000-1000-8000-00805F9B34FB";
|
||||
|
||||
class BleV2Test : public testing::Test {
|
||||
protected:
|
||||
@@ -80,12 +78,11 @@ TEST_F(BleV2Test, CanStartFastAdvertising) {
|
||||
EXPECT_TRUE(fast_advertisement);
|
||||
found_latch.CountDown();
|
||||
},
|
||||
},
|
||||
std::string(kFastAdvertisementServiceUuid));
|
||||
});
|
||||
|
||||
EXPECT_TRUE(ble_a.StartAdvertising(
|
||||
std::string(kServiceIDA), advertisement_bytes, PowerLevel::kHighPower,
|
||||
std::string(kFastAdvertisementServiceUuid)));
|
||||
/*is_fast_advertisement=*/true));
|
||||
EXPECT_TRUE(found_latch.Await(kWaitDuration).result());
|
||||
EXPECT_TRUE(ble_a.StopAdvertising(std::string(kServiceIDA)));
|
||||
ble_b.StopScanning(std::string(kServiceIDA));
|
||||
@@ -105,7 +102,7 @@ TEST_F(BleV2Test, CanStartFastScanning) {
|
||||
|
||||
ble_b.StartAdvertising(std::string(kServiceIDA), advertisement_bytes,
|
||||
PowerLevel::kHighPower,
|
||||
std::string(kFastAdvertisementServiceUuid));
|
||||
/*is_fast_advertisement=*/true);
|
||||
|
||||
EXPECT_TRUE(ble_a.StartScanning(
|
||||
std::string(kServiceIDA), PowerLevel::kHighPower,
|
||||
@@ -118,8 +115,7 @@ TEST_F(BleV2Test, CanStartFastScanning) {
|
||||
EXPECT_TRUE(fast_advertisement);
|
||||
found_latch.CountDown();
|
||||
},
|
||||
},
|
||||
std::string(kFastAdvertisementServiceUuid)));
|
||||
}));
|
||||
|
||||
EXPECT_TRUE(found_latch.Await(kWaitDuration).result());
|
||||
ble_b.StopAdvertising(std::string(kServiceIDA));
|
||||
@@ -149,12 +145,11 @@ TEST_F(BleV2Test, CanStartAdvertising) {
|
||||
EXPECT_FALSE(fast_advertisement);
|
||||
found_latch.CountDown();
|
||||
},
|
||||
},
|
||||
/*fast_advertisement_service_uuid=*/"");
|
||||
});
|
||||
|
||||
EXPECT_TRUE(ble_a.StartAdvertising(
|
||||
std::string(kServiceIDA), advertisement_bytes, PowerLevel::kHighPower,
|
||||
/*fast_advertisement_service_uuid=*/""));
|
||||
/*is_fast_advertisement=*/false));
|
||||
EXPECT_TRUE(found_latch.Await(kWaitDuration).result());
|
||||
EXPECT_TRUE(ble_a.StopAdvertising(std::string(kServiceIDA)));
|
||||
ble_b.StopScanning(std::string(kServiceIDA));
|
||||
@@ -174,7 +169,7 @@ TEST_F(BleV2Test, CanStartScanning) {
|
||||
|
||||
ble_b.StartAdvertising(std::string(kServiceIDA), advertisement_bytes,
|
||||
PowerLevel::kHighPower,
|
||||
/*fast_advertisement_service_uuid=*/"");
|
||||
/*is_fast_advertisement=*/false);
|
||||
|
||||
EXPECT_TRUE(ble_a.StartScanning(
|
||||
std::string(kServiceIDA), PowerLevel::kHighPower,
|
||||
@@ -187,8 +182,7 @@ TEST_F(BleV2Test, CanStartScanning) {
|
||||
EXPECT_FALSE(fast_advertisement);
|
||||
found_latch.CountDown();
|
||||
},
|
||||
},
|
||||
/*fast_advertisement_service_uuid=*/""));
|
||||
}));
|
||||
|
||||
EXPECT_TRUE(found_latch.Await(kWaitDuration).result());
|
||||
ble_b.StopAdvertising(std::string(kServiceIDA));
|
||||
@@ -204,18 +198,15 @@ TEST_F(BleV2Test, CanStartStopMultipleScanningWithDifferentServiceIds) {
|
||||
|
||||
EXPECT_TRUE(ble.StartScanning(std::string(kServiceIDA),
|
||||
PowerLevel::kHighPower,
|
||||
mediums::DiscoveredPeripheralCallback{},
|
||||
std::string(kFastAdvertisementServiceUuid)));
|
||||
mediums::DiscoveredPeripheralCallback{}));
|
||||
EXPECT_TRUE(ble.StartScanning(std::string(kServiceIDB),
|
||||
PowerLevel::kHighPower,
|
||||
mediums::DiscoveredPeripheralCallback{},
|
||||
std::string(kFastAdvertisementServiceUuid)));
|
||||
mediums::DiscoveredPeripheralCallback{}));
|
||||
EXPECT_TRUE(ble.StopScanning(std::string(kServiceIDA)));
|
||||
|
||||
EXPECT_TRUE(ble.StartScanning(std::string(kServiceIDA),
|
||||
PowerLevel::kHighPower,
|
||||
mediums::DiscoveredPeripheralCallback{},
|
||||
std::string(kFastAdvertisementServiceUuid)));
|
||||
mediums::DiscoveredPeripheralCallback{}));
|
||||
EXPECT_TRUE(ble.StopScanning(std::string(kServiceIDA)));
|
||||
EXPECT_TRUE(ble.StopScanning(std::string(kServiceIDB)));
|
||||
env_.Stop();
|
||||
@@ -235,20 +226,18 @@ TEST_F(BleV2Test, DestructWorksForStartAdvertisingAndScanningWithoutStop) {
|
||||
// Device A starts advertising with service IDA and IDB.
|
||||
EXPECT_TRUE(ble_a.StartAdvertising(
|
||||
std::string(kServiceIDA), advertisement_bytes, PowerLevel::kHighPower,
|
||||
std::string(kFastAdvertisementServiceUuid)));
|
||||
/*is_fast_advertisement=*/true));
|
||||
EXPECT_TRUE(ble_a.StartAdvertising(
|
||||
std::string(kServiceIDB), advertisement_bytes, PowerLevel::kHighPower,
|
||||
std::string(kFastAdvertisementServiceUuid)));
|
||||
/*is_fast_advertisement=*/true));
|
||||
|
||||
// Device B starts scanning with service IDA and IDB
|
||||
EXPECT_TRUE(ble_b.StartScanning(std::string(kServiceIDA),
|
||||
PowerLevel::kHighPower,
|
||||
mediums::DiscoveredPeripheralCallback{},
|
||||
std::string(kFastAdvertisementServiceUuid)));
|
||||
mediums::DiscoveredPeripheralCallback{}));
|
||||
EXPECT_TRUE(ble_b.StartScanning(std::string(kServiceIDB),
|
||||
PowerLevel::kHighPower,
|
||||
mediums::DiscoveredPeripheralCallback{},
|
||||
std::string(kFastAdvertisementServiceUuid)));
|
||||
mediums::DiscoveredPeripheralCallback{}));
|
||||
env_.Stop();
|
||||
}
|
||||
|
||||
@@ -266,7 +255,7 @@ TEST_F(BleV2Test, StartFastScanningDiscoverAndLostPeripheral) {
|
||||
|
||||
ble_b.StartAdvertising(std::string(kServiceIDA), advertisement_bytes,
|
||||
PowerLevel::kHighPower,
|
||||
std::string(kFastAdvertisementServiceUuid));
|
||||
/*is_fast_advertisement=*/true);
|
||||
|
||||
ble_a.StartScanning(
|
||||
std::string(kServiceIDA), PowerLevel::kHighPower,
|
||||
@@ -284,8 +273,7 @@ TEST_F(BleV2Test, StartFastScanningDiscoverAndLostPeripheral) {
|
||||
const std::string& service_id) {
|
||||
lost_latch.CountDown();
|
||||
},
|
||||
},
|
||||
std::string(kFastAdvertisementServiceUuid));
|
||||
});
|
||||
|
||||
EXPECT_TRUE(found_latch.Await(kWaitDuration).result());
|
||||
|
||||
@@ -316,7 +304,7 @@ TEST_F(BleV2Test,
|
||||
|
||||
ble_b.StartAdvertising(std::string(kServiceIDA), advertisement_bytes,
|
||||
PowerLevel::kHighPower,
|
||||
std::string(kFastAdvertisementServiceUuid));
|
||||
/*is_fast_advertisement=*/true);
|
||||
|
||||
ble_a.StartScanning(
|
||||
std::string(kServiceIDA), PowerLevel::kHighPower,
|
||||
@@ -334,8 +322,7 @@ TEST_F(BleV2Test,
|
||||
const std::string& service_id) {
|
||||
lost_latch.CountDown();
|
||||
},
|
||||
},
|
||||
std::string(kFastAdvertisementServiceUuid));
|
||||
});
|
||||
|
||||
EXPECT_TRUE(found_latch.Await(kWaitDuration).result());
|
||||
|
||||
@@ -363,7 +350,7 @@ TEST_F(BleV2Test, StartScanningDiscoverAndLostPeripheral) {
|
||||
|
||||
ble_b.StartAdvertising(std::string(kServiceIDA), advertisement_bytes,
|
||||
PowerLevel::kHighPower,
|
||||
/*fast_advertisement_service_uuid=*/"");
|
||||
/*is_fast_advertisement=*/false);
|
||||
|
||||
ble_a.StartScanning(
|
||||
std::string(kServiceIDA), PowerLevel::kHighPower,
|
||||
@@ -381,8 +368,7 @@ TEST_F(BleV2Test, StartScanningDiscoverAndLostPeripheral) {
|
||||
const std::string& service_id) {
|
||||
lost_latch.CountDown();
|
||||
},
|
||||
},
|
||||
/*fast_advertisement_service_uuid=*/"");
|
||||
});
|
||||
|
||||
EXPECT_TRUE(found_latch.Await(kWaitDuration).result());
|
||||
|
||||
@@ -411,7 +397,8 @@ TEST_F(BleV2Test, StartScanningDiscoverButNoPeripheralLostAfterStopScanning) {
|
||||
CountDownLatch lost_latch(1);
|
||||
|
||||
ble_b.StartAdvertising(std::string(kServiceIDA), advertisement_bytes,
|
||||
PowerLevel::kHighPower, "");
|
||||
PowerLevel::kHighPower,
|
||||
/*is_fast_advertisement=*/false);
|
||||
|
||||
ble_a.StartScanning(
|
||||
std::string(kServiceIDA), PowerLevel::kHighPower,
|
||||
@@ -429,8 +416,7 @@ TEST_F(BleV2Test, StartScanningDiscoverButNoPeripheralLostAfterStopScanning) {
|
||||
const std::string& service_id) {
|
||||
lost_latch.CountDown();
|
||||
},
|
||||
},
|
||||
/*fast_advertisement_service_uuid=*/"");
|
||||
});
|
||||
|
||||
EXPECT_TRUE(found_latch.Await(kWaitDuration).result());
|
||||
|
||||
|
||||
@@ -39,7 +39,7 @@ bool BleV2Medium::StartAdvertising(
|
||||
|
||||
bool BleV2Medium::StopAdvertising() { return impl_->StopAdvertising(); }
|
||||
|
||||
bool BleV2Medium::StartScanning(const std::vector<std::string>& service_uuids,
|
||||
bool BleV2Medium::StartScanning(const std::string& service_uuid,
|
||||
PowerMode power_mode, ScanCallback callback) {
|
||||
MutexLock lock(&mutex_);
|
||||
if (scanning_enabled_) {
|
||||
@@ -47,7 +47,7 @@ bool BleV2Medium::StartScanning(const std::vector<std::string>& service_uuids,
|
||||
return false;
|
||||
}
|
||||
bool success = impl_->StartScanning(
|
||||
service_uuids, power_mode,
|
||||
service_uuid, power_mode,
|
||||
{
|
||||
.advertisement_found_cb =
|
||||
[this](api::ble_v2::BlePeripheral& peripheral,
|
||||
|
||||
@@ -158,7 +158,7 @@ class BleV2Medium final {
|
||||
bool StopAdvertising();
|
||||
|
||||
// Returns true once the BLE scan has been initiated.
|
||||
bool StartScanning(const std::vector<std::string>& service_uuids,
|
||||
bool StartScanning(const std::string& service_uuid,
|
||||
api::ble_v2::PowerMode power_mode, ScanCallback callback);
|
||||
bool StopScanning();
|
||||
|
||||
|
||||
@@ -36,7 +36,6 @@ using ::testing::Optional;
|
||||
constexpr absl::Duration kWaitDuration = absl::Milliseconds(1000);
|
||||
constexpr absl::string_view kAdvertisementString = "\x0a\x0b\x0c\x0d";
|
||||
constexpr absl::string_view kCopresenceServiceUuid = "F3FE";
|
||||
constexpr absl::string_view kFastAdvertisementServiceUuid = "FE2C";
|
||||
constexpr PowerMode kPowerMode(PowerMode::kHigh);
|
||||
|
||||
// A stub BlePeripheral implementation.
|
||||
@@ -84,7 +83,7 @@ TEST_F(BleV2MediumTest, CanStartFastScanningAndFastAdvertising) {
|
||||
CountDownLatch found_latch(1);
|
||||
|
||||
EXPECT_TRUE(ble_a.StartScanning(
|
||||
{std::string(kFastAdvertisementServiceUuid)}, kPowerMode,
|
||||
std::string(kCopresenceServiceUuid), kPowerMode,
|
||||
{
|
||||
.advertisement_found_cb =
|
||||
[&found_latch](BleV2Peripheral peripheral,
|
||||
@@ -95,11 +94,10 @@ TEST_F(BleV2MediumTest, CanStartFastScanningAndFastAdvertising) {
|
||||
|
||||
// Assemble fast advertising and scan response data.
|
||||
BleAdvertisementData advertising_data;
|
||||
advertising_data.service_uuids.insert(
|
||||
std::string(kFastAdvertisementServiceUuid));
|
||||
advertising_data.service_uuids.insert(std::string(kCopresenceServiceUuid));
|
||||
BleAdvertisementData scan_response_data;
|
||||
scan_response_data.service_data.insert(
|
||||
{std::string(kFastAdvertisementServiceUuid),
|
||||
{std::string(kCopresenceServiceUuid),
|
||||
ByteArray(std::string(kAdvertisementString))});
|
||||
|
||||
EXPECT_TRUE(
|
||||
@@ -119,7 +117,7 @@ TEST_F(BleV2MediumTest, CanStartScanningAndAdvertising) {
|
||||
CountDownLatch found_latch(1);
|
||||
|
||||
EXPECT_TRUE(ble_a.StartScanning(
|
||||
{std::string(kCopresenceServiceUuid)}, kPowerMode,
|
||||
std::string(kCopresenceServiceUuid), kPowerMode,
|
||||
{
|
||||
.advertisement_found_cb =
|
||||
[&found_latch](BleV2Peripheral peripheral,
|
||||
@@ -144,79 +142,6 @@ TEST_F(BleV2MediumTest, CanStartScanningAndAdvertising) {
|
||||
env_.Stop();
|
||||
}
|
||||
|
||||
TEST_F(BleV2MediumTest,
|
||||
CanStartFastAdvertisingButRegularScanningFailToFoundAdvertisement) {
|
||||
env_.Start();
|
||||
BluetoothAdapter adapter_a;
|
||||
BluetoothAdapter adapter_b;
|
||||
BleV2Medium ble_a(adapter_a);
|
||||
BleV2Medium ble_b(adapter_b);
|
||||
CountDownLatch found_latch(1);
|
||||
|
||||
EXPECT_TRUE(ble_a.StartScanning(
|
||||
{std::string(kCopresenceServiceUuid)}, kPowerMode,
|
||||
{
|
||||
.advertisement_found_cb =
|
||||
[&found_latch](BleV2Peripheral peripheral,
|
||||
const BleAdvertisementData& advertisement_data) {
|
||||
found_latch.CountDown();
|
||||
},
|
||||
}));
|
||||
|
||||
// Assemble fast advertising and scan response data.
|
||||
BleAdvertisementData advertising_data;
|
||||
advertising_data.service_uuids.insert(
|
||||
std::string(kFastAdvertisementServiceUuid));
|
||||
BleAdvertisementData scan_response_data;
|
||||
scan_response_data.service_data.insert(
|
||||
{std::string(kFastAdvertisementServiceUuid),
|
||||
ByteArray(std::string(kAdvertisementString))});
|
||||
|
||||
EXPECT_TRUE(
|
||||
ble_b.StartAdvertising(advertising_data, scan_response_data, kPowerMode));
|
||||
// Fail to found the advertiement.
|
||||
EXPECT_FALSE(found_latch.Await(kWaitDuration).result());
|
||||
EXPECT_TRUE(ble_a.StopScanning());
|
||||
EXPECT_TRUE(ble_b.StopAdvertising());
|
||||
env_.Stop();
|
||||
}
|
||||
|
||||
TEST_F(BleV2MediumTest,
|
||||
CanStartAdvertisingButFastScanningFailToFoundAdvertisement) {
|
||||
env_.Start();
|
||||
BluetoothAdapter adapter_a;
|
||||
BluetoothAdapter adapter_b;
|
||||
BleV2Medium ble_a(adapter_a);
|
||||
BleV2Medium ble_b(adapter_b);
|
||||
CountDownLatch found_latch(1);
|
||||
|
||||
EXPECT_TRUE(ble_a.StartScanning(
|
||||
{std::string(kFastAdvertisementServiceUuid)}, kPowerMode,
|
||||
{
|
||||
.advertisement_found_cb =
|
||||
[&found_latch](BleV2Peripheral peripheral,
|
||||
const BleAdvertisementData& advertisement_data) {
|
||||
found_latch.CountDown();
|
||||
},
|
||||
}));
|
||||
|
||||
// Assemble regular advertising and scan response data.
|
||||
BleAdvertisementData advertising_data = {};
|
||||
BleAdvertisementData scan_response_data;
|
||||
scan_response_data.service_uuids.insert(std::string(kCopresenceServiceUuid));
|
||||
scan_response_data.service_data.insert(
|
||||
{std::string(kCopresenceServiceUuid),
|
||||
ByteArray(std::string(kAdvertisementString))});
|
||||
|
||||
EXPECT_TRUE(
|
||||
ble_b.StartAdvertising(advertising_data, scan_response_data, kPowerMode));
|
||||
// Fail to found the advertiement.
|
||||
EXPECT_FALSE(found_latch.Await(kWaitDuration).result());
|
||||
EXPECT_TRUE(ble_a.StopScanning());
|
||||
EXPECT_TRUE(ble_b.StopAdvertising());
|
||||
env_.Stop();
|
||||
}
|
||||
|
||||
TEST_F(BleV2MediumTest, CanStartGattServer) {
|
||||
env_.Start();
|
||||
BluetoothAdapter adapter;
|
||||
|
||||
@@ -319,7 +319,7 @@ class BleMedium {
|
||||
// HIGH:
|
||||
// - Scan window = ~4096ms
|
||||
// - Scan interval = ~4096ms
|
||||
virtual bool StartScanning(const std::vector<std::string>& service_uuids,
|
||||
virtual bool StartScanning(const std::string& service_uuid,
|
||||
PowerMode power_mode, ScanCallback callback) = 0;
|
||||
|
||||
// https://developer.android.com/reference/android/bluetooth/le/BluetoothLeScanner.html#stopScan(android.bluetooth.le.ScanCallback)
|
||||
|
||||
@@ -103,13 +103,13 @@ bool BleV2Medium::StopAdvertising() {
|
||||
return true;
|
||||
}
|
||||
|
||||
bool BleV2Medium::StartScanning(const std::vector<std::string>& service_uuids,
|
||||
bool BleV2Medium::StartScanning(const std::string& service_uuid,
|
||||
PowerMode power_mode, ScanCallback callback) {
|
||||
NEARBY_LOGS(INFO) << "G3 Ble StartScanning";
|
||||
absl::MutexLock lock(&mutex_);
|
||||
|
||||
MediumEnvironment::Instance().UpdateBleV2MediumForScanning(
|
||||
/*enabled=*/true, service_uuids.front(), std::move(callback), *this);
|
||||
/*enabled=*/true, service_uuid, std::move(callback), *this);
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
@@ -42,7 +42,7 @@ class BleV2Medium : public api::ble_v2::BleMedium {
|
||||
api::ble_v2::PowerMode power_mode) override ABSL_LOCKS_EXCLUDED(mutex_);
|
||||
bool StopAdvertising() override ABSL_LOCKS_EXCLUDED(mutex_);
|
||||
|
||||
bool StartScanning(const std::vector<std::string>& service_uuids,
|
||||
bool StartScanning(const std::string& service_uuid,
|
||||
api::ble_v2::PowerMode power_mode,
|
||||
ScanCallback callback) override
|
||||
ABSL_LOCKS_EXCLUDED(mutex_);
|
||||
|
||||
@@ -216,7 +216,7 @@ bool BleV2Medium::StopAdvertising() {
|
||||
return true;
|
||||
}
|
||||
|
||||
bool BleV2Medium::StartScanning(const std::vector<std::string>& service_uuids,
|
||||
bool BleV2Medium::StartScanning(const std::string& service_uuid,
|
||||
PowerMode power_mode, ScanCallback callback) {
|
||||
NEARBY_LOGS(INFO) << "Windows Ble StartScanning";
|
||||
absl::MutexLock lock(&mutex_);
|
||||
|
||||
@@ -53,7 +53,7 @@ class BleV2Medium : public api::ble_v2::BleMedium {
|
||||
api::ble_v2::PowerMode power_mode) override ABSL_LOCKS_EXCLUDED(mutex_);
|
||||
bool StopAdvertising() override ABSL_LOCKS_EXCLUDED(mutex_);
|
||||
|
||||
bool StartScanning(const std::vector<std::string>& service_uuids,
|
||||
bool StartScanning(const std::string& service_uuid,
|
||||
api::ble_v2::PowerMode power_mode,
|
||||
ScanCallback callback) override
|
||||
ABSL_LOCKS_EXCLUDED(mutex_);
|
||||
|
||||
@@ -14,6 +14,8 @@
|
||||
|
||||
#include "internal/platform/implementation/windows/ble_v2.h"
|
||||
|
||||
#include <string>
|
||||
|
||||
#include "gtest/gtest.h"
|
||||
#include "absl/synchronization/notification.h"
|
||||
#include "internal/platform/implementation/ble_v2.h"
|
||||
@@ -53,7 +55,7 @@ TEST(BleV2Medium, DISABLED_StartScanning) {
|
||||
|
||||
BluetoothAdapter bluetoothAdapter;
|
||||
BleV2Medium blev2_medium(bluetoothAdapter);
|
||||
std::vector<std::string> service_uuids;
|
||||
std::string service_uuid;
|
||||
|
||||
api::ble_v2::BleMedium::ScanCallback callback;
|
||||
callback.advertisement_found_cb =
|
||||
@@ -65,7 +67,7 @@ TEST(BleV2Medium, DISABLED_StartScanning) {
|
||||
};
|
||||
|
||||
EXPECT_TRUE(blev2_medium.StartScanning(
|
||||
service_uuids, api::ble_v2::PowerMode::kHigh, callback));
|
||||
service_uuid, api::ble_v2::PowerMode::kHigh, callback));
|
||||
|
||||
EXPECT_TRUE(scan_response_notification.WaitForNotificationWithTimeout(
|
||||
absl::Seconds(5)));
|
||||
@@ -75,7 +77,7 @@ TEST(BleV2Medium, DISABLED_StartScanning) {
|
||||
TEST(BleV2Medium, DISABLED_StopScanning) {
|
||||
BluetoothAdapter bluetoothAdapter;
|
||||
BleV2Medium blev2_medium(bluetoothAdapter);
|
||||
std::vector<std::string> service_uuids;
|
||||
std::string service_uuid;
|
||||
|
||||
api::ble_v2::BleMedium::ScanCallback callback;
|
||||
callback.advertisement_found_cb =
|
||||
@@ -83,7 +85,7 @@ TEST(BleV2Medium, DISABLED_StopScanning) {
|
||||
const api::ble_v2::BleAdvertisementData& advertisement_data) {};
|
||||
|
||||
EXPECT_TRUE(blev2_medium.StartScanning(
|
||||
service_uuids, api::ble_v2::PowerMode::kHigh, callback));
|
||||
service_uuid, api::ble_v2::PowerMode::kHigh, callback));
|
||||
|
||||
EXPECT_TRUE(blev2_medium.StopScanning());
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user