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?
You seem to have two instances of GKSession. One outside and the other inside the
if
statement.This means that if
connectButtonHasBeenPressed
isfalse
it will create it's own version of GKSession that it keeps. but if it'strue
thensession
will equalnil
.Also I would recommend using
nil
as the session ID as it then gets set for you using the bundle ID. Though this may be personal preference.Try using something like this:
You don't really need to have a
connectButtonHasBeenPressed
variable as you can just check to see if the GKSession is equal to nil, which it should always be if there is no connection. When your session ends you should always cancel all session actions and setsession = nil;
.The
session
variable should really be declared in your .h file so that you can use it throughout the class. So thatGKSession *session;
is no longer needed.Note: Just a note from your
connectToBluetoothDevice has been called
log. From my experience, GKSession will use WiFi or Bluetooth, depending on whatever is available. So much so that you can have 3 devices, 1 with only Bluetooth on, 1 with only WiFi on and the last with both on and they will all connect and talk with each other absolutely fine.Hope this helps.
Edit: Removed unneeded
connectButtonHasBeenPressed
variable from example code and added more explanation.