0

How do you simply connect with a peer (the identity is not important, just the data), without going through the MCBrowserView? Like an automatic connection. I have read that this may be difficult because if both peers are browsing and advertising at the same time there can be issues when connecting (someone needs to have priority).

If the above is possible, how would one go about adding a simple pop up saying something like - 'someone would like to connect: accept/decline'.

Basically the MCBrowserView isn't really important if the identity of the user you're connecting with isn't a factor. Anyone who has had experience on this?

tashuhka
  • 5,028
  • 4
  • 45
  • 64
Peeter Vedic
  • 101
  • 1
  • 3
  • 8

1 Answers1

0

You can connect with a peer without showing a browser by using the MCNearbyServiceBrowser, as it has no associated UI.

One peer starts browsing like this:

self.thisPeer = [[MCPeerID alloc] initWithDisplayName:@"Peer Name"];
self.session = [[MCSession alloc] initWithPeer:self.thisPeer ];
self.session.delegate = self;

self.serviceBrowser = [[MCNearbyServiceBrowser alloc] initWithPeer:self.thisPeer serviceType:<lowercase 1-15 chars>
self.serviceBrowser.delegate = self;
[self.serviceBrowser startBrowsingForPeers];

The other peer starts advertising like this:

MCPeerID *peerID = [[MCPeerID alloc] initWithDisplayName:@"some name"];
self.session = [[MCSession alloc] initWithPeer:peerID];
self.session.delegate = self;    
self.advertiser = [[MCNearbyServiceAdvertiser alloc] initWithPeer:peerID discoveryInfo:nil serviceType:<lowercase 1-15 chars>];
self.advertiser.delegate = self;
[self.advertiser startAdvertisingPeer];

Then, when the browsing peer hears the nearby advertising peer, it sends an invite to join a session:

- (void)browser:(MCNearbyServiceBrowser *)browser foundPeer:(MCPeerID *)peerID withDiscoveryInfo:(NSDictionary *)info {
    NSLog(@"Found a nearby advertising peer %@", peerID);
   [self.serviceBrowser invitePeer:peerID toSession:self.session withContext:nil timeout:60];        
}

On receipt of the invite, you can show an accept/decline alert if you want to or you can simply accept the invitation.

Keith Coughtrey
  • 1,386
  • 13
  • 17
  • That answers it. But is there a way to both advertise and browse at the same time? – Peeter Vedic Aug 07 '14 at 23:39
  • It is certainly possible to browse and advertise at the same time as long as you have a strategy to avoid two peers both accepting an invite from the other. See http://stackoverflow.com/a/19529933/2077204 – Keith Coughtrey Aug 12 '14 at 23:54