Question

I figured that multipeer connectivity sometimes has issues with reconnecting. Whenever I try to reconnect to the client that I just disconnected with, it fails to send a new invitation to the client. I am not sure what is the correct way to disconnect with connected peers, and how to disconnect with all connected peers if the app is getting killed due to in suspended mode. Can anyone help me with this one please?

For example:

If I have A and B connected, and B walked out of range, the session is automatically disconnected due to out of reachability and

- (void)session:(MCSession *)session peer:(MCPeerID *)peerID didChangeState:(MCSessionState)state {
    case MCSessionStateNotConnected:
}

is invoked, I believe at this point both parties know they have disconnected from each other, however, when B walks back and wants to reconnect -- the invitation is sent but the other one never received it. What exactly went on? Do I have to manually disconnect the session to prevent this?

Was it helpful?

Solution

I reset the PeerID and Session on the Browser side whenever I get disconnected and want to reconnect:

    // reset my PeerID.  Sometimes when trying to reconnect to the same Advertiser with the same PeerID nothing happens
    mcPeerID = [[MCPeerID alloc] initWithDisplayName:[UIDevice currentDevice].name];
    mcSession = [[MCSession alloc] initWithPeer:mcPeerID];
    mcSession.delegate = self;

    mcNearbyServiceBrowser = [[MCNearbyServiceBrowser alloc] initWithPeer:mcPeerID serviceType:kMCServiceType];
    mcNearbyServiceBrowser.delegate = self;
    [mcNearbyServiceBrowser startBrowsingForPeers];

On the Advertiser side, the connectedPeers count is decremented correctly on the disconnect, so reconnecting as a * different * browser doesn't seem to have a negative impact (maybe this isn't proper coding, but it works). With a new PeerID I can reconnect without issue. I reset the Session at the same time to keep them in sync (ie, I don't want a Session laying about that was init'd from the old PeerID).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top