Vijay Hirpara
Vijay Hirpara

Reputation: 555

Duplicate peer name appear in GKPeerPicker Controller

I am getting duplicate peername if I connect and disconnect bluetooth multiple time in my both ios device.

Is there any way to get single name for unique peer in gkpeerpickercontroller for bluetooth chat application.

Also attached the screenshot for it. Multiple duplicate peer appears

I am using below code to show GKPeerPickerController.


-(IBAction)btnConnectClicked:(id)sender
 {
    [self openPeerPickerController];
 }

-(IBAction)btnDisconnectClicked:(id)sender
{
    [currentSession disconnectFromAllPeers];
}
-(void)openPeerPickerController
{
    if (!currentSession)
    {
        GKPeerPickerController *peerPicker2 = [[GKPeerPickerController alloc] init];
        peerPicker2.delegate = self;
        peerPicker2.connectionTypesMask = GKPeerPickerConnectionTypeNearby;
        [peerPicker2 show];
    }
}

-(void)peerPickerController:(GKPeerPickerController *)picker didConnectPeer:(NSString *)peerID toSession:(GKSession *) session
{
    NSLog(@"Peer session connected");
    //set session delegate and dismiss the picker
    session.delegate = self;
    currentSession = session;
picker.delegate = nil;
    [picker dismiss];
}
- (GKSession *)peerPickerController:(GKPeerPickerController *)picker sessionForConnectionType:(GKPeerPickerConnectionType)type
{
    //create ID for session
    NSString *sessionIDString = @"MTBluetoothSessionID";
    //create GKSession object
    GKSession *session = [[GKSession alloc] initWithSessionID:sessionIDString displayName:nil sessionMode:GKSessionModePeer];
    return session;
}

-(void)peerPickerControllerDidCancel:(GKPeerPickerController *)picker
{
    NSLog(@"Peer cancelled");
    [currentSession disconnectFromAllPeers];
    currentSession=nil;
    picker.delegate = nil;
}
-(void)session:(GKSession *)session peer:(NSString *)peerID didChangeState:(GKPeerConnectionState)state
{
    switch (state)
    {
        case GKPeerStateAvailable:
        {
           // not connected to session, but available for connectToPeer:withTimeout:
        }
        break;

        case GKPeerStateUnavailable:
        {
            // no longer available
        }
            break;

        case GKPeerStateConnected:
        {
            // connected to the session
            [currentSession setDataReceiveHandler:self withContext:nil]; //set ViewController to receive data
        }
            break;

        case GKPeerStateDisconnected:
        {
            // disconnected from the session
            currentSession.delegate = nil;
            currentSession = nil; //allow session to reconnect if it gets disconnected
        }
            break;

        case GKPeerStateConnecting:
        {
            // waiting for accept, or deny response
        }
            break;

        default:
            break;
    }
}

Upvotes: 2

Views: 184

Answers (1)

Swapnil Luktuke
Swapnil Luktuke

Reputation: 10475

You GKPeerPickerControllerDelegate method

- (GKSession *)peerPickerController:(GKPeerPickerController *)picker sessionForConnectionType:(GKPeerPickerConnectionType)type

Returns a new session every time. In your case its being called twice and hence two sessions are created. From the documentation:

When the peer picker needs a session, it calls this method. Your application can either create a new session or return a previously created session to the peer picker.

So you can declare the session as a property and write a getter and just return the session property in the delegate which will avoid creation of multiple sessions

@property (nonatomic, string) GKSession *session;
#define sessionIDString @"MTBluetoothSessionID"

- (GKSession) session {
    if(!_session) {
        //create GKSession object
        _session = [[GKSession alloc] initWithSessionID:sessionIDString displayName:nil sessionMode:GKSessionModePeer];
    }
    return _session;
}

And change the delegate method to :

- (GKSession *)peerPickerController:(GKPeerPickerController *)picker sessionForConnectionType:(GKPeerPickerConnectionType)type {
    return self.session;
}

Make sure to nullify the session when you're done.

Upvotes: 2

Related Questions