diff --git a/fastpair/internal/ble/ble.cc b/fastpair/internal/ble/ble.cc index 6e8234b0..7edfd25e 100644 --- a/fastpair/internal/ble/ble.cc +++ b/fastpair/internal/ble/ble.cc @@ -18,10 +18,26 @@ #include #include +#include "internal/platform/ble_v2.h" +#include "internal/platform/bluetooth_adapter.h" #include "internal/platform/logging.h" namespace nearby { namespace fastpair { +namespace { +// A stub BlePeripheral implementation. +class BlePeripheralStub : public api::ble_v2::BlePeripheral { + public: + explicit BlePeripheralStub(absl::string_view ble_address) { + ble_address_ = std::string(ble_address); + } + + std::string GetAddress() const override { return ble_address_; } + + private: + std::string ble_address_; +}; +} // namespace Ble::~Ble() { // We never enabled Bluetooth, nothing to do. @@ -167,6 +183,15 @@ bool Ble::StopScanning(const std::string& service_id) { return ret; } +std::unique_ptr Ble::ConnectToGattServer( + absl::string_view ble_address) { + MutexLock lock(&mutex_); + auto v2_peripheral = std::make_unique(ble_address); + return v2_medium_.ConnectToGattServer(BleV2Peripheral(v2_peripheral.get()), + api::ble_v2::TxPowerLevel::kUnknown, + {}); +} + bool Ble::IsScanning() { MutexLock lock(&mutex_); diff --git a/fastpair/internal/ble/ble.h b/fastpair/internal/ble/ble.h index 9644140a..b1710bdf 100644 --- a/fastpair/internal/ble/ble.h +++ b/fastpair/internal/ble/ble.h @@ -16,12 +16,14 @@ #define THIRD_PARTY_NEARBY_FASTPAIR_INTERNAL_BLE_BLE_H_ #include +#include #include #include "absl/container/flat_hash_map.h" #include "absl/container/flat_hash_set.h" #include "internal/platform/atomic_boolean.h" #include "internal/platform/ble.h" +#include "internal/platform/ble_v2.h" #include "internal/platform/bluetooth_adapter.h" #include "internal/platform/multi_thread_executor.h" #include "internal/platform/mutex.h" @@ -74,6 +76,10 @@ class Ble { DiscoveredPeripheralCallback callback) ABSL_LOCKS_EXCLUDED(mutex_); + // Returns a new GattClient connection to a gatt server. + std::unique_ptr ConnectToGattServer( + absl::string_view ble_address); + // Disables Ble discovery mode. bool StopScanning(const std::string& service_id) ABSL_LOCKS_EXCLUDED(mutex_); @@ -89,6 +95,7 @@ class Ble { BluetoothAdapter adapter_; DiscoveredPeripheralCallback discovered_peripheral_callback_; BleMedium medium_ ABSL_GUARDED_BY(mutex_){adapter_}; + BleV2Medium v2_medium_ ABSL_GUARDED_BY(mutex_){adapter_}; bool is_scanning_ = false; // Same as IsAvailable(), but must be called with mutex_ held. diff --git a/fastpair/internal/ble/ble_test.cc b/fastpair/internal/ble/ble_test.cc index 63ad9aed..f4d7c0d6 100644 --- a/fastpair/internal/ble/ble_test.cc +++ b/fastpair/internal/ble/ble_test.cc @@ -18,6 +18,7 @@ #include "gtest/gtest.h" #include "internal/platform/ble.h" +#include "internal/platform/bluetooth_adapter.h" #include "internal/platform/count_down_latch.h" #include "internal/platform/medium_environment.h" @@ -114,6 +115,13 @@ TEST_F(BleTest, CanStartDiscovery) { env_.Stop(); } +TEST_F(BleTest, CannConnectToGattServer) { + env_.Start(); + Ble ble; + EXPECT_NE(ble.ConnectToGattServer("bleaddress"), nullptr); + env_.Stop(); +} + } // namespace } // namespace fastpair } // namespace nearby