diff --git a/connections/swift/NearbyConnections/Sources/Advertiser.swift b/connections/swift/NearbyConnections/Sources/Advertiser.swift index a930a9a9..bfab693c 100644 --- a/connections/swift/NearbyConnections/Sources/Advertiser.swift +++ b/connections/swift/NearbyConnections/Sources/Advertiser.swift @@ -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 = [ + .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 + /// delegate’s `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, + 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) diff --git a/connections/swift/NearbyConnections/Sources/Discoverer.swift b/connections/swift/NearbyConnections/Sources/Discoverer.swift index 3804d2d2..1d1760ee 100644 --- a/connections/swift/NearbyConnections/Sources/Discoverer.swift +++ b/connections/swift/NearbyConnections/Sources/Discoverer.swift @@ -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 = [ + .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 + /// delegate’s `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, 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, + 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) diff --git a/connections/swift/NearbyConnections/Sources/Medium.swift b/connections/swift/NearbyConnections/Sources/Medium.swift new file mode 100644 index 00000000..3d3a9140 --- /dev/null +++ b/connections/swift/NearbyConnections/Sources/Medium.swift @@ -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 +}