GameKit Tutorial: GKSession

I want to connect two devices using GKSession, starting from the server, and the other as a client. Using this configuration, I cannot use the GKPeerPickerController.

I am having problems connecting two devices:

  • Using bluetooth only: not possible
  • using WiFi: at least there is some data exchange between devices, but there is no successful connection.

In the interface file I have

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 begins to use this code:

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

How can I get to use bluetooth instead of wifi?

I also made these 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 in the debugger:

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

And when another device starts to find, I get the following:

 ~ 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 am I missing here?

I am sure that both devices have bluetooth and are connected to the same WiFi.

thanks,

g.

+6
iphone gamekit
source share
2 answers

I think you do not agree with the connection to the client. After receiving the “didReceiveConnectionRequestFromPeer” callback, you need to accept the client connection as follows:

 -(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 that, you will get "GKPeerStateConnected" here:

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

Hope this helps you.

+1
source share

I had similar problems, but from the above description, I think you are making some Mistakes:

GKSession only implements BT; if you use the collector, then you can provide separate methods for working with WiFi connections.

The didReceiveConnectionRequestFromPeer method should call the acceptConnectionFromPeer method on the session object - you do not implement "acceptConnectionFromPeer" on your own.

For debugging, you must register state changes in the delegate method "Session: peers: didChangeState:" (see http://developer.apple.com/iPhone/library/documentation/GameKit/Reference/GKSessionDelegate_Protocol/Reference/Reference.html ). When the peer connection is "available", you can call "connectToPeer:"; when "Connected", you can use "sendData: toPeers:".

For IO, after you have established a connection, you want to call the "setDataReceiveHandler: withContext:" method in the session.

I used to have some typos in my code, but now it works.

Good luck.

-one
source share

All Articles