Files
nearby/internal/platform/implementation/apple/bluetooth_adapter.h
T
Janusz Sobczak 0901aec6e2 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
2023-05-10 15:40:38 -07:00

109 lines
3.4 KiB
Objective-C

// Copyright 2022 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef THIRD_PARTY_NEARBY_INTERNAL_PLATFORM_IMPLEMENTATION_APPLE_BLUETOOTH_ADAPTER_H_
#define THIRD_PARTY_NEARBY_INTERNAL_PLATFORM_IMPLEMENTATION_APPLE_BLUETOOTH_ADAPTER_H_
#include <string>
#import "internal/platform/implementation/apple/Mediums/Ble/GNCMBleCentral.h"
#include "internal/platform/implementation/ble_v2.h"
#include "internal/platform/implementation/bluetooth_adapter.h"
#include "internal/platform/prng.h"
namespace nearby {
namespace apple {
class BluetoothAdapter;
// Concrete BlePeripheral implementation.
class BlePeripheral : public api::ble_v2::BlePeripheral {
public:
std::string GetAddress() const override;
api::ble_v2::BlePeripheral::UniqueId GetUniqueId() const override {
return unique_id_;
}
std::string GetPeripheralId() const { return peripheral_id_; }
void SetPeripheralId(const std::string& peripheral_id) {
peripheral_id_ = peripheral_id;
}
void SetConnectionRequester(GNCMBleConnectionRequester connection_requester) {
connection_requester_ = connection_requester;
}
GNCMBleConnectionRequester GetConnectionRequester() {
return connection_requester_;
}
private:
// Only BluetoothAdapter may instantiate BlePeripheral.
friend class BluetoothAdapter;
explicit BlePeripheral(BluetoothAdapter* adapter) : adapter_(*adapter) {
unique_id_ = Prng().NextInt64();
}
BluetoothAdapter& adapter_;
std::string peripheral_id_;
GNCMBleConnectionRequester connection_requester_;
api::ble_v2::BlePeripheral::UniqueId unique_id_;
};
// Concrete BluetoothAdapter implementation.
class BluetoothAdapter : public api::BluetoothAdapter {
public:
using Status = api::BluetoothAdapter::Status;
using ScanMode = api::BluetoothAdapter::ScanMode;
~BluetoothAdapter() override { SetStatus(Status::kDisabled); }
bool SetStatus(Status status) override {
enabled_ = status == Status::kEnabled;
return true;
}
bool IsEnabled() const override { return enabled_; }
ScanMode GetScanMode() const override { return mode_; }
bool SetScanMode(ScanMode mode) override { return false; }
std::string GetName() const override { return name_; }
bool SetName(absl::string_view name) {
return SetName(name, /* persist= */ true);
}
bool SetName(absl::string_view name, bool persist) override {
name_ = std::string(name);
return true;
}
std::string GetMacAddress() const override { return mac_address_; }
void SetMacAddress(absl::string_view mac_address) {
mac_address_ = std::string(mac_address);
}
BlePeripheral& GetPeripheral() { return peripheral_; }
private:
BlePeripheral peripheral_{this};
ScanMode mode_ = ScanMode::kNone;
std::string name_;
std::string mac_address_;
bool enabled_ = true;
};
} // namespace apple
} // namespace nearby
#endif // THIRD_PARTY_NEARBY_INTERNAL_PLATFORM_IMPLEMENTATION_APPLE_BLUETOOTH_ADAPTER_H_