简体   繁体   中英

PeerID object received by another peer is different that the original object in MultipeerConnectivity Framework

I am using Multipeer Connectivity framework. I am saving my PeerId in NSUserDefaults to reuse it (as suggested by Apple) like this (following code is in swift but Objective c has the same behaviour as well). :

        var peerId: MCPeerID = {
        let defaults = NSUserDefaults.standardUserDefaults();
        let dataToShow = defaults.dataForKey("kPeerID");
        var peer = NSKeyedUnarchiver.unarchiveObjectWithData(dataToShow) as? MCPeerID;
        if peer == nil {
            peer = MCPeerID(displayName: UIDevice.currentDevice().name);
            let data: NSData = NSKeyedArchiver.archivedDataWithRootObject(peer);
            defaults.setObject(data, forKey: "kPeerID");
            defaults.synchronize();
        }
        println("I am peer: \(peer!)");
        return peer!;
    }()

I send the invitation to the discovered peer like this:

func browser(browser: MCNearbyServiceBrowser!, foundPeer peerID: MCPeerID!, withDiscoveryInfo info: [NSObject : AnyObject]!) {    
    println("\(self.peerId) Found peer \(peerID)");
    if let browserSession = self.session {
        browserSession.delegate = self;
        browser.invitePeer(peerID, toSession: browserSession, withContext: nil, timeout: 30);
    }else {
        println("Browser session is nil");
   }    
}

Advertiser finds it using code:

func advertiser(advertiser: MCNearbyServiceAdvertiser!, didReceiveInvitationFromPeer peerID: MCPeerID!, withContext context: NSData!, invitationHandler: ((Bool, MCSession!) -> Void)!) {
    println("\(self.peerId) Received Invitation from \(peerID)");
    var session = availableSession();
    invitationHandler(true, session);
}

The problem that I am facing is: when the advertiser finds peerId, it shows a different peerId than the original peerId but has the correct display name.

Browser console output is:

  I am peer: <MCPeerID: 0x1663e670 DisplayName = Jon Yang’s iPad>
  <MCPeerID: 0x1663e670 DisplayName = Jon Yang’s iPad> Received Invitation from <MCPeerID: 0x1666daf0 DisplayName = Jon Yang’s iPadMini>
  <MCPeerID: 0x1663e670 DisplayName = Jon Yang’s iPad> Connecting with <MCPeerID: 0x1666daf0 DisplayName = Jon Yang’s iPadMini>
  <MCPeerID: 0x1663e670 DisplayName = Jon Yang’s iPad> Connected with <MCPeerID: 0x1666daf0 DisplayName = Jon Yang’s iPadMini>

and Advertiser Console output is :

 I am peer: <MCPeerID: 0x1569d230 DisplayName = Jon Yang’s iPadMini>
 <MCPeerID: 0x1569d230 DisplayName = Jon Yang’s iPadMini> Found peer <MCPeerID: 0x15586600 DisplayName = Jon Yang’s iPad>
 <MCPeerID: 0x1569d230 DisplayName = Jon Yang’s iPadMini> Connecting with <MCPeerID: 0x15586600 DisplayName = Jon Yang’s iPad>
 <MCPeerID: 0x1569d230 DisplayName = Jon Yang’s iPadMini> Connected with <MCPeerID: 0x15586600 DisplayName = Jon Yang’s iPad>

As you can see even though Browser and Advertiser has their peerId information, when they are received by other peer, their peerId is different. Can anyone explain that? Is there anything that I am doing wrong?

 <MCPeerID: 0x1663e670 DisplayName = Jon Yang’s iPad>

The 0x1663e670 represents the memory address of the MCPeerID object on the current device. Don't confuse that with the actual MCPeerID object that the framework is referencing. As long as the displayName is as expected then it is the correct peer.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM