mirror of
https://github.com/kidfromjupiter/nearby.git
synced 2026-09-16 15:36:12 -04:00
Add g3 implementation for BluetoothPairing
PiperOrigin-RevId: 530965844
This commit is contained in:
committed by
Copybara-Service
parent
69d2edaea9
commit
69f4feaf55
@@ -15,8 +15,10 @@
|
||||
#include "internal/platform/implementation/g3/bluetooth_classic.h"
|
||||
|
||||
#include <memory>
|
||||
#include <optional>
|
||||
#include <string>
|
||||
|
||||
#include "absl/strings/escaping.h"
|
||||
#include "absl/synchronization/mutex.h"
|
||||
#include "internal/platform/cancellation_flag_listener.h"
|
||||
#include "internal/platform/implementation/bluetooth_classic.h"
|
||||
@@ -176,6 +178,36 @@ Exception BluetoothServerSocket::DoClose() {
|
||||
return {Exception::kSuccess};
|
||||
}
|
||||
|
||||
BluetoothPairing::BluetoothPairing(api::BluetoothDevice& remote_device)
|
||||
: remote_device_(remote_device) {}
|
||||
|
||||
BluetoothPairing::~BluetoothPairing() {
|
||||
MediumEnvironment::Instance().ClearBluetoothDevicesForPairing();
|
||||
}
|
||||
|
||||
bool BluetoothPairing::InitiatePairing(
|
||||
api::BluetoothPairingCallback pairing_cb) {
|
||||
return MediumEnvironment::Instance().InitiatePairing(&remote_device_,
|
||||
std::move(pairing_cb));
|
||||
}
|
||||
|
||||
bool BluetoothPairing::FinishPairing(
|
||||
std::optional<absl::string_view> pin_code) {
|
||||
return MediumEnvironment::Instance().FinishPairing(&remote_device_);
|
||||
}
|
||||
|
||||
bool BluetoothPairing::CancelPairing() {
|
||||
return MediumEnvironment::Instance().CancelPairing(&remote_device_);
|
||||
}
|
||||
|
||||
bool BluetoothPairing::Unpair() {
|
||||
return MediumEnvironment::Instance().SetPairingState(&remote_device_, false);
|
||||
}
|
||||
|
||||
bool BluetoothPairing::IsPaired() {
|
||||
return MediumEnvironment::Instance().IsPaired(&remote_device_);
|
||||
}
|
||||
|
||||
BluetoothClassicMedium::BluetoothClassicMedium(api::BluetoothAdapter& adapter)
|
||||
// TODO(apolyudov): implement and use downcast<> with static assertions.
|
||||
: adapter_(static_cast<BluetoothAdapter*>(&adapter)) {
|
||||
@@ -273,14 +305,18 @@ BluetoothClassicMedium::ListenForService(const std::string& service_name,
|
||||
|
||||
std::unique_ptr<api::BluetoothPairing> BluetoothClassicMedium::CreatePairing(
|
||||
api::BluetoothDevice& remote_device) {
|
||||
// TODO(b/279964840): Add g3 implementation for BluetoothPairing.
|
||||
return nullptr;
|
||||
return std::make_unique<BluetoothPairing>(remote_device);
|
||||
}
|
||||
|
||||
api::BluetoothDevice* BluetoothClassicMedium::GetRemoteDevice(
|
||||
const std::string& mac_address) {
|
||||
std::string bt_mac_address = mac_address;
|
||||
// Remove the colon delimiters.
|
||||
bt_mac_address.erase(
|
||||
std::remove(bt_mac_address.begin(), bt_mac_address.end(), ':'),
|
||||
bt_mac_address.end());
|
||||
auto& env = MediumEnvironment::Instance();
|
||||
return env.FindBluetoothDevice(mac_address);
|
||||
return env.FindBluetoothDevice(absl::HexStringToBytes(bt_mac_address));
|
||||
}
|
||||
|
||||
void BluetoothClassicMedium::AddObserver(
|
||||
|
||||
@@ -16,6 +16,7 @@
|
||||
#define PLATFORM_IMPL_G3_BLUETOOTH_CLASSIC_H_
|
||||
|
||||
#include <memory>
|
||||
#include <optional>
|
||||
#include <string>
|
||||
|
||||
#include "absl/container/flat_hash_map.h"
|
||||
@@ -169,6 +170,24 @@ class BluetoothServerSocket : public api::BluetoothServerSocket {
|
||||
bool closed_ ABSL_GUARDED_BY(mutex_) = false;
|
||||
};
|
||||
|
||||
// A concrete implementation for BluetoothPairing.
|
||||
class BluetoothPairing : public api::BluetoothPairing {
|
||||
public:
|
||||
explicit BluetoothPairing(api::BluetoothDevice& remote_device);
|
||||
BluetoothPairing(const BluetoothPairing&) = default;
|
||||
BluetoothPairing& operator=(const BluetoothPairing&) = default;
|
||||
~BluetoothPairing() override;
|
||||
|
||||
bool InitiatePairing(api::BluetoothPairingCallback pairing_cb) override;
|
||||
bool FinishPairing(std::optional<absl::string_view> pin_code) override;
|
||||
bool CancelPairing() override;
|
||||
bool Unpair() override;
|
||||
bool IsPaired() override;
|
||||
|
||||
private:
|
||||
api::BluetoothDevice& remote_device_;
|
||||
};
|
||||
|
||||
// Container of operations that can be performed over the Bluetooth Classic
|
||||
// medium.
|
||||
class BluetoothClassicMedium : public api::BluetoothClassicMedium {
|
||||
|
||||
Reference in New Issue
Block a user