달력

10

« 2024/10 »

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
http://www.iphonedevsdk.com/forum/iphone-sdk-development/40710-gamekit-api.html

Registered Member
 
Join Date: Feb 2010
Posts: 3
Default

Quote:
Originally Posted by iPhoneSpain View Post
Hi!

This time I´ll try to post my question very clear because I think I messed up last time.

1 - I want to connect several iPhones using Gamekit via Bluetooth.

2 - I don´t want to use GKPeerPickerController (I want to implement a custom picker myself). Thus, I have to use GKSession and GKSessionDelegate.

3 - I´ve tried (with no result) with this methods:

GKSession *currentSession=[[GKSession alloc] initWithSessionID:@"test" displayName:@"XXX" sessionMode:GKSessionModePeer];

[currentSession peersWithConnectionState:GKPeerStateAvailable];

¿Any idea?

I do really need to get this solved.

Thanks in advance
I hope with "several" you mean less than 4. Bluetooth on the iPhone can't handle any more than that (see here.).

Anyways, here's a step by step guide:
1. Init the session, just like you did.
2. Set the delegate and the isAvailable property.
3. Set the data receiver.
Code:
self.curSession = [[GKSession alloc] initWithSessionID:nil displayName:nil sessionMode:GKSessionModePeer];
self.curSession.delegate = self;
self.curSession.available = YES;
self.curSession.disconnectTimeout = 0;
		
// Set data handler.
[self.curSession setDataReceiveHandler:self withContext:nil];
4. Fire up a second device.
5. Wait.
6. The delegates method "didChangeState" should be called. The state should be "GKPeerStateAvailable". Once there, connect to that peer, using the GKSession's "connect" method.

Code:
- (void)session:(GKSession *)session peer:(NSString *)peerID didChangeState:(GKPeerConnectionState)state {
	
	// Retain new session.
	self.curSession = session;
	
	// Do stuff depending on state.
	switch (state) {
		case GKPeerStateAvailable:
			[session connectToPeer:peerID withTimeout:0];
			break;
	}
}
7. Wait (the first connection can take up to 30 seconds, your bluetooth icon should be blinking in the process).
8. The delegates method "didReceiveConnectionRequestFromPeer" should be called. Accept the connection there using the sessions "acceptConnectionFromPeer" method.
Code:
- (void)session:(GKSession*) session didReceiveConnectionRequestFromPeer:(NSString*) peerID {
	
    [session acceptConnectionFromPeer:peerID error:nil];
}
9. Again, "didChangeState" will be called, this time with "GKPeerStateConnected".
10. Boom, you are done.

Don't forget to handle the other delgates methods, e.g. disconnecting the peer in "connectionToPeerFailed". Also, disconnect all peers and release the Bluetooth session before quitting the app.

And, if for the love of god you just can't get it to work, try restarting the device. Sometimes BT just stopped working for me.

Last edited by Regnits; 02-17-2010 at 03:08 AM.

:
Posted by netkorea