views:

119

answers:

1

I have been trying to implement the GameKit Framework for bluetooth connection and want to use a Server/Client relationship to reduce lag and be able to distinguish between to connected devices. I found this thread and it is similar to what I am trying to do, but the code doesn't work for me. Here is what I have:

Connect Method:

-(IBAction) btnConnect:(id) sender {
if(sender == server){
    [self.currentSession initWithSessionID:@"BT" displayName:nil sessionMode:GKSessionModeServer];
    currentSession.available == YES;
    NSLog(@"Setup Server");
}else{
    [self.currentSession initWithSessionID:@"BT" displayName:nil sessionMode:GKSessionModeClient];
    currentSession.available == YES;
    NSLog(@"Setup Client");
}
currentSession.delegate = self;
currentSession.disconnectTimeout = 0;
[currentSession setDataReceiveHandler:self withContext:nil];
[client setHidden:YES];
[server setHidden:YES];
[disconnect setHidden:NO];       
}

didChangeState:

- (void)session:(GKSession *)session peer:(NSString *)peerID didChangeState:(GKPeerConnectionState)state {
NSLog(@"didChangeState was called with status: %@.", state);
switch (state)
{
    case GKPeerStateConnected:
        NSLog(@"connected");
        break;
    case GKPeerStateDisconnected:
        NSLog(@"disconnected");
        [self.currentSession release];
        currentSession = nil;
        [connect setHidden:NO];
        [disconnect setHidden:YES];
        break;
    case GKPeerStateAvailable:
    NSLog(@"Server is Available, Presenting UIALert...");
    NSLog(@"%@", peerID);
    peerName = [session displayNameForPeer:peerID];
    NSLog(@"%@", peerName);
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Server Available!" message:[NSString stringWithFormat:@"The Server %@ is Available, Would you like to Connect?", peerName] delegate:self cancelButtonTitle:@"Decline" otherButtonTitles:@"Accept", nil];
    [alert show];
    [alert release];
    if(selection == @"accept"){
        [session connectToPeer:peerID withTimeout:15];
        session.available = NO;
    }else{
    }
            break;
      }
 }

didReceiveConnectionRequest:

- (void)session:(GKSession *)session didReceiveConnectionRequestFromPeer:(NSString *)peerID{
NSLog(@"Recieved Connection Request");
NSLog(@"%@", peerID);
peerName = [session displayNameForPeer:peerID];
NSLog(@"%@", peerName);
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Connection Request" message:[NSString stringWithFormat:@"The Client %@ is trying to connect.", peerName] delegate:self cancelButtonTitle:@"Decline" otherButtonTitles:@"Accept", nil];
[alert show];
[alert release];
if(selection == @"accept"){
    [session acceptConnectionFromPeer:peerID error:nil];
}else{
    [session denyConnectionFromPeer:peerID];
}
}

I think I have this all setup right, but the didChangeState isn't getting called to inform the user that another device is available. Am I missing something or should I try to use a different method. Thanks for any help

A: 
currentSession.disconnectTimeout = 0;

The disconnect timeout is a time in seconds that peers should wait before disconnecting unresponsive peers. You don't want this to be 0. The default is 20 seconds, you should leave it there or say like 10 seconds. I actually don't set this in my GameKit code and it works well.

Also, it might help to post your entire implementation class somewhere. We'll need to make sure you are implementing GKSessionDelegate, e.g.:

@interface SomeObject : NSObject <GKSessionDelegate>

Also, you're setting up a Peer-2-Peer above. You said you were trying to do client/server. If so you should start the client session with a mode of GKSessionModeClient and server as GKSessionModePeer.

Lastly...are you testing this on actual devices or with a device and simulator? Don't forget that simulator and first gen iPhones and touches do not support bluetooth. So you'll have to have everyone involved connected to the same wireless network for anything to happen.

What are you seeing in the console when you start up your debug session?

Typeoneerror
Thank you for the reply,
Monoxgas
I changed the disconnectimeout with no luck, I still have no response from the application after I click either the server or client button and Nothing happens in the console after "Setup Server". I have implemented the GKSessionDelegate and I am using an iPhone 4 and an iPad. I am really stumped on this and don't understand why it isn't working
Monoxgas
I SOLVED IT...I had to say:
Monoxgas
self.currentSession = [[GKSession alloc] initWithSessionID:@"BT" displayName:nil sessionMode:GKSessionModePeer]; instead of just [self.currentSession initWithSessionID:@"BT" displayName:nil sessionMode:GKSessionModePeer];
Monoxgas
oh, yeah, that would do it. Totally read right past that!
Typeoneerror