Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MultipeerConnectivity Session management

I am really stuck at the moment trying to get the grasp of invites in the MultipeerConnectivityFramework.

Right now I have an ipad App acting as the Advertiser and an iphone App acting as Browser. I have implemented a sharedService for the MultipeerFramework and did the following:

Advertiser

@implementation MultipeerConnectivityService {
    MCNearbyServiceAdvertiser *_advertiser;
    MCSession *_session;
    MCNearbyServiceBrowser *_browser;        
}

- (void)automaticAdvertiseWithName:(NSString *)name {
    MCPeerID *peerID = [[MCPeerID alloc] initWithDisplayName:name];
    _session = [[MCSession alloc] initWithPeer:peerID];
    _session.delegate = self;

    _advertiser = [[MCNearbyServiceAdvertiser alloc] initWithPeer:peerID discoveryInfo:nil serviceType:kServiceType];
    _advertiser.delegate = self;
    [_advertiser startAdvertisingPeer];
}

- (void)advertiser:(MCNearbyServiceAdvertiser *)advertiser didReceiveInvitationFromPeer:(MCPeerID *)peerID withContext:(NSData *)context invitationHandler:(void (^)(BOOL, MCSession *))invitationHandler {
    invitationHandler([@YES boolValue], _session);
    NSLog(@"Invitation accepted");
}

Browser

- (void)automaticBrowsingWithName:(NSString *)name {
    MCPeerID *peerID = [[MCPeerID alloc] initWithDisplayName:name];
    _browser = [[MCNearbyServiceBrowser alloc] initWithPeer:peerID serviceType:kServiceType];
    _browser.delegate = self;
    [_browser startBrowsingForPeers];
}

- (void)browser:(MCNearbyServiceBrowser *)browser didNotStartBrowsingForPeers:(NSError *)error {
    if ([_delegate respondsToSelector:@selector(browser:didNotStartBrowsingForPeers:)]) {
        [_delegate browserDidNotStartBrowsingForPeers];
    }
}
- (void)browser:(MCNearbyServiceBrowser *)browser foundPeer:(MCPeerID *)peerID withDiscoveryInfo:(NSDictionary *)info {
    [browser invitePeer:peerID toSession:[self getMCSession] withContext:nil timeout:10];
    if ([_delegate respondsToSelector:@selector(browser:foundPeer:)]) {
        [_delegate browser:browser foundPeer:peerID];
    }
}
- (void)browser:(MCNearbyServiceBrowser *)browser lostPeer:(MCPeerID *)peerID {
    if ([_delegate respondsToSelector:@selector(browserLostPeer:)]) {
        [_delegate browserLostPeer:peerID];
    }
}

- (MCSession *) getMCSession {
    return _session;
}

But then I am getting as feedback in the console:

-[MCNearbyServiceBrowser invitePeer:toSession:withContext:timeout:] Bad argument session=nil

When I check for the found Advertisers, everything is OK. My advertiser ipad is being found. But how can I manage the invite?

So I don't get it right now... When I send an invitation by the browser, what session do I have to use? On the iPad I set up the session like you can see in the "automaticAdvertiseWithName" method. but on the iphone I don't do this when calling "automaticBrowsingWithName"... Is that the problem? And don't they have to be the same session to successfully connect them? And how can I successfully invite my advertiser ipad to the browser?

I am confused by the notion of creating a new session when one has already been created by the advertiser.

I am actually not sure, if the delegate didReceiveInvitation is adding the peer to the connectedPeers at all.

- (void)automaticAdvertiseWithName:(NSString *)name {
    MCPeerID *peerID = [[MCPeerID alloc] initWithDisplayName:name];
    self.session = [[MCSession alloc] initWithPeer:peerID];
    self.session.delegate = self;
    _advertiser = [[MCNearbyServiceAdvertiser alloc] initWithPeer:peerID discoveryInfo:nil serviceType:kServiceType];
    _advertiser.delegate = self;
    [_advertiser startAdvertisingPeer];
}
- (void)advertiser:(MCNearbyServiceAdvertiser *)advertiser didReceiveInvitationFromPeer:(MCPeerID *)peerID withContext:(NSData *)context invitationHandler:(void (^)(BOOL, MCSession *))invitationHandler {
    BOOL accept = YES;
    invitationHandler(accept, self.session);
    NSLog(@"Invitation accepted: %@", self.session);
}

And when I call the property "connectedPeers" on my session, there are no connected peers at all, even though the delegate found one. Did I make a mistake there?

like image 467
sesc360 Avatar asked Oct 20 '22 21:10

sesc360


1 Answers

Your problem is that your session is null at the time you call invitePeer:toSession:withContext:timeout...Anyway you have two options to fix this.

You have two options :
Option 1 - move the peerID creation, session creation and session delegate assignment in a place where its executed at all time. For example in the init code for MultipeerConnectivityService class of if its a UIViewController in the viewDidLoad.

Option 2 - add the following snippet before you call "invitePeer:toSession:withContext:timeout:"

if (!_session) {
      MCPeerID *peerID = [[MCPeerID alloc] initWithDisplayName:@"Browser"]; // you can customize the name here
     _session = [[MCSession alloc] initWithPeer:peerID];
     _session.delegate = self;
}

Hope this helps ...good luck!

like image 157
Beuz Avatar answered Oct 23 '22 23:10

Beuz