4
我有下面的代碼,我的目標是讓Mac識別具有Multipeer Connectivity的iOS設備。這大部分工作,除了當我運行這兩個我得到兩個「發現!!!」在控制檯。我怎樣才能解決這個問題?Multipeer Connectivity foundDevice兩次
這裏是我的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) {
}
}
,以及在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) {
}
}
感謝,
有在開發過程中改變任一設備的節點ID顯示名稱?當我使用類似的Apple感應技術iBeacon時,如果更改了名稱/對等ID,則會導致內核中的映射重複到單個設備,從而導致對一個設備進行兩次檢測。嘗試從每臺設備中刪除應用,然後查看問題是否仍然存在。 – Midas