[BLE Refactor] Removes fast advertisement uuid in StartAdvertising and StartScanning.

PiperOrigin-RevId: 448426759
This commit is contained in:
edwinwu
2022-05-13 00:19:16 -07:00
committed by Copybara-Service
parent 080e964a4c
commit eddf53ec29
14 changed files with 68 additions and 180 deletions
+2 -2
View File
@@ -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,
+1 -1
View File
@@ -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();
+4 -79
View File
@@ -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;
+1 -1
View File
@@ -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;
}
+1 -1
View File
@@ -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());
}