कनेक्शन प्रबंधित करें

कनेक्ट करें

जब आस-पास के डिवाइस मिलते हैं, तो खोजने वाला व्यक्ति कनेक्शन शुरू कर सकता है. निम्न उदाहरण डिवाइस के पता चलते ही उससे कनेक्ट करने का अनुरोध करता है.

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)
  }
}

दोनों तरफ़ से स्वीकार किए जाने पर ही पूरी तरह से कनेक्शन बनाया जाता है. अगर एक या दोनों अस्वीकार कर दिए जाते हैं, तो कनेक्शन को खारिज कर दिया जाता है.

ऊपर दिए गए उदाहरण बताते हैं कि दोनों तरफ़ से कनेक्शन अपने-आप स्वीकार होता है. हालांकि, आपके इस्तेमाल के उदाहरण के आधार पर, यह चुना जा सकता है कि यह विकल्प उपयोगकर्ता को किसी तरीके से दिखाना है.