Enbale ble to connect to Gatt srver

PiperOrigin-RevId: 516896186
This commit is contained in:
Qin Wang
2023-03-15 12:22:44 -07:00
committed by Copybara-Service
parent fd5430764f
commit 178bfc2897
3 changed files with 40 additions and 0 deletions
+25
View File
@@ -18,10 +18,26 @@
#include <string>
#include <utility>
#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<GattClient> Ble::ConnectToGattServer(
absl::string_view ble_address) {
MutexLock lock(&mutex_);
auto v2_peripheral = std::make_unique<BlePeripheralStub>(ble_address);
return v2_medium_.ConnectToGattServer(BleV2Peripheral(v2_peripheral.get()),
api::ble_v2::TxPowerLevel::kUnknown,
{});
}
bool Ble::IsScanning() {
MutexLock lock(&mutex_);
+7
View File
@@ -16,12 +16,14 @@
#define THIRD_PARTY_NEARBY_FASTPAIR_INTERNAL_BLE_BLE_H_
#include <cstdint>
#include <memory>
#include <string>
#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<GattClient> 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.
+8
View File
@@ -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