Multipeer Connectivity foundDevice twice

I have the code below and my goal was to get a mac to recognize an iOS device using Multipeer Connectivity. This worked for the most part, except that when I run both of them, I get two "FOUND !!!" in the console. How can i fix this?

Here is my code for an iOS device:

import UIKit
import MultipeerConnectivity

class ViewController: UIViewController, MCNearbyServiceBrowserDelegate,      MCNearbyServiceAdvertiserDelegate {


let browser = MCNearbyServiceBrowser(peer: MCPeerID(displayName: "iOS Device"), serviceType: "example-test")
let peerID = MCPeerID(displayName: "iOS Device")
let advertiser = MCNearbyServiceAdvertiser(peer: MCPeerID(displayName: "iOS Device"), discoveryInfo: nil, serviceType: "example-test")



override func viewDidLoad() {
    super.viewDidLoad()
    advertiser.delegate = self
    advertiser.startAdvertisingPeer()
    browser.delegate = self
    browser.startBrowsingForPeers()
}

func browser(_ browser: MCNearbyServiceBrowser, lostPeer peerID: MCPeerID) {

}

func browser(_ browser: MCNearbyServiceBrowser, didNotStartBrowsingForPeers error: Error) {

}

func browser(_ browser: MCNearbyServiceBrowser, foundPeer peerID: MCPeerID, withDiscoveryInfo info: [String : String]?) {
    print("FOUND!!!")
}

func advertiser(_ advertiser: MCNearbyServiceAdvertiser, didNotStartAdvertisingPeer error: Error) {

}

func advertiser(_ advertiser: MCNearbyServiceAdvertiser, didReceiveInvitationFromPeer peerID: MCPeerID, withContext context: Data?, invitationHandler: @escaping (Bool, MCSession?) -> Void) {

}


 }

And for mac:

import MultipeerConnectivity

class ConnectionsManager: NSObject, MCNearbyServiceBrowserDelegate,     MCNearbyServiceAdvertiserDelegate {


let browser : MCNearbyServiceBrowser
let advertiser: MCNearbyServiceAdvertiser
let peerID = MCPeerID(displayName: "macDevice")



override init() {
    advertiser = MCNearbyServiceAdvertiser(peer: MCPeerID(displayName: "mac Device"), discoveryInfo: nil, serviceType: "example-test")
    browser = MCNearbyServiceBrowser(peer: MCPeerID(displayName: "mac Device"), serviceType: "example-test")
    super.init()
    advertiser.delegate = self
    advertiser.startAdvertisingPeer()
    browser.delegate = self
    browser.startBrowsingForPeers()
}
deinit {
    browser.stopBrowsingForPeers()
    advertiser.stopAdvertisingPeer()
}

    func browser(_ browser: MCNearbyServiceBrowser, lostPeer peerID: MCPeerID) {

    }

    func browser(_ browser: MCNearbyServiceBrowser, didNotStartBrowsingForPeers error: Error) {

    }

    func browser(_ browser: MCNearbyServiceBrowser, foundPeer peerID: MCPeerID, withDiscoveryInfo info: [String : String]?) {
        print("FOUND!!!")
    }

    func advertiser(_ advertiser: MCNearbyServiceAdvertiser, didNotStartAdvertisingPeer error: Error) {

    }

    func advertiser(_ advertiser: MCNearbyServiceAdvertiser, didReceiveInvitationFromPeer peerID: MCPeerID, withContext context: Data?, invitationHandler: @escaping (Bool, MCSession?) -> Void) {

    }



    }

Thank,

+4
source share
1 answer

This is due to how developers advertise their accessibility to other devices.

, , , . .

, didDiscoverPeripheral. . .

.

? , , , peer.

( bluetooth bluetooth)

+5

Source: https://habr.com/ru/post/1661353/


All Articles