简体   繁体   中英

iOS Multipeer Connectivity both Advertiser and Browser

My setup is this: I have somewhere between 0 and 8 devices running at a time, and devices can be added or removed at any time.

I want to use the iOS 7 Multipeer framework to connect them. I have this working in a controlled environment, I can start 1-7 devices in advertiser mode, then start one in browser mode and they all link up.

What I'm unsure of is how I should know if the device needs to be in advertiser or browser mode when I start it? I've tried defaulting to advertiser mode for X seconds then switching to browser, the problem with this is that it's possible that all the devices started at the same time and turn off advertiser mode at the same time.

I've also considered running devices in both advertiser and browser mode, but the initial problem is that the device discovers itself. Also I believe I have 1 less device to connect this way.

I'm sure there's a recommended way to set this up but I've been unable to find anything that doesn't assume there's a set browser and advertiser, anyone have suggestions for this?

It is easy to make all devices both advertiser and browsers, and it's a normal behavior (I'm pretty sure there's a mention of this in the documentation, I'll search it and add the link later).

I haven't had problems with the device discovering itself, maybe you're creating two peerIDs for the same device?

You may like to check PLPartyTime's implementation of this. It just has a few simple checks to see if it needs to connect/accept a connection.

Advertiser Delegate

- (void)advertiser:(MCNearbyServiceAdvertiser *)advertiser
didReceiveInvitationFromPeer:(MCPeerID *)peerID
       withContext:(NSData *)context
 invitationHandler:(void(^)(BOOL accept, MCSession *session))invitationHandler
{
  // Only accept invitations with IDs lower than the current host
  // If both people accept invitations, then connections are lost
  // However, this should always be the case since we only send invites in one direction
  if ([peerID.displayName compare:self.peerID.displayName] == NSOrderedDescending)
  {
    invitationHandler(YES, self.session);
  }
}

Browser Delegate

- (void)browser:(MCNearbyServiceBrowser *)browser foundPeer:(MCPeerID *)peerID withDiscoveryInfo:(NSDictionary *)info
{
  // Whenever we find a peer, let's just send them an invitation
  // But only send invites one way
  // TODO: What if display names are the same?
  // TODO: Make timeout configurable
  if ([peerID.displayName compare:self.peerID.displayName] == NSOrderedAscending)
  {
    NSLog(@"Sending invite: Self: %@", self.peerID.displayName);
    [browser invitePeer:peerID
              toSession:self.session
            withContext:nil
                timeout:10];
  }
}

You may also want to check out my fork , which is a little bit smaller and both browser and advertiser are separate objects.

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