ios - Multipeer Connectivity foundDevice 两次

标签 ios swift multipeer-connectivity

我有下面的代码,我的目标是让 mac 识别具有多点连接的 iOS 设备。这在大多数情况下都有效,除了当我同时运行这两个时,我得到两个“FOUND !!!”在控制台中。我该如何解决这个问题?

这是我的 iOS 设备代码:

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

}


 }

对于苹果机:

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

    }



    }

谢谢,

最佳答案

之所以会发生这种情况,是因为设备向其他设备宣传其可用性的方式。

enter image description here

只要外围设备准备好进行通信,它们就会在数据包中发送该信息,这称为广告。基于广告数据的设计可能会在发现时返回额外的数据。

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.

欲了解更多信息,请查看 this thread .

如何解决这个问题?您可以只保留您收到的 ID 列表,并丢弃您为该特定对等 ID 获得的任何其他发现调用。

(有关 bluetooth advertising 的更多信息,请查看蓝牙文档)

关于ios - Multipeer Connectivity foundDevice 两次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40705541/

相关文章:

objective-c - 如何在iOS中的UItableview中处理100000行

ios - 在覆盖之前检查 Firebase 是否存在现有数据?

ios - swift 4 : UIButton not working

swift - NSLayoutConstraint 不起作用

ios - 在 MCSession 中设置最大对等点数

ios - 通过多点连接跟踪 NSProgress 的变化

objective-c - Objective-C 中的调解器模式 : Controller objects to perform calculations etc.

ios - 如何使用facebook sdk从iphone在facebook上分享截图

objective-c - 存在哪些第三方框架/库/实用程序来帮助使用 UITableView 创建表单?

ios - 通过蓝牙 iOS 设备之间的连接