Thiết lập Trình quản lý kết nối
Trình quản lý kết nối xử lý tất cả các sự kiện kết nối và là giao diện cho chuyển dữ liệu giữa các thiết bị. Đây là yêu cầu cho cả quảng cáo và khám phá.
Trước tiên, bạn phải chọn Mã dịch vụ để nhận dạng duy nhất ứng dụng của mình. Tốt nhất
hãy dùng tên gói của ứng dụng (ví dụ: com.example.app
).
Tiếp theo, hãy chọn một chiến lược cho trường hợp sử dụng của bạn. Chiến lược phát hành đĩa đơn
chiến lược bạn chọn sẽ xác định cấu trúc liên kết kết nối cho ứng dụng của mình (.cluster
,
.star
.pointToPoint
).
Khởi chạy trình quản lý kết nối và triển khai các phương thức uỷ quyền của trình quản lý kết nối đó:
Swift
import NearbyConnections
class Example {
let connectionManager: ConnectionManager
init() {
connectionManager = ConnectionManager(serviceID: "com.example.app", strategy: .cluster)
connectionManager.delegate = self
}
}
extension Example: ConnectionManagerDelegate {
func connectionManager(
_ connectionManager: ConnectionManager, didReceive verificationCode: String,
from endpointID: EndpointID, verificationHandler: @escaping (Bool) -> Void) {
// Optionally show the user the verification code. Your app should call this handler
// with a value of `true` if the nearby endpoint should be trusted, or `false`
// otherwise.
verificationHandler(true)
}
func connectionManager(
_ connectionManager: ConnectionManager, didReceive data: Data,
withID payloadID: PayloadID, from endpointID: EndpointID) {
// A simple byte payload has been received. This will always include the full data.
}
func connectionManager(
_ connectionManager: ConnectionManager, didReceive stream: InputStream,
withID payloadID: PayloadID, from endpointID: EndpointID,
cancellationToken token: CancellationToken) {
// We have received a readable stream.
}
func connectionManager(
_ connectionManager: ConnectionManager,
didStartReceivingResourceWithID payloadID: PayloadID,
from endpointID: EndpointID, at localURL: URL,
withName name: String, cancellationToken token: CancellationToken) {
// We have started receiving a file. We will receive a separate transfer update
// event when complete.
}
func connectionManager(
_ connectionManager: ConnectionManager,
didReceiveTransferUpdate update: TransferUpdate,
from endpointID: EndpointID, forPayload payloadID: PayloadID) {
// A success, failure, cancelation or progress update.
}
func connectionManager(
_ connectionManager: ConnectionManager, didChangeTo state: ConnectionState,
for endpointID: EndpointID) {
switch state {
case .connecting:
// A connection to the remote endpoint is currently being established.
case .connected:
// We're connected! Can now start sending and receiving data.
case .disconnected:
// We've been disconnected from this endpoint. No more data can be sent or received.
case .rejected:
// The connection was rejected by one or both sides.
}
}
}
Bắt đầu quảng cáo
Để bắt đầu quảng cáo, hãy khởi tạo nhà quảng cáo, triển khai các phương thức uỷ quyền của họ,
và gọi đến số startAdvertising(using:)
.
Swift
import NearbyConnections
class Example {
let connectionManager: ConnectionManager
let advertiser: Advertiser
init() {
connectionManager = ConnectionManager(serviceID: "com.example.app", strategy: .cluster)
connectionManager.delegate = self
advertiser = Advertiser(connectionManager: connectionManager)
advertiser.delegate = self
// The endpoint info can be used to provide arbitrary information to the
// discovering device (e.g. device name or type).
advertiser.startAdvertising(using: "My Device".data(using: .utf8)!)
}
}
extension Example: AdvertiserDelegate {
func advertiser(
_ advertiser: Advertiser, didReceiveConnectionRequestFrom endpointID: EndpointID,
with context: Data, connectionRequestHandler: @escaping (Bool) -> Void) {
// Accept or reject any incoming connection requests. The connection will still need to
// be verified in the connection manager delegate.
connectionRequestHandler(true)
}
}
Bắt đầu khám phá
Để bắt đầu khám phá, hãy khởi chạy trình khám phá, triển khai các phương thức uỷ quyền của trình khám phá đó,
và gọi đến số startDiscovery()
.
Swift
import NearbyConnections
class Example {
let connectionManager: ConnectionManager
let discoverer: Discoverer
init() {
connectionManager = ConnectionManager(serviceID: "com.example.app", strategy: .cluster)
connectionManager.delegate = self
discoverer = Discoverer(connectionManager: connectionManager)
discoverer.delegate = self
discoverer.startDiscovery()
}
}
extension Example: DiscovererDelegate {
func discoverer(
_ discoverer: Discoverer, didFind endpointID: EndpointID, with context: Data) {
// An endpoint was found.
}
func discoverer(_ discoverer: Discoverer, didLose endpointID: EndpointID) {
// A previously discovered endpoint has gone away.
}
}