gpt4 book ai didi

ios - Multipeer Connectivity foundDevice 两次

转载 作者:搜寻专家 更新时间:2023-10-31 08:25:14 25 4
gpt4 key购买 nike

我有下面的代码,我的目标是让 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com