mirror of
https://github.com/kidfromjupiter/nearby.git
synced 2026-09-16 15:36:12 -04:00
Add RetrieveBlePeripheralFromNativeId to BleMedium.
PiperOrigin-RevId: 835253368
This commit is contained in:
committed by
Copybara-Service
parent
dd943af8e9
commit
b45077eedb
@@ -284,4 +284,19 @@ bool BleMedium::IsExtendedAdvertisementsAvailable() {
|
||||
|
||||
bool BlePeripheral::IsValid() const { return unique_id_.has_value(); }
|
||||
|
||||
std::optional<BlePeripheral> BleMedium::RetrieveBlePeripheralFromNativeId(
|
||||
const std::string& ble_peripheral_native_id) {
|
||||
if (!IsValid()) {
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
std::optional<api::ble::BlePeripheral::UniqueId> id =
|
||||
impl_->RetrieveBlePeripheralIdFromNativeId(ble_peripheral_native_id);
|
||||
if (id.has_value()) {
|
||||
return BlePeripheral(*this, id.value());
|
||||
}
|
||||
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
} // namespace nearby
|
||||
|
||||
@@ -570,6 +570,13 @@ class BleMedium final {
|
||||
impl_->AddAlternateUuidForService(uuid, service_id);
|
||||
}
|
||||
|
||||
// Retrieves a BlePeripheral from a native BLE peripheral ID.
|
||||
// On Apple platform, the native ID is NSUUID in string format like
|
||||
// "E621E1F8-C36C-495A-93FC-0C247A3E6E5F", other platform will be MAC address
|
||||
// as string format like "0C:24:7A:3E:6E:5F".
|
||||
std::optional<BlePeripheral> RetrieveBlePeripheralFromNativeId(
|
||||
const std::string& ble_peripheral_native_id);
|
||||
|
||||
private:
|
||||
Mutex mutex_;
|
||||
std::unique_ptr<api::ble::BleMedium> impl_;
|
||||
|
||||
@@ -38,6 +38,7 @@
|
||||
#include "internal/platform/implementation/bluetooth_adapter.h"
|
||||
#include "internal/platform/implementation/g3/bluetooth_adapter.h"
|
||||
#include "internal/platform/logging.h"
|
||||
#include "internal/platform/mac_address.h"
|
||||
#include "internal/platform/medium_environment.h"
|
||||
#include "internal/platform/prng.h"
|
||||
#include "internal/platform/uuid.h"
|
||||
@@ -352,6 +353,23 @@ bool BleMedium::IsExtendedAdvertisementsAvailable() {
|
||||
return is_extended_advertisements_available_;
|
||||
}
|
||||
|
||||
std::optional<api::ble::BlePeripheral::UniqueId>
|
||||
BleMedium::RetrieveBlePeripheralIdFromNativeId(
|
||||
const std::string& ble_peripheral_native_id) {
|
||||
MacAddress mac_address;
|
||||
if (!MacAddress::FromString(ble_peripheral_native_id, mac_address)) {
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
if (MediumEnvironment::Instance()
|
||||
.FindBlePeripheral(mac_address.address())
|
||||
.IsSet()) {
|
||||
return mac_address.address();
|
||||
}
|
||||
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
BleMedium::GattServer::GattServer(
|
||||
BleMedium& medium, api::ble::ServerGattConnectionCallback callback)
|
||||
: medium_(medium), callback_(std::move(callback)) {
|
||||
|
||||
@@ -189,6 +189,10 @@ class BleMedium : public api::ble::BleMedium {
|
||||
|
||||
BluetoothAdapter& GetAdapter() { return adapter_; }
|
||||
|
||||
std::optional<api::ble::BlePeripheral::UniqueId>
|
||||
RetrieveBlePeripheralIdFromNativeId(
|
||||
const std::string& ble_peripheral_native_id) override;
|
||||
|
||||
private:
|
||||
class GattClient;
|
||||
// A concrete implementation for GattServer.
|
||||
|
||||
@@ -60,7 +60,6 @@ std::string LastAddressCandidateToString(
|
||||
}
|
||||
} // namespace
|
||||
|
||||
|
||||
MediumEnvironment& MediumEnvironment::Instance() {
|
||||
alignas(MediumEnvironment) static char storage[sizeof(MediumEnvironment)];
|
||||
static MediumEnvironment* env = new (&storage) MediumEnvironment();
|
||||
@@ -274,6 +273,27 @@ api::ble::BleMedium* MediumEnvironment::FindBleMedium(
|
||||
return device;
|
||||
}
|
||||
|
||||
api::ble::BlePeripheral MediumEnvironment::FindBlePeripheral(
|
||||
api::ble::BlePeripheral::UniqueId id) {
|
||||
api::ble::BlePeripheral peripheral;
|
||||
CountDownLatch latch(1);
|
||||
RunOnMediumEnvironmentThread([&]() {
|
||||
for (auto& item : ble_mediums_) {
|
||||
if (item.second.ble_peripheral_id == id) {
|
||||
peripheral = api::ble::BlePeripheral(item.second.ble_peripheral_id);
|
||||
break;
|
||||
}
|
||||
}
|
||||
latch.CountDown();
|
||||
});
|
||||
latch.Await();
|
||||
if (!peripheral.IsSet()) {
|
||||
LOG(INFO) << "FindBlePeripheral, not found: " << id;
|
||||
}
|
||||
|
||||
return peripheral;
|
||||
}
|
||||
|
||||
void MediumEnvironment::OnBlePeripheralStateChanged(
|
||||
bool enabled, BleMediumContext& context, const Uuid& service_id,
|
||||
const api::ble::BleAdvertisementData& ble_advertisement_data,
|
||||
|
||||
@@ -338,6 +338,9 @@ class MediumEnvironment {
|
||||
|
||||
api::ble::BleMedium* FindBleMedium(api::ble::BlePeripheral::UniqueId id);
|
||||
|
||||
api::ble::BlePeripheral FindBlePeripheral(
|
||||
api::ble::BlePeripheral::UniqueId id);
|
||||
|
||||
void RegisterGattServer(api::ble::BleMedium& medium,
|
||||
api::ble::BlePeripheral::UniqueId peripheral_id,
|
||||
Borrowable<api::ble::GattServer*> gatt_server);
|
||||
|
||||
Reference in New Issue
Block a user