연결 시작
근처 기기가 발견되면 검색기가 연결을 시작할 수 있습니다. 다음 예에서는 기기가 검색되는 즉시 기기와의 연결을 요청합니다.
Swift
extension Example: DiscovererDelegate {
func discoverer(
_ discoverer: Discoverer, didFind endpointID: EndpointID, with context: 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)!)
}
func discoverer(_ discoverer: Discoverer, didLose endpointID: EndpointID) {
// A previously discovered endpoint has gone away.
}
}
사용 사례에 따라 검색된 기기 목록을 사용자에게 표시하여 사용자가 연결할 기기를 선택할 수 있도록 하는 것이 좋습니다.
연결 수락 또는 거부
검색자가 광고주와의 연결을 요청한 후 광고주는 advertiser(_:didReceiveConnectionRequestFrom:with:connectionRequestHandler:)
위임 메서드를 통해 연결 요청에 관한 알림을 받습니다.
Swift
extension Example: AdvertiserDelegate {
func advertiser(
_ advertiser: Advertiser, didReceiveConnectionRequestFrom endpointID: EndpointID,
with context: 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
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)
}
}
양측이 동의한 경우에만 연결이 완전히 설정됩니다. 둘 중 하나 또는 둘 다 거부하면 연결이 삭제됩니다.
위의 예는 양쪽에서 자동으로 연결을 수락하는 것을 보여주지만 사용 사례에 따라 어떤 식으로든 이 옵션을 사용자에게 표시하는 것이 좋습니다.