Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GameKit: GKSession manual

Tags:

iphone

gamekit

I want to connect two devices using the GKSession, starting one as a server and the other one as a client. Using this configuration I can't use the GKPeerPickerController.

I'm having problems for connecting the two devices:

  • Using only bluetooth: impossible
  • using WiFi: at least there are some data exchange between the devices but no successfully conection.

In the interface file I have the

GKSessionDelegate
GKSession *session;

In the implementation, I start the server using this code:

session = [[GKSession alloc] initWithSessionID:@"iFood"  displayName:nil sessionMode:GKSessionModeClient];
session.delegate = self;
session.available = YES;

The client starts using this code:

session = [[GKSession alloc] initWithSessionID:@"iFood"  displayName:nil sessionMode:GKSessionModeServer];
session.delegate = self;
session.available = YES;

How I can force the use of Bluetooth instead of the WiFi ?

Also I have implemented those calls:

-(void)session:(GKSession *)session didReceiveConnectionRequestFromPeer:(NSString *)peerID {
NSLog(@"Someone is trying to connect"); 
}

- (BOOL)acceptConnectionFromPeer:(NSString *)peerID error:(NSError **)error {
NSLog(@"acceptConnectionFromPeer");
}

When I start, I get this into the debugger:

Listening on port 50775
2010-02-19 14:55:02.547 iFood[3009:5103] handleEvents started (2)

And when the other device starts to find, I get this:

~ DNSServiceBrowse callback: Ref=187f70, Flags=2, IFIndex=2 (name=[en0]), ErrorType=0 name=00eGs1R1A..Only by Audi regtype=_2c3mugr67ej6j7._udp. domain=local.
~ DNSServiceQueryRecord callback: Ref=17bd40, Flags=2, IFIndex=2 (name=[en0]), ErrorType=0 fullname=00eGs1R1A\.\.Only\032by\032Audi._2c3mugr67ej6j7._udp.local. rrtype=16 rrclass=1 rdlen=18 ttl=4500
** peer 1527211048: oldbusy=0, newbusy=0
~ DNSServiceBrowse callback: Ref=187f70, Flags=2, IFIndex=-3 (name=[]), ErrorType=0 name=00eGs1R1A..Only by Audi regtype=_2c3mugr67ej6j7._udp. domain=local.
GKPeer[186960] 1527211048 service count old=1 new=2
~ DNSServiceQueryRecord callback: Ref=17bd40, Flags=2, IFIndex=-3 (name=[]), ErrorType=0 fullname=00egs1r1a\.\.only\032by\032audi._2c3mugr67ej6j7._udp.local. rrtype=16 rrclass=1 rdlen=18 ttl=7200
** peer 1527211048: oldbusy=0, newbusy=0
~ DNSServiceBrowse callback: Ref=187f70, Flags=2, IFIndex=-3 (name=[]), ErrorType=0 name=00TF5kc1A..Only by Audi regtype=_2c3mugr67ej6j7._udp. domain=local.
~ DNSServiceQueryRecord callback: Ref=188320, Flags=2, IFIndex=-3 (name=[]), ErrorType=0 fullname=00tf5kc1a\.\.only\032by\032audi._2c3mugr67ej6j7._udp.local. rrtype=16 rrclass=1 rdlen=18 ttl=7200
** peer 1723356125: oldbusy=0, newbusy=0
~ DNSServiceQueryRecord callback: Ref=188320, Flags=2, IFIndex=2 (name=[en0]), ErrorType=0 fullname=00TF5kc1A\.\.Only\032by\032Audi._2c3mugr67ej6j7._udp.local. rrtype=16 rrclass=1 rdlen=18 ttl=4500
** peer 1723356125: oldbusy=0, newbusy=0

What I'm missing here ?

I'm sure that both devices have bluetooth enabled and connected into the same WiFi.

thanks,

r.

like image 407
mongeta Avatar asked Feb 19 '10 14:02

mongeta


1 Answers

I think you miss to accept connection with client. After you receive the "didReceiveConnectionRequestFromPeer" callback, you need to accept connection with client like this:

-(void)session:(GKSession *)session didReceiveConnectionRequestFromPeer:(NSString *)peerID {
  NSLog(@"Someone is trying to connect"); 
  NSError *error;
  [gkSession acceptConnectionFromPeer:peerID error:&error];
  if(error)
    NSLog(@"Error on accept connection with peer: ", error);
}

After this you will receive "GKPeerStateConnected" here:

- (void)session:(GKSession *)session peer:(NSString *)peerID didChangeState:(GKPeerConnectionState)state{}

I hope this help you.

like image 114
Jan Cássio Avatar answered Nov 06 '22 20:11

Jan Cássio