Why does my MCSession peer disconnect randomly?

前端 未结 5 1199
我寻月下人不归
我寻月下人不归 2020-11-29 18:36

Im using MCNearbyServiceBrowser and MCNearbyServiceAdvertiser to join two peers to a MCSession. I am able to send data between them using MCSession\'s sendData method. All

相关标签:
5条回答
  • 2020-11-29 19:06

    UPDATE After using a support ticket to Apple, they confirmed that calling sendData too often and with too much data can cause disconnects.

    I have had disconnects when hitting break points and when backgrounding. Since the break points won't happen on the app store, you need to handle the backgrounding case by beginning a background task when your app is about to enter the background. Then end this task when your app comes back to the foreground. On iOS 7 this gives you about 3 background minutes which is better than nothing.

    An additional strategy would be to schedule a local notification for maybe 15 seconds before your background time expires by using [[UIApplication sharedApplication] backgroundTimeRemaining], that way you can bring the user back into the app before it suspends and the multi peer framework has to be shutdown. Perhaps the local notification would warn them that their session will expire in 10 seconds or something...

    If the background task expires and the app is still in the background, you have to tear down everything related to multi-peer connectivity, otherwise you will get crashes.

    - (void) createExpireNotification
    {
        [self killExpireNotification];
    
        if (self.connectedPeerCount != 0) // if peers connected, setup kill switch
        {
            NSTimeInterval gracePeriod = 20.0f;
    
            // create notification that will get the user back into the app when the background process time is about to expire
            NSTimeInterval msgTime = UIApplication.sharedApplication.backgroundTimeRemaining - gracePeriod;
            UILocalNotification* n = [[UILocalNotification alloc] init];
            self.expireNotification = n;
            self.expireNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:msgTime];
            self.expireNotification.alertBody = TR(@"Text_MultiPeerIsAboutToExpire");
            self.expireNotification.soundName = UILocalNotificationDefaultSoundName;
            self.expireNotification.applicationIconBadgeNumber = 1;
    
            [UIApplication.sharedApplication scheduleLocalNotification:self.expireNotification];
        }
    }
    
    - (void) killExpireNotification
    {
        if (self.expireNotification != nil)
        {
            [UIApplication.sharedApplication cancelLocalNotification:self.expireNotification];
            self.expireNotification = nil;
        }
    }
    
    - (void) applicationWillEnterBackground
    {
        self.taskId = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^
        {
            [self shutdownMultiPeerStuff];
            [[UIApplication sharedApplication] endBackgroundTask:self.taskId];
            self.taskId = UIBackgroundTaskInvalid;
        }];
        [self createExpireNotification];
    }
    
    - (void) applicationWillEnterForeground
    {
        [self killExpireNotification];
        if (self.taskId != UIBackgroundTaskInvalid)
        {
            [[UIApplication sharedApplication] endBackgroundTask:self.taskId];
            self.taskId = UIBackgroundTaskInvalid;
        }
    }
    
    - (void) applicationWillTerminate
    {
        [self killExpireNotification];
        [self stop]; // shutdown multi-peer
    }
    

    You'll also want this handler in your MCSession delegate due to Apple bug:

    - (void) session:(MCSession*)session didReceiveCertificate:(NSArray*)certificate fromPeer:(MCPeerID*)peerID certificateHandler:(void (^)(BOOL accept))certificateHandler
     {
         if (certificateHandler != nil) { certificateHandler(YES); }
     }
    
    0 讨论(0)
  • 2020-11-29 19:20

    There are many causes of this, and the two answers thus far are both correct in my experience. Another which you'll find in other similar questions is this: Only one peer can accept another's invitation.

    So, to clarify, if you set up an app where all devices are both advertisers and browsers, any devices can freely invite any others found to join a session. However, between any two given devices, only one device can actually accept the invitation and connect to the other device. If both devices accept each others' invitations they will disconnect within a minute or less.

    Note that this limitation does not prevent the desired behavior because - unlike what my intuition stated before I built my multipeer implementation - when one device accepts an invitation and connects to another device they both become connected and receive connection delegate methods and can send each other messages.

    Therefore, if you are connecting devices which both browse and advertise, send invitations freely but only accept one of a pair.

    The problem of only accepting one of two invitations can be solved a myriad of ways. To begin, understand that you can pass any arbitrary object or dictionary (archived as data) as the context argument in an invitation. Therefore, both devices have access to any arbitrary information about the other (and of course itself). So, you could use at least these strategies:

    • simply compare: the display name of the peerID. But there's no guarantee these won't be equal.
    • store the date your multipeer controller was initialized and use that for comparison
    • give each peer a UUID and send this for comparison (my technique, in which each device - indeed each user of the app on a device - has a persistent UUID it employs).
    • etc - any object which supports both NSCoding and compare: will do fine.
    0 讨论(0)
  • 2020-11-29 19:24

    This is a bug, which I just reported to Apple. The docs claim the didReceiveCertificate callback is optional, but it's not. Add this method to your MCSessionDelegate:

    - (void) session:(MCSession *)session didReceiveCertificate:(NSArray *)certificate fromPeer:(MCPeerID *)peerID certificateHandler:(void (^)(BOOL accept))certificateHandler
     {
         certificateHandler(YES);
     }
    

    The random disconnects should cease.

    0 讨论(0)
  • 2020-11-29 19:25

    I was disconnecting immediately after I accepted the connection request. Observing the state, I saw it change from MCSessionStateConnected to MCSessionStateNotConnected.

    I am creating my sessions with:

    [[MCSession alloc] initWithPeer:peerID]
    

    NOT the instantiation method dealing with security certificates:

     - (instancetype)initWithPeer:(MCPeerID *)myPeerID securityIdentity:(NSArray *)identity encryptionPreference:(MCEncryptionPreference)encryptionPreference 
    

    Based on Andrew's tip above, I added the delegate method

       - (void) session:(MCSession *)session didReceiveCertificate:(NSArray *)certificate fromPeer:(MCPeerID *)peerID certificateHandler:(void (^)(BOOL accept))certificateHandler {
             certificateHandler(YES);
         }
    

    and the disconnects stopped.

    0 讨论(0)
  • 2020-11-29 19:27

    I've been having similar problems. It seems though that if I have run my app on one iOS device, and connected to another, then quit and relaunch (say when I rerun from Xcode), then I am in a situation where I get a Connected message and then a Not Connected message a little later. This was throwing me off. But looking more carefully, I can see that the Not Connected message is actually meant for a different peerId than the one that has connected.

    I think the problem here is that most samples I've seen just care about the displayName of the peerID, and neglect the fact that you can get multiple peerIDs for the same device/displayName.

    I am now checking the displayName first and then verifying that the peerID is the same, by doing a compare of the pointers.

    - (void)session:(MCSession *)session peer:(MCPeerID *)peerID didChangeState:(MCSessionState)state {
    
        MyPlayer *player = _players[peerID.displayName];
    
        if ((state == MCSessionStateNotConnected) &&
            (peerID != player.peerID)) {
            NSLog(@"remnant connection drop");
            return; // note that I don't care if player is nil, since I don't want to
                    // add a dictionary object for a Not Connecting peer.
        }
        if (player == nil) {
            player = [MyPlayer init];
            player.peerID = peerID;
            _players[peerID.displayName] = player;
        }
        player.state = state;
    
    ...
    
    0 讨论(0)
提交回复
热议问题