I am having an issue with getting a GKSession to work. Below is my code that is executed when a specific button is pressed.
GKSession *session;
if (connectButtonHasBeenPressed == false) {
NSLog(@"connectToBluetoothDevice has been called");
connectButtonHasBeenPressed = true;
GKSession *session = [[GKSession alloc] initWithSessionID:@"Unicorn" displayName:nil sessionMode:GKSessionModePeer];
[session setDataReceiveHandler:self withContext:nil];
[session setDelegate:self];
[session setAvailable:YES];
NSLog(@"Session ID: %@", [session sessionID]);
NSLog(@"Currently Available Peers: %i", [[session peersWithConnectionState:GKPeerStateAvailable] count]);
if ([session isAvailable]) {
NSLog(@"The Session Is Available");
}
[connectToDeviceButton setTitle:@"Searching..." forState:UIControlStateNormal];
}
else {
NSLog(@"Currently Available Peers: %i", [[session peersWithConnectionState:GKPeerStateAvailable] count]);
}
After the button is pressed the first time, everything appears to be working okay. And every time I press the button after that, it prints "Currently Available Peers: 0". This would be the expected output if I didn't have two devices sitting next to each other, running the program both with the button pushed. I also have all of the GKSessionDelegate methods implemented into this class, which all log a message to the console. None of those methods are ever run. All of this would indicate to me that the devices could not find each other.
However, I have run the sample program GKRocket which uses GKSession to connect two devices and it works fine between these same two devices. I have compared the code of GKRocket to my program's code, and I have not found any differences that I think could effect GKSession.
Any Suggestions?