Sử dụng bộ sưu tập để sắp xếp ngăn nắp các trang
Lưu và phân loại nội dung dựa trên lựa chọn ưu tiên của bạn.
Bắt đầu kết nối
Khi tìm thấy thiết bị ở gần, người khám phá có thể bắt đầu kết nối. Chiến lược phát hành đĩa đơn
ví dụ sau đây yêu cầu kết nối với một thiết bị ngay khi thiết bị đó được kết nối
phát hiện.
Swift
extensionExample:DiscovererDelegate{funcdiscoverer(_discoverer:Discoverer,didFindendpointID:EndpointID,withcontext:Data){// An endpoint was found. We request a connection to it. The endpoint info can be used// to provide arbitrary information to the discovering device (e.g. device name or type).discoverer.requestConnection(to:endpointID,using:"My Device".data(using:.utf8)!)}funcdiscoverer(_discoverer:Discoverer,didLoseendpointID:EndpointID){// A previously discovered endpoint has gone away.}}
Tuỳ thuộc vào trường hợp sử dụng, bạn nên hiển thị danh sách
thiết bị nào cho người dùng, cho phép họ chọn thiết bị cần kết nối.
Chấp nhận hoặc từ chối kết nối
Sau khi người khám phá đã yêu cầu kết nối với một nhà quảng cáo, nhà quảng cáo đó
sẽ được thông báo về yêu cầu kết nối qua
advertiser(_:didReceiveConnectionRequestFrom:with:connectionRequestHandler:)
uỷ quyền.
Swift
extensionExample:AdvertiserDelegate{funcadvertiser(_advertiser:Advertiser,didReceiveConnectionRequestFromendpointID:EndpointID,withcontext:Data,connectionRequestHandler:@escaping(Bool)->Void){// Call with `true` to accept or `false` to reject the incoming connection request.connectionRequestHandler(true)}}
Khi nhà quảng cáo chấp nhận, cả hai bên đều được thông báo và phải xác minh thông tin
kết nối qua connectionManager(_:didReceive:from:verificationHandler:)
uỷ quyền.
Bạn nên xác minh kết nối ứng dụng bằng cách sử dụng
mã xác minh do phương thức ủy quyền cung cấp. Đây là cách để cho phép
người dùng xác nhận rằng họ đang kết nối với thiết bị dự định. Cả hai thiết bị đều
cho cùng một mã, đó là một chuỗi ngắn ngẫu nhiên; điều này tuỳ thuộc vào bạn quyết định
cách xác minh tài khoản. Thông thường, hoạt động này bao gồm việc hiển thị mã thông báo trên cả hai thiết bị và
yêu cầu người dùng so sánh và xác nhận theo cách thủ công, tương tự như ghép nối Bluetooth
.
Swift
extensionExample:ConnectionManagerDelegate{funcconnectionManager(_connectionManager:ConnectionManager,didReceiveverificationCode:String,fromendpointID: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)}}
Kết nối chỉ được thiết lập đầy đủ sau khi cả hai bên đều chấp nhận. Nếu có
hoặc từ chối cả hai, kết nối đều bị huỷ.
Các ví dụ ở trên cho thấy kết nối được cả hai tự động chấp nhận
nhưng tuỳ thuộc vào trường hợp sử dụng của mình, bạn có thể phải đưa ra lựa chọn này cho
người dùng theo một cách nào đó.
[null,null,["Cập nhật lần gần đây nhất: 2025-08-13 UTC."],[[["\u003cp\u003eNearby Connections API enables devices to discover each other and initiate connections for data exchange.\u003c/p\u003e\n"],["\u003cp\u003eUpon discovery, the discovering device can request a connection, which the advertising device can accept or reject.\u003c/p\u003e\n"],["\u003cp\u003eSecure connections require mutual verification using a code, typically displayed to users for confirmation.\u003c/p\u003e\n"],["\u003cp\u003eConnection establishment involves both sides accepting the request and verifying, if enabled, before data transfer can begin.\u003c/p\u003e\n"],["\u003cp\u003e\u003cstrong\u003eCaution:\u003c/strong\u003e Connections without verification are insecure and vulnerable to exploitation, prioritize secure practices for robust protection.\u003c/p\u003e\n"]]],[],null,["# Manage connections\n\nInitiate a connection\n---------------------\n\nWhen nearby devices are found, the discoverer can initiate connections. The\nfollowing example requests a connection with a device as soon as it is\ndiscovered. \n\n### Swift\n\n extension Example: DiscovererDelegate {\n func discoverer(\n _ discoverer: Discoverer, didFind endpointID: EndpointID, with context: Data) {\n // An endpoint was found. We request a connection to it. The endpoint info can be used\n // to provide arbitrary information to the discovering device (e.g. device name or type).\n discoverer.requestConnection(to: endpointID, using: \"My Device\".data(using: .utf8)!)\n }\n\n func discoverer(_ discoverer: Discoverer, didLose endpointID: EndpointID) {\n // A previously discovered endpoint has gone away.\n }\n }\n\nDepending on your use case, you may wish to instead display a list of discovered\ndevices to the user, allowing them to choose which devices to connect to.\n\nAccept or reject a connection\n-----------------------------\n\nAfter the discoverer has requested a connection to an advertiser, the advertiser\nis notified of the connection request via the\n`advertiser(_:didReceiveConnectionRequestFrom:with:connectionRequestHandler:)`\ndelegate method. \n\n### Swift\n\n extension Example: AdvertiserDelegate {\n func advertiser(\n _ advertiser: Advertiser, didReceiveConnectionRequestFrom endpointID: EndpointID,\n with context: Data, connectionRequestHandler: @escaping (Bool) -\u003e Void) {\n // Call with `true` to accept or `false` to reject the incoming connection request.\n connectionRequestHandler(true)\n }\n }\n\nOnce the advertiser accepts, both side are notified and must verify the\nconnection via the `connectionManager(_:didReceive:from:verificationHandler:)`\ndelegate method.\n\nIt is recommended that your app verifies the connection by using the\nverification code provided by the delegate method. This provides a way to let\nusers confirm that they are connecting to the intended device. Both devices are\ngiven the same code, which is a short random string; it's up to you to decide\nhow to verify it. Typically this involves showing the token on both devices and\nasking the users to manually compare and confirm, similar to a bluetooth pairing\ndialog. \n\n### Swift\n\n extension Example: ConnectionManagerDelegate {\n func connectionManager(\n _ connectionManager: ConnectionManager, didReceive verificationCode: String,\n from endpointID: EndpointID, verificationHandler: @escaping (Bool) -\u003e Void) {\n // Optionally show the user the verification code. Your app should call this handler\n // with a value of `true` if the nearby endpoint should be trusted, or `false`\n // otherwise.\n verificationHandler(true)\n }\n }\n\nThe connection is fully established only when both sides have accepted. If one\nor both reject, the connection is discarded.\n\nThe above examples shows the connection being automatically accepted by both\nsides, but depending on your use case you may wish to present this choice to the\nuser in some way.\n| **Caution:** While verification is optional, connections established without verification are insecure, and can expose devices to severe security vulnerabilities. To avoid this, always use verification to secure your connections."]]