Add BleMedium::GetRemotePeripheral

Added:
nearby::api::ble_v2::BlePeripheral::GetUniqueId()
nearby::api::ble_v2::BleMedium::GetRemotePeripheral(...)

Moved BlePeripheral implementation from bluetooth_adapter_* to ble_v2.*
Added G3 (test) implementation for the new methods.

PiperOrigin-RevId: 531023568
This commit is contained in:
Janusz Sobczak
2023-05-10 15:40:38 -07:00
committed by Copybara-Service
parent 69f4feaf55
commit 0901aec6e2
29 changed files with 597 additions and 354 deletions
@@ -26,6 +26,7 @@
#include "absl/strings/str_cat.h"
#include "absl/strings/str_format.h"
#include "absl/synchronization/mutex.h"
#include "internal/platform/bluetooth_adapter.h"
#include "internal/platform/cancellation_flag.h"
#include "internal/platform/cancellation_flag_listener.h"
#include "internal/platform/implementation/ble_v2.h"
@@ -842,5 +843,34 @@ void BleV2Medium::AdvertisementReceivedHandler(
}
}
bool BleV2Medium::GetRemotePeripheral(absl::string_view mac_address,
GetRemotePeripheralCallback callback) {
for (auto& item : peripherals_) {
if (item.second->GetAddress() == mac_address) {
callback(*(item.second));
return true;
}
}
auto peripheral = std::make_unique<BleV2Peripheral>();
peripheral->SetAddress(mac_address);
if (peripheral->GetUniqueId() == 0) {
return false;
}
BleV2Peripheral* ptr = peripheral.get();
peripherals_[peripheral->GetUniqueId()] = std::move(peripheral);
callback(*ptr);
return true;
}
bool BleV2Medium::GetRemotePeripheral(api::ble_v2::BlePeripheral::UniqueId id,
GetRemotePeripheralCallback callback) {
auto it = peripherals_.find(id);
if (it == peripherals_.end()) {
return false;
}
callback(*(it->second));
return true;
}
} // namespace windows
} // namespace nearby
@@ -73,6 +73,12 @@ class BleV2Medium : public api::ble_v2::BleMedium {
BluetoothAdapter& GetAdapter() { return *adapter_; }
bool GetRemotePeripheral(absl::string_view mac_address,
GetRemotePeripheralCallback callback) override;
bool GetRemotePeripheral(api::ble_v2::BlePeripheral::UniqueId id,
GetRemotePeripheralCallback callback) override;
private:
bool StartBleAdvertising(
const api::ble_v2::BleAdvertisementData& advertising_data,
@@ -127,6 +133,10 @@ class BleV2Medium : public api::ble_v2::BleMedium {
::winrt::event_token advertisement_received_token_;
BleGattServer* ble_gatt_server_ = nullptr;
absl::flat_hash_map<BleV2Peripheral::UniqueId,
std::unique_ptr<BleV2Peripheral>>
peripherals_;
};
} // namespace windows
@@ -19,6 +19,7 @@
#include "absl/strings/string_view.h"
#include "internal/platform/implementation/ble_v2.h"
#include "internal/platform/prng.h"
namespace nearby {
namespace windows {
@@ -27,18 +28,23 @@ namespace windows {
// about a particular BLE device to connect to its GATT server.
class BleV2Peripheral : public api::ble_v2::BlePeripheral {
public:
BleV2Peripheral() { unique_id_ = Prng().NextInt64(); }
~BleV2Peripheral() override = default;
// Returns the MAC address of the peripheral. The format is in
// "00:B0:D0:63:C2:26".
std::string GetAddress() const override { return address_; }
api::ble_v2::BlePeripheral::UniqueId GetUniqueId() const override {
return unique_id_;
}
// Sets the MAC address of the peripheral. The address format must be in
// pattern of "00:B0:D0:63:C2:26".
bool SetAddress(absl::string_view address);
private:
std::string address_;
api::ble_v2::BlePeripheral::UniqueId unique_id_;
};
} // namespace windows