diff --git a/internal/platform/implementation/BUILD b/internal/platform/implementation/BUILD index a9203745..1eeb0b58 100644 --- a/internal/platform/implementation/BUILD +++ b/internal/platform/implementation/BUILD @@ -101,7 +101,6 @@ cc_library( # TODO: Support WebRTC "@com_google_absl//absl/container:flat_hash_map", "@com_google_absl//absl/functional:any_invocable", - "@com_google_absl//absl/log:check", "@com_google_absl//absl/status", "@com_google_absl//absl/status:statusor", "@com_google_absl//absl/strings", diff --git a/internal/platform/implementation/apple/BUILD b/internal/platform/implementation/apple/BUILD index af77fa20..98af2cba 100644 --- a/internal/platform/implementation/apple/BUILD +++ b/internal/platform/implementation/apple/BUILD @@ -111,6 +111,7 @@ objc_library( "ble_server_socket.mm", "ble_socket.mm", "ble_utils.mm", + "bluetooth_adapter_v2.mm", "utils.mm", ], hdrs = [ @@ -118,6 +119,7 @@ objc_library( "ble_server_socket.h", "ble_socket.h", "ble_utils.h", + "bluetooth_adapter_v2.h", "utils.h", ], # Prevent Objective-C++ headers from being pulled into swift. diff --git a/internal/platform/implementation/apple/bluetooth_adapter_v2.h b/internal/platform/implementation/apple/bluetooth_adapter_v2.h new file mode 100644 index 00000000..f1c09b3e --- /dev/null +++ b/internal/platform/implementation/apple/bluetooth_adapter_v2.h @@ -0,0 +1,90 @@ +// 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. + +// Note: File language is detected using heuristics. Many Objective-C++ headers are incorrectly +// classified as C++ resulting in invalid linter errors. The use of "NSArray" and other Foundation +// classes like "NSData", "NSDictionary" and "NSUUID" are highly weighted for Objective-C and +// Objective-C++ scores. Oddly, "#import " does not contribute any points. +// This comment alone should be enough to trick the IDE in to believing this is actually some sort +// of Objective-C file. See: cs/google3/devtools/search/lang/recognize_language_classifiers_data + +#import + +#include + +#include "absl/strings/string_view.h" +#include "internal/platform/implementation/bluetooth_adapter.h" + +namespace nearby { +namespace apple { + +// Concrete implementation representing the local device Bluetooth adapter. +class BluetoothAdapter : public api::BluetoothAdapter { + public: + BluetoothAdapter() = default; + ~BluetoothAdapter() override = default; + + // Synchronously sets the status of the BluetoothAdapter to @c status, and + // returns true if the operation was a success. + bool SetStatus(api::BluetoothAdapter::Status status) override; + + // Returns true if the BluetoothAdapter's current status is @c kEnabled. + bool IsEnabled() const override; + + // Get the current Bluetooth scan mode of the local Bluetooth adapter. + // + // The Bluetooth scan mode determines if the local adapter is connectable + // and/or discoverable from remote Bluetooth devices. + // + // Possible values are: @c kNone, @c kConnectable, + // @c kConnectableDiscoverable. + // + // Returns @c kUnknown on error. + api::BluetoothAdapter::ScanMode GetScanMode() const override; + + // Synchronously sets the scan mode of the adapter, and returns true if the + // operation was a success. + bool SetScanMode(api::BluetoothAdapter::ScanMode scan_mode) override; + + // Get the friendly Bluetooth name of the local Bluetooth adapter. + // + // Returns an empty string on error + std::string GetName() const override; + + // Set and persist the friendly Bluetooth name of the local Bluetooth adapter. + // + // This name is visible to remote Bluetooth devices. + // + // Valid Bluetooth names are a maximum of 248 bytes using UTF-8 encoding, + // although many remote devices can only display the first 40 characters, and + // some may be limited to just 20. + bool SetName(absl::string_view name) override; + + // Set the friendly Bluetooth name of the local Bluetooth adapter. + // + // This name is visible to remote Bluetooth devices. + // + // Valid Bluetooth names are a maximum of 248 bytes using UTF-8 encoding, + // although many remote devices can only display the first 40 characters, and + // some may be limited to just 20. + // + // If persist is false, we will restore the original radio names when complete. + bool SetName(absl::string_view name, bool persist) override; + + // Returns BT MAC address assigned to this adapter. + std::string GetMacAddress() const override; +}; + +} // namespace apple +} // namespace nearby diff --git a/internal/platform/implementation/apple/bluetooth_adapter_v2.mm b/internal/platform/implementation/apple/bluetooth_adapter_v2.mm new file mode 100644 index 00000000..a6c6bfe7 --- /dev/null +++ b/internal/platform/implementation/apple/bluetooth_adapter_v2.mm @@ -0,0 +1,71 @@ +// 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. + +#import "internal/platform/implementation/apple/bluetooth_adapter_v2.h" + +#import + +#include + +#include "absl/strings/string_view.h" +#include "internal/platform/implementation/bluetooth_adapter.h" + +namespace nearby { +namespace apple { + +bool BluetoothAdapter::SetStatus(api::BluetoothAdapter::Status status) { + // We can't force a radio state in macOS/iOS. + // NOTE: This is supposed to return the success/failure of the change (which should always be + // "fail" for our case). However, usage in the codebase expects this to return with "success" if + // the radio is on, so we just return whether the radio is currently enabled. + return IsEnabled(); +} + +// TODO(b/290385712): Implement. +bool BluetoothAdapter::IsEnabled() const { + return true; +} + +// TODO(b/290385712): Implement. +api::BluetoothAdapter::ScanMode BluetoothAdapter::GetScanMode() const { + return api::BluetoothAdapter::ScanMode::kUnknown; +} + +// TODO(b/290385712): Implement. +bool BluetoothAdapter::SetScanMode(api::BluetoothAdapter::ScanMode scan_mode) { + return false; +} + +// TODO(b/290385712): Implement. +std::string BluetoothAdapter::GetName() const { + return ""; +} + +// TODO(b/290385712): Implement. +bool BluetoothAdapter::SetName(absl::string_view name) { + return false; +} + +// TODO(b/290385712): Implement. +bool BluetoothAdapter::SetName(absl::string_view name, bool persist) { + return false; +} + +// TODO(b/290385712): Implement. +std::string BluetoothAdapter::GetMacAddress() const { + return ""; +} + +} // namespace apple +} // namespace nearby