Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You seem to have two instances of GKSession. One outside and the other inside the <code>if</code> statement.</p> <p>This means that if <code>connectButtonHasBeenPressed</code> is <code>false</code> it will create it's own version of GKSession that it keeps. but if it's <code>true</code> then <code>session</code> will equal <code>nil</code>.</p> <p>Also I would recommend using <code>nil</code> as the session ID as it then gets set for you using the bundle ID. Though this may be personal preference.</p> <p>Try using something like this:</p> <pre><code>if (session == nil) { NSLog(@"connectToBluetoothDevice has been called"); session = [[GKSession alloc] initWithSessionID:nil displayName:nil sessionMode:GKSessionModePeer]; [session setDataReceiveHandler:self withContext:nil]; [session setDelegate:self]; [session setAvailable:YES]; NSLog(@"Session ID: %@", [session sessionID]); if ([session isAvailable]) { NSLog(@"The Session Is Available"); } [connectToDeviceButton setTitle:@"Searching..." forState:UIControlStateNormal]; connectButtonHasBeenPressed = true; } NSLog(@"Currently Available Peers: %i", [[session peersWithConnectionState:GKPeerStateAvailable] count]); </code></pre> <p>You don't really need to have a <code>connectButtonHasBeenPressed</code> 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 set <code>session = nil;</code>.</p> <p>The <code>session</code> variable should really be declared in your .h file so that you can use it throughout the class. So that <code>GKSession *session;</code> is no longer needed.</p> <p><strong>Note:</strong> Just a note from your <code>connectToBluetoothDevice has been called</code> 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.</p> <p>Hope this helps.</p> <p><strong>Edit:</strong> Removed unneeded <code>connectButtonHasBeenPressed</code> variable from example code and added more explanation.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload