Add API to retrieve BLE peripheral ID from native BLE ID

PiperOrigin-RevId: 773373972
This commit is contained in:
Guogang Li
2025-06-19 09:54:00 -07:00
committed by Copybara-Service
parent 611306da1d
commit 0c0a59d8a1
5 changed files with 79 additions and 10 deletions
@@ -182,6 +182,13 @@ NS_ASSUME_NONNULL_BEGIN
- (void)retrievePeripheralWithIdentifier:(NSUUID *)identifier
advertisementData:(NSDictionary<NSString *, id> *)advertisementData;
/**
* Tries to retrieve a peripheral that matches the given identifier.
*
* @param identifier The peripheral identifier.
*/
- (nullable CBPeripheral *)retrievePeripheralWithIdentifier:(NSUUID *)identifier;
/**
* Stops "no-scan" mode.
*/
@@ -143,6 +143,22 @@ static NSString *CBManagerStateString(CBManagerState state) {
}
}
- (nullable CBPeripheral *)retrievePeripheralWithIdentifier:(NSUUID *)identifier {
NSAssert(_cbCentralManager, @"CBCentralManager not created.");
NSAssert(identifier, @"Should have an identifier, self: %@", self);
NSArray<CBPeripheral *> *peripherals =
[_cbCentralManager retrievePeripheralsWithIdentifiers:@[ identifier ]];
for (CBPeripheral *peripheral in peripherals) {
if ([peripheral.identifier isEqual:identifier]) {
return peripheral;
}
}
return nil;
}
- (void)stopNoScanMode {
_advertisedServiceUUIDs = nil;
}
@@ -181,8 +197,8 @@ static NSString *CBManagerStateString(CBManagerState state) {
NSStringFromClass([self class]), self, _socketServiceUUID,
_advertisedName, _scanning ? @"YES" : @"NO",
_cbCentralScanStarted ? @"YES" : @"NO",
CBManagerStateString([self cbManagerState]),
_cbCentralManager, _centralPeerManagers];
CBManagerStateString([self cbManagerState]), _cbCentralManager,
_centralPeerManagers];
}
#pragma mark - Private
@@ -26,12 +26,12 @@
#include <vector>
#include "internal/platform/cancellation_flag.h"
#include "internal/platform/implementation/ble_v2.h"
#include "internal/platform/implementation/bluetooth_adapter.h"
#include "internal/platform/uuid.h"
#import "internal/platform/implementation/apple/ble_l2cap_server_socket.h"
#import "internal/platform/implementation/apple/ble_server_socket.h"
#import "internal/platform/implementation/apple/bluetooth_adapter_v2.h"
#include "internal/platform/implementation/ble_v2.h"
#include "internal/platform/implementation/bluetooth_adapter.h"
#include "internal/platform/uuid.h"
@class GNCBLEMedium;
@class GNSCentralManager;
@@ -164,6 +164,15 @@ class BleMedium : public api::ble_v2::BleMedium {
// This is currently always false for all Apple hardware.
bool IsExtendedAdvertisementsAvailable() override;
// Retrieves a BlePeripheral ID from a native BLE peripheral ID.
// On Apple platform, the native ID is NSUUID in string format like
// "E621E1F8-C36C-495A-93FC-0C247A3E6E5F".
//
// Returns std::nullopt if cannot retrieve the BlePeripheral from the native
// BLE peripheral id.
std::optional<api::ble_v2::BlePeripheral::UniqueId> RetrieveBlePeripheralIdFromNativeId(
const std::string &ble_peripheral_native_id) override;
private:
// A map for maintaining the set of currently known peripherals.
class PeripheralsMap {
@@ -584,6 +584,31 @@ bool BleMedium::IsExtendedAdvertisementsAvailable() {
return [medium_ supportsExtendedAdvertisements];
}
std::optional<api::ble_v2::BlePeripheral::UniqueId> BleMedium::RetrieveBlePeripheralIdFromNativeId(
const std::string &ble_peripheral_native_id) {
NSString *uuidString = [[NSString alloc] initWithUTF8String:ble_peripheral_native_id.c_str()];
if (uuidString == nil) {
GNCLoggerError(@"Empty native BLE peripheral ID is invalid.");
return std::nullopt;
}
NSUUID *uuidFromString = [[NSUUID alloc] initWithUUIDString:uuidString];
if (uuidFromString == nil) {
GNCLoggerError(@"Native BLE peripheral ID is not a valid UUID.");
return std::nullopt;
}
id<GNCPeripheral> peripheral = peripherals_.Get(uuidFromString.hash);
if (peripheral) {
// Already has the BLE peripheral, return the Nearby Connections BLE peripheral id.
return peripheral.identifier.hash;
}
// Retrieve the BLE peripheral from CBCentralManager.
peripheral = [socketCentralManager_ retrievePeripheralWithIdentifier:uuidFromString];
return peripheral.identifier.hash;
}
void BleMedium::ClearAdvertisementPacketsMap() {
absl::MutexLock lock(&advertisement_packets_mutex_);
advertisement_packets_map_.clear();
+17 -5
View File
@@ -18,6 +18,7 @@
#include <algorithm>
#include <cstdint>
#include <memory>
#include <optional>
#include <string>
#include <utility>
#include <vector>
@@ -90,8 +91,7 @@ class BlePeripheral {
BlePeripheral(UniqueId unique_id, MacAddress address)
: unique_id_(unique_id), address_(std::move(address)) {}
explicit BlePeripheral(UniqueId unique_id = 0)
: unique_id_(unique_id) {}
explicit BlePeripheral(UniqueId unique_id = 0) : unique_id_(unique_id) {}
virtual ~BlePeripheral() = default;
// https://developer.android.com/reference/android/bluetooth/BluetoothDevice#getAddress()
@@ -111,9 +111,7 @@ class BlePeripheral {
virtual UniqueId GetUniqueId() const { return unique_id_; }
// Sets platform specific data that can be retrieved by `GetPlatformData()`.
void SetPlatformData(void* platform_data) {
platform_data_ = platform_data;
}
void SetPlatformData(void* platform_data) { platform_data_ = platform_data; }
void* GetPlatformData() const { return platform_data_; }
@@ -627,6 +625,20 @@ class BleMedium {
virtual void AddAlternateUuidForService(uint16_t uuid,
const std::string& service_id) {}
// Retrieves a BlePeripheral ID from a native BLE peripheral id.
// The native ID is platform specific and is used to identify a BLE
// peripheral. On iOS, it should be NSUUID in string format. On other
// platforms, it should be the Bluetooth address in string format
// "XX:XX:XX:XX:XX:XX".
//
// Returns std::nullopt if cannot retrieve the BlePeripheral from the native
// BLE peripheral id.
virtual std::optional<BlePeripheral::UniqueId>
RetrieveBlePeripheralIdFromNativeId(
const std::string& ble_peripheral_native_id) {
return std::nullopt;
}
};
} // namespace ble_v2