근처 기기가 발견되면 발견자가 연결을 시작할 수 있습니다. 이
다음 예는 기기가 대기하는 즉시 기기와의 연결을
있습니다.
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.}}
사용 사례에 따라 탐색된 항목 목록을 대신 표시하고자 할 수 있습니다.
사용자에게 연결할 수 있으며, 사용자는 연결할 기기를 선택할 수 있습니다.
연결 수락 또는 거부
발견자가 광고주에 대한 연결을 요청하면 광고주는
를 통해 연결 요청에 대한 알림을 받게 됩니다.
advertiser(_:didReceiveConnectionRequestFrom:with:connectionRequestHandler:)
대리자 메서드로 전달하세요.
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)}}
광고주가 수락하면 양측에 알림이 전달되고
connectionManager(_:didReceive:from:verificationHandler:)을(를) 통한 연결
대리자 메서드로 전달하세요.
앱에서
인증 코드를 받습니다. 이를 통해
사용자는 자신이 의도한 기기에 연결되고 있는지 확인합니다. 두 기기 모두
동일한 코드가 주어지면 임의의 짧은 문자열입니다. 그것은 당신에게 달려 있습니다.
확인하는 방법에 대해 알아보겠습니다. 일반적으로 그러려면 두 장치에 토큰을 표시하고
블루투스 페어링과 유사하게 사용자에게 직접 비교 및 확인 요청
대화상자
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)}}
양측이 수락한 경우에만 연결이 완전히 설정됩니다. 만약
둘 다 거부되면 연결이 삭제됩니다.
위의 예는 둘 다에서 자동으로 연결을 수락하는 것을 보여줍니다.
그러나 사용 사례에 따라 이 선택사항을
어떤 식으로든 말이죠.
[null,null,["최종 업데이트: 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."]]