Multipeer Connectivity Disconnect

﹥>﹥吖頭↗ 提交于 2019-12-04 11:01:00

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/19101838/multipeer-connectivity-disconnect

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!