简体   繁体   中英

Multipeer Connectivity foundDevice twice

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

Here is my code for the 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 the 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) {

    }



    }

Thanks,

This is happening because of the way devises advertise their availability to other devises.

在此输入图像描述

Whenever a peripheral is ready for communication they send that information in data packets, this is called advertising. Based on the advertising data devise may return additional data when discovering.

If the peripheral supports active scanning and the application is in the foreground, you will get two calls to the didDiscoverPeripheral. The first call contains the data in the advertising packet from the peripheral. The second call contains additional data from the scan response packet from the peripheral.

For more information checkout this thread .

How to solve this ? You can just keep a list of id you received and discard any additionally discovery calls that you get for that particular peer id.

(check out the bluetooth documentation for more info on bluetooth advertising )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM