3

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?

LettersBa
  • 747
  • 1
  • 8
  • 27

1 Answers1

3

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.

ChrisH
  • 4,468
  • 2
  • 33
  • 42