每当我尝试使用Multipeer Connectivity Framework连接对等体时,对等体的状态会从MCSessionState.Connecting
更改为MCSessionState.NotConnected
。
以下是我运行的事件的顺序:
注意:
以下是我正在使用的代码:
import UIKit
import MultipeerConnectivity
class ViewController: UIViewController {
var advertiser: MCNearbyServiceAdvertiser!
var browser: MCNearbyServiceBrowser!
var session: MCSession!
override func viewDidLoad() {
super.viewDidLoad()
let localPeerID = MCPeerID(displayName: UIDevice.currentDevice().name)
session = MCSession(peer: localPeerID, securityIdentity: nil, encryptionPreference: MCEncryptionPreference.None)
session.delegate = self
}
@IBAction func hostTapped(sender: AnyObject) {
let localPeerID = MCPeerID(displayName: UIDevice.currentDevice().name)
advertiser = MCNearbyServiceAdvertiser(peer: localPeerID, discoveryInfo: nil, serviceType: "abc")
advertiser.delegate = self
print("started advertising")
advertiser.startAdvertisingPeer()
}
@IBAction func connectTapped(sender: AnyObject) {
let localPeerID = MCPeerID(displayName: UIDevice.currentDevice().name)
browser = MCNearbyServiceBrowser(peer: localPeerID, serviceType: "abc")
browser.delegate = self
print("started searching")
browser.startBrowsingForPeers()
}
}
extension ViewController: MCNearbyServiceAdvertiserDelegate {
func advertiser(advertiser: MCNearbyServiceAdvertiser, didReceiveInvitationFromPeer peerID: MCPeerID, withContext context: NSData?, invitationHandler: (Bool, MCSession) -> Void) {
print("accepting invitation from \(peerID.displayName)")
invitationHandler(true, session)
}
func advertiser(advertiser: MCNearbyServiceAdvertiser, didNotStartAdvertisingPeer error: NSError) {
print("did not start advertising \(error)")
}
}
extension ViewController: MCSessionDelegate {
func session(session: MCSession, peer peerID: MCPeerID, didChangeState state: MCSessionState) {
print("\(peerID.displayName) changed state: \(state.toString())")
}
func session(session: MCSession, didReceiveData data: NSData, fromPeer peerID: MCPeerID) {
}
func session(session: MCSession, didReceiveStream stream: NSInputStream, withName streamName: String, fromPeer peerID: MCPeerID) {
}
func session(session: MCSession, didStartReceivingResourceWithName resourceName: String, fromPeer peerID: MCPeerID, withProgress progress: NSProgress) {
}
func session(session: MCSession, didFinishReceivingResourceWithName resourceName: String, fromPeer peerID: MCPeerID, atURL localURL: NSURL, withError error: NSError?) {
}
func session(session: MCSession, didReceiveCertificate certificate: [AnyObject]?, fromPeer peerID: MCPeerID, certificateHandler: (Bool) -> Void) {
certificateHandler(true)
}
}
extension ViewController: MCNearbyServiceBrowserDelegate {
func browser(browser: MCNearbyServiceBrowser, foundPeer peerID: MCPeerID, withDiscoveryInfo info: [String : String]?) {
print("found \(peerID.displayName), inviting to session")
browser.invitePeer(peerID, toSession: session, withContext: nil, timeout: 30)
}
func browser(browser: MCNearbyServiceBrowser, lostPeer peerID: MCPeerID) {
print("lost \(peerID.displayName)")
}
}
extension MCSessionState {
func toString() -> String {
switch self {
case .Connected: return "Connected"
case .Connecting: return "Connecting"
case .NotConnected: return "Not Connected"
}
}
}
有一个类似的question here,但链接的代码已经过时,无法再访问了。此外,问题是正在使用相同的会话对象。
答案 0 :(得分:1)
我正在为MCSession,MCNearbyServiceAdvertiser和MCNearbyServiceBrowser使用不同的MCPeerID个实例。
为了解决这个问题,我创建了一个MCPeerID实例变量:
var localPeerID: MCPeerID?
override func viewDidLoad() {
// ...
localPeerID = MCPeerID(displayName: UIDevice.currentDevice().name)
// ...
}
...并使用它而不是将它们创建为局部变量(let localPeerID = ...
)。
现在正确地从连接到连接状态。
答案 1 :(得分:1)
总是可以将它创建为类的常量!
class ViewController: UIViewController {
var advertiser: MCNearbyServiceAdvertiser!
var browser: MCNearbyServiceBrowser!
var session: MCSession!
let localPeerID = MCPeerID(displayName: UIDevice.currentDevice().name)
...
}