- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我有下面的代码,我的目标是让 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) {
}
}
谢谢,
最佳答案
之所以会发生这种情况,是因为设备向其他设备宣传其可用性的方式。
只要外围设备准备好进行通信,它们就会在数据包中发送该信息,这称为广告。基于广告数据的设计可能会在发现时返回额外的数据。
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/
我有下面的代码,我的目标是让 mac 识别具有多点连接的 iOS 设备。这在大多数情况下都有效,除了当我同时运行这两个时,我得到两个“FOUND !!!”在控制台中。我该如何解决这个问题? 这是我的
我是一名优秀的程序员,十分优秀!