即使两个设备都连接,多路连接数据也只发送一种方式

时间:2014-10-21 12:02:25

标签: ios objective-c multipeer-connectivity

我目前有两个设备通过多对等连接框架相互连接。当连接时,每个设备将立即相互发送一组nsdata,在第一种情况下,数据将包含声音和图像。这似乎工作正常,用户都收到相关数据。

用户现在可以从视图中相互选择并发送另一位数据。但是,我发现如果主机(发送邀请的客户端)尝试发送更多数据,则控制台中会显示以下错误。

 Peers (
        PeerName
    ) not connected

但是如果客户端(被邀请者)发送数据,它将按预期在主机上到达。特别有趣的是我在NSV中有这样的事实:

-(void)peerDidChangeStateWithNotification:(NSNotification *)notification

发生断开连接时触发的方法,没有断开连接且设备彼此连接。我的代码如下,是否有人知道为什么会这样?

#pragma mark Send Buzz Notification
#pragma mark -
-(void)sendBuzzNotification{

    // create the user (remove the image to lower size)
    LocalUserClass *withoutImage = [[LocalUserClass alloc]initWithName:[[[GlobalData sharedGlobalData]localUser]personName] personImage:nil personSound:[[[GlobalData sharedGlobalData]localUser]personSound] personPeerId:nil];

    // send the users sound and name to the other peer/s
    NSData *dataToSend = [NSKeyedArchiver archivedDataWithRootObject:withoutImage];
    NSError *error;

    // skip if no users are selcted
    if ([selectedUsers count] == 0) {
        return;
    }
    else{
        // create the array of peer Ids from selected users
        NSMutableArray *tempArray = [[NSMutableArray alloc]init];

        for (LocalUserClass *object in selectedUsers) {

            // add the peer id
            MCPeerID *theId = object.personPeerId;
            [tempArray addObject:theId];
        }
        NSArray *theUsers = [[NSArray alloc]init];
        theUsers = tempArray;

    // send the data
    [appDelegate.mcManager.session sendData:dataToSend
                                    toPeers:theUsers
                                   withMode:MCSessionSendDataReliable
                                      error:&error];

    if (error) {
        NSLog(@"%@", [error localizedDescription]);
    }

    // vibrate the device to show its been sent
    AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
    }
}
#pragma mark Peer Connected State Checker
#pragma mark -
-(void)peerDidChangeStateWithNotification:(NSNotification *)notification{

    MCPeerID *peerID = [[notification userInfo] objectForKey:@"peerID"];
    //NSLog(@"%@",peerID.displayName);
    MCSessionState state = [[[notification userInfo] objectForKey:@"state"] intValue];
    LocalUserClass *theUser = [[LocalUserClass alloc]initWithName:peerID.displayName personImage:nil personSound:nil personPeerId:peerID];
    //NSLog(@"the user name is: %@ the user sound is: %@ the user peer id is %@",theUser.personName,theUser.personSound,theUser.personPeerId);

    if (state != MCSessionStateConnecting) {

        if (state == MCSessionStateConnected) {

            // add the user
            [connectedUsers addObject:theUser];
            // send the users details to the newly connected peer (profile pic and sound)
            [self sendUserDetailsToPeer:peerID];

        }
        else if (state == MCSessionStateNotConnected){

            // do we have connections
            if ([connectedUsers count] > 0) {

                // get the user to remove
                NSInteger thePeerIndex = 0;
                NSInteger thePeerIndexSelectedUsers = 0;

                for (LocalUserClass *object in connectedUsers) {
                    if (object.personPeerId == theUser.personPeerId) {
                        thePeerIndex = [connectedUsers indexOfObject:object];
                        [connectedUsers removeObjectAtIndex:thePeerIndex];
                    }
                }
                for (LocalUserClass *user in selectedUsers) {
                    if (user.personPeerId == theUser.personPeerId) {
                    thePeerIndexSelectedUsers = [selectedUsers indexOfObject:user];
                    [selectedUsers removeObjectAtIndex:thePeerIndexSelectedUsers];
                    }
                }
            }
        }
    }

    // push to main queue for speedy response
    dispatch_async(dispatch_get_main_queue(), ^(void) {

        // create user playback devices
        [playbackManager createUserAudioPlayers:connectedUsers withSound:nil];

        [collView reloadData];
        [self populateUserBox];

        BOOL peersExist = ([[appDelegate.mcManager.session connectedPeers] count] == 0);
        //NSLog(@"PEER COUNT IS %lu",(unsigned long)[[appDelegate.mcManager.session connectedPeers] count]);
        [disconnectButton setEnabled:!peersExist];

        if ([disconnectButton isEnabled]) {
            [disconnectButton setBackgroundColor:[UIColor colorWithRed:(51/255.0) green:(202/255.0) blue:(168/255.0) alpha:1.0]];
        }
        else{
            [disconnectButton setBackgroundColor:[UIColor colorWithRed:(107/255.0) green:(107/255.0) blue:(107/255.0) alpha:1.0]];
        }

    });

}

#pragma mark Peer Received Data
#pragma mark -
-(void)didReceiveDataWithNotification:(NSNotification *)notification{

    NSData *receivedData = [[notification userInfo] objectForKey:@"data"];
    receivedUser = [NSKeyedUnarchiver unarchiveObjectWithData:receivedData];

    CGImageRef cgref = [receivedUser.personImage CGImage];
    CIImage *cim = [receivedUser.personImage CIImage];

     // are we getting a whole user or just the user sound and name (check for no image)
    if (cim == nil && cgref == NULL)
    {
        // recieve the data (TODO probably needs to be run in the background)
        [self performSelectorOnMainThread:@selector(recieveBuzzNotification) withObject:nil waitUntilDone:YES];
    }
    else{
        // recieve the data (TODO probably needs to be run in the background)
       [self performSelectorOnMainThread:@selector(receivedUserProfile) withObject:nil waitUntilDone:YES];
    }
}

更新:在初始数据传输后,主机似乎失去了与客户端的连接:

[self sendUserDetailsToPeer:peerID];

然而,断开状态不会触发任何内容,客户端仍然可以向主机发送数据。通过注销连接的会话可以看到这一点:

session.connectedPeers

编辑:视图控制器中的附加代码:

#pragma mark Create Session
#pragma mark -
-(void)setupSession{

    // create session and peer with details from the user
    [appDelegate.mcManager setupPeerAndSessionWithDisplayName:[[[GlobalData sharedGlobalData]localUser]personName]];
    [appDelegate.mcManager advertiseSelf:visibleSwitch.isOn];
    appDelegate.mcManager.browser.delegate = self;

    // set global user peerId
    [[[GlobalData sharedGlobalData]localUser]setPersonPeerId:appDelegate.mcManager.peerIDGlobal];

    // setup notification to track the user info of connected peers
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(peerDidChangeStateWithNotification:)
                                                 name:@"MCDidChangeStateNotification"
                                               object:nil];
    // notifcation to track recieved data
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(didReceiveDataWithNotification:)
                                                 name:@"MCDidReceiveDataNotification"
                                               object:nil];
}

和MCManager:

-(id)init{
    self = [super init];

    if (self) {
        peerIDGlobal = nil;
        session = nil;
        browser = nil;
        advertiser = nil;
    }

    return self;
}

//----CUSTOM---------------------------------------------------------------------------------------------------------------//

//-------------------------------------------------------------------------------------------------------------------------//
//----PUBLIC---------------------------------------------------------------------------------------------------------------//
#pragma mark Setup Peer and Session
#pragma mark -
-(void)setupPeerAndSessionWithDisplayName:(NSString *)displayName{

    peerIDGlobal = [[MCPeerID alloc] initWithDisplayName:[[[GlobalData sharedGlobalData]localUser]personName]];
    session = [[MCSession alloc] initWithPeer:peerIDGlobal];
    session.delegate = self;
}

#pragma mark Setup Browser
#pragma mark -
-(void)setupMCBrowser{
    browser = [[MCBrowserViewController alloc] initWithServiceType:@"chat-files" session:session];
}

#pragma mark Setup Advertiser
#pragma mark -
-(void)advertiseSelf:(BOOL)shouldAdvertise{

    if (shouldAdvertise) {
        advertiser = [[MCAdvertiserAssistant alloc] initWithServiceType:@"chat-files"
                                                           discoveryInfo:nil
                                                                 session:session];
        [advertiser start];
    }
    else{
        [advertiser stop];
        advertiser = nil;
    }
}


//----DELEGATES------------------------------------------------------------------------------------------------------------//

//-------------------------------------------------------------------------------------------------------------------------//
#pragma mark Session delegate
#pragma mark -
-(void)session:(MCSession *)session peer:(MCPeerID *)peerID didChangeState:(MCSessionState)state{

    // create the dict with connected user info
    NSDictionary *dict = @{@"peerID": peerID,
                           @"state" : [NSNumber numberWithInt:state]
                           };

    // post the notification for tracking
    [[NSNotificationCenter defaultCenter] postNotificationName:@"MCDidChangeStateNotification"
                                                        object:nil
                                                      userInfo:dict];
}


-(void)session:(MCSession *)session didReceiveData:(NSData *)data fromPeer:(MCPeerID *)peerID{

    NSDictionary *dict = @{@"data": data,
                           @"peerID": peerID
                           };

    [[NSNotificationCenter defaultCenter] postNotificationName:@"MCDidReceiveDataNotification"
                                                        object:nil
                                                      userInfo:dict];
}

3 个答案:

答案 0 :(得分:3)

谢谢Chris,这看起来是个问题,我根据这里的一些样板代码重写了代码: https://github.com/shrtlist/MCSessionP2P

现在该应用程序自动连接到没有浏览器的用户,这是一个很好的解决方案,我强烈建议有问题的人看看代码。

答案 1 :(得分:0)

当两台设备同时浏览/通告和发送/接受邀请时,Multipeer Connectivity存在已知问题。

Apple在WWDC 14期间推荐的解决方案是我在此发布的解决方案:Reconnecting to disconnected peers,它使用确定性方法来决定一对中的哪个同伴接受其他邀请。

MCBrowserViewControllerMCAdvertiserAssistant似乎只能在一个同行正在浏览其他广告的应用中可靠地使用。

答案 2 :(得分:-1)

当设备同时进行广告和浏览时,我遇到了类似的问题。我在Apple WWDC视频中使用了这种方法(我相信),以确保没有重复的连接;

func browser(browser: MCNearbyServiceBrowser!, foundPeer peerID: MCPeerID!, withDiscoveryInfo info: [NSObject : AnyObject]!) {
    if (self.myPeerID.hash > peerID.hash){
        println("foundPeer:\(peerID.description) -> invitePeer")
        mpBrowser.invitePeer(peerID, toSession: self.mpSession, withContext: nil, timeout: peerInviteTimeout)   
    }else{
        println("foundPeer:  \(peerID.description) hash is higher->defer invite")     
    }
}