Update the assigned mediums and leave those unassigned as the default false.

PiperOrigin-RevId: 729383663
This commit is contained in:
hai007
2025-02-20 22:26:57 -08:00
committed by Copybara-Service
parent fdb0f0f351
commit 51607dc548
3 changed files with 119 additions and 0 deletions
@@ -25,6 +25,11 @@ public class Advertiser {
/// The connection manager for this instance.
public let connectionManager: ConnectionManager
/// The set of all mediums that can be used for advertising.
let allMediums: Set<Medium> = [
.bluetooth, .ble, .webRTC, .wifiLAN, .wifiHotspot, .wifiDirect,
]
lazy var connection: InternalConnection? = {
let connection = InternalConnection()
connection.delegate = self
@@ -52,7 +57,39 @@ public class Advertiser {
/// failed to start.
///
public func startAdvertising(using context: Data, completionHandler: ((Error?) -> Void)? = nil) {
startAdvertising(using: context, mediums: allMediums, completionHandler: completionHandler)
}
/// Starts advertising the local endpoint.
///
/// After this method is called (until you call `stopAdvertising()`), the framework calls your
/// delegates `advertiser(_:didReceiveConnectionRequestFrom:with:connectionRequestHandler:)`
/// method when remote endpoints request a connection.
///
/// - Parameters:
/// - context: An arbitrary piece of data that is advertised to the nearby endpoint.
/// This can be used to provide further information to the user about the nature of the
/// advertisement.
/// - mediums: The mediums to be used for advertising.
/// - completionHandler: Called with `nil` if advertising started, or an error if advertising
/// failed to start.
public func startAdvertising(
using context: Data, mediums: Set<Medium>,
completionHandler: ((Error?) -> Void)? = nil
) {
let options = GNCAdvertisingOptions(strategy: connectionManager.strategy.objc)
// Update the advertising mediums based on the user selection
options.mediums = GNCSupportedMediums(
bluetooth: mediums.contains(.bluetooth),
ble: mediums.contains(.ble),
webRTC: mediums.contains(.webRTC),
wifiLAN: mediums.contains(.wifiLAN),
wifiHotspot: mediums.contains(.wifiHotspot),
wifiDirect: mediums.contains(.wifiDirect)
)
// Start advertising with the options
GNCCoreAdapter.shared.startAdvertising(
asService: connectionManager.serviceID, endpointInfo: context,
options: options, delegate: connection, withCompletionHandler: completionHandler)
@@ -24,6 +24,11 @@ public class Discoverer {
/// The connection manager for this instance.
public let connectionManager: ConnectionManager
/// The set of all mediums that can be used for discovering and connecting.
let allMediums: Set<Medium> = [
.bluetooth, .ble, .webRTC, .wifiLAN, .wifiHotspot, .wifiDirect,
]
lazy var connection: InternalConnection? = {
let connection = InternalConnection()
connection.delegate = self
@@ -52,7 +57,33 @@ public class Discoverer {
/// - Parameter completionHandler: Called with `nil` if discovery starts, or an error if
/// discovery failed to start.
public func startDiscovery(completionHandler: ((Error?) -> Void)? = nil) {
startDiscovery(mediums: allMediums, completionHandler: completionHandler)
}
/// Starts searching for nearby remote endpoints.
///
/// After this method is called (until you call `stopDiscovery()`), the framework calls your
/// delegates `discoverer(_:didFind:with:)` and `discoverer(_:didLose:)` methods as new endpoints
/// are found and lost.
///
/// - Parameters:
/// - mediums: The mediums to be used for discovery.
/// - completionHandler: Called with `nil` if discovery starts, or an error if
/// discovery failed to start.
public func startDiscovery(mediums: Set<Medium>, completionHandler: ((Error?) -> Void)? = nil) {
let options = GNCDiscoveryOptions(strategy: connectionManager.strategy.objc)
// Update the discovery mediums based on the user selection and set the default values for
// the mediums that could not be selected by the user
options.mediums = GNCSupportedMediums(
bluetooth: mediums.contains(.bluetooth),
ble: mediums.contains(.ble),
webRTC: mediums.contains(.webRTC),
wifiLAN: mediums.contains(.wifiLAN),
wifiHotspot: mediums.contains(.wifiHotspot),
wifiDirect: mediums.contains(.wifiDirect)
)
GNCCoreAdapter.shared.startDiscovery(
asService: connectionManager.serviceID, options: options, delegate: discovery,
withCompletionHandler: completionHandler
@@ -77,8 +108,35 @@ public class Discoverer {
/// or an error if disconnecting failed
public func requestConnection(
to endpointID: EndpointID, using context: Data, completionHandler: ((Error?) -> Void)? = nil
) {
requestConnection(
to: endpointID, using: context, mediums: allMediums,
completionHandler: completionHandler)
}
/// Requests a connection to a discovered remote endpoint.
///
/// - Parameters:
/// - endpointID: The ID of the endpoint to request a connection to.
/// - context: An arbitrary piece of data that is passed to the nearby endpoint. This can be
/// used to provide further information to the user about the nature of the invitation.
/// - mediums: The mediums to be used for connection.
/// - completionHandler: Called with `nil` when the endpoint has been disconnected,
/// or an error if disconnecting failed
public func requestConnection(
to endpointID: EndpointID, using context: Data, mediums: Set<Medium>,
completionHandler: ((Error?) -> Void)? = nil
) {
let options = GNCConnectionOptions()
options.mediums = GNCSupportedMediums(
bluetooth: mediums.contains(.bluetooth),
ble: mediums.contains(.ble),
webRTC: mediums.contains(.webRTC),
wifiLAN: mediums.contains(.wifiLAN),
wifiHotspot: mediums.contains(.wifiHotspot),
wifiDirect: mediums.contains(.wifiDirect)
)
GNCCoreAdapter.shared.requestConnection(
toEndpoint: endpointID, endpointInfo: context,
options: options, delegate: connection, withCompletionHandler: completionHandler)
@@ -0,0 +1,24 @@
// 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.
/// Indicates the mediums for advertisement, discovery or connection for the communication
/// with the NearbyConnections caller.
public enum Medium {
case bluetooth
case ble
case webRTC
case wifiLAN
case wifiHotspot
case wifiDirect
}