LettersBa
LettersBa

Reputation: 767

Set the maximum peers in MCSession

I'm sending files between devices using the framework MultipeerConnectivity. I learned that by default this framework can connect with up to 8 devices, so in my case I would like to reduce this to only 2 devices (which in this case is mine and the other person)

In the documentation he says that we can use the constant:

kMCSessionMaximumNumberOfPeers and kMCSessionMinimumNumberOfPeers

Below is my code that configure the MCSession:

mySession = [[MCSession alloc] initWithPeer:self.myPeerID];

mySession.delegate = self;

Now, how can I use the constant up in my code to define the maximumPeers and the minimumPeers?

Upvotes: 3

Views: 1001

Answers (1)

ChrisH
ChrisH

Reputation: 4558

Those constants are informational. If you want to limit the number of connected peers, you will need to check the count of your MCSession instance's connectedPeers property whenever peers connect/disconnect. You can then turn browsing/advertising on/off as needed.

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

    NSInteger sessionCount = [session.connectedPeers count];

    if (sessionCount==2){

        //Stop browsing and/or advertising
    }
    else {

        //Based on your own rules/requirements, if the session count
        //has dropped below 2 you can start browsing and/or advertising
    }   
}

You may also need to add checks in your MCNearbyServiceBrowser and MCNearbyServiceAdvertiser delegate methods to ensure you are not inviting/accepting peers concurrently as the MCPeerID connection states change.

Upvotes: 3

Related Questions