11

I'm having trouble with staying connected using the Multipeer Connectivity Framework in iOs7. Currently my app is programmatically handling the browsing and advertising using MCNearbyServiceAdvertiser and MCNearbyServiceBrowser. I have an alert view that asks the user if he is a browser or an advertiser. On the return from that view I instantiate either an MCNearbyServiceAdvertiser or Browser accordingly.

#pragma - Alert Delegate
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 0)
    {
        _browser = [[MCNearbyServiceBrowser alloc]initWithPeer:_peerID serviceType:@"Context-xl"];
        [_browser setDelegate:self];
        [self.detailViewController setRemote:YES];
        [_browser startBrowsingForPeers];
    } else
    {
        _advertiser = [[MCNearbyServiceAdvertiser alloc]initWithPeer:_peerID discoveryInfo:nil serviceType:@"Context-xl"];
        [_advertiser setDelegate:self];
        [self.detailViewController setRemote:NO];
        [_advertiser startAdvertisingPeer];
    }
    [self.detailViewController configureView];
}

My session delegate method peer:...DidChangeState... is getting called twice, once for the connect and again for the disconnect. I'm not stopping the advertiser or browser at all after the session is started. Should I stop browsing/advertising?

Corey Zambonie
  • 614
  • 5
  • 17
  • I've continued working on it. I thought that maybe setting breakpoints was causing the devices to disconnect, which actually seems to be the case. However, the devices aren't staying connected when running normally. – Corey Zambonie Oct 01 '13 at 23:03
  • 1
    I'm having the same problem. The devices connect for a bit then disconnect and won't stay connected or reconnect. – James Andrews Oct 18 '13 at 22:41
  • It happened a lot when I was doing a file transfer using MCSession's sendData:ToPeer method. I've since switched to opening a stream using the startStream:withName method and the devices have been staying connected. – Corey Zambonie Oct 21 '13 at 02:02
  • possible duplicate of [Why does my MCSession peer disconnect randomly?](http://stackoverflow.com/questions/18935288/why-does-my-mcsession-peer-disconnect-randomly) – Diogenes Creosote Oct 31 '13 at 00:01
  • 1
    I am also getting random disconnects, even when not backgrounded or on breakpoints. Anyone found any solutions? – jjxtra Nov 30 '13 at 03:35

1 Answers1

7

EDIT Used a support ticket with Apple and they confirmed that calling sendData with too much data or too often can cause disconnects.

EDIT My hypothesis is that Apple has a thread or queue that is polling to check if peers are connected. If this thread / queue stalls (i.e. a breakpoint is hit or the app pegs the CPU or does something that takes a while on the main thread) it appears that this causes a disconnect.

Creating my session without encryption seems to have helped performance and with the disconnects, although they still happen.

MCPeerID* peerId = [[MCPeerID alloc] initWithDisplayName:self.displayName];
self.peer = [[MultiPeerPeer alloc] initWithDisplayName:peerId.displayName andPeer:peerId];
self.session = [[MCSession alloc] initWithPeer:peerId securityIdentity:nil encryptionPreference:MCEncryptionNone];

In addition, I have found calling sendData too often (more than 30-60 times a second) can cause the framework to get in a bad state and cause freezes and disconnects.

jjxtra
  • 20,415
  • 16
  • 100
  • 140
  • I actually talked to the Apple engineer's who worked on multi-peer connectivity, and they confirmed that calling sendData too often can result in problems. Maybe iOS 8 will fix this, we'll see... – jjxtra May 28 '14 at 16:18
  • Hey, goof to know. Do you have a link to the support ticket? – Omer Jan 04 '16 at 17:36
  • @Omer no sadly the email has been deleted. – jjxtra Jan 04 '16 at 18:13