Advertise here




Advertise here

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Gamekit API

iPhoneSpainiPhoneSpain Posts: 41Registered Users
edited March 2012 in iPhone SDK Development
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
Post edited by iPhoneSpain on

Replies

  • ZunePodZunePod Posts: 1,031Registered Users
    edited February 2010
    This should shine some light on it:

    iPhone Bluetooth Programming
    <div align="center">Will code for food</div>
  • iPhoneSpainiPhoneSpain Posts: 41Registered Users
    edited February 2010
    ZunePod wrote: »
    This should shine some light on it:

    iPhone Bluetooth Programming

    Thanks 4 the quick reply but that tutorial (which was indeed the one I followed to learn something about bluetooth connection in the iPhone) uses GKPeerPickerController. The problem is that the vast majority of the tutorials related to bluetooth use that picker (but is up to you whether use it or not)

    I want to connect to or more pickers without using that interface.

    Help! :-)
  • ZunePodZunePod Posts: 1,031Registered Users
    edited February 2010
    I can't think why you wouldn't want to use that one, it makes things easier.
    <div align="center">Will code for food</div>
  • iPhoneSpainiPhoneSpain Posts: 41Registered Users
    edited February 2010
    ZunePod wrote: »
    I can't think why you wouldn't want to use that one, it makes things easier.

    I may be understanding things wrong. With that dialog you can see all the peers available for connecting. When you click in one of them, you connect with that peer, and to see again all the peers available you need to show again that dialog in order to connect to other one. Is this that way or you can select all the peers you want to connect with? I don´t think so...

    So the reason why I do not want to use that dialog is because I want to connect to several peers available at the same time without having to show that dialog every time I want to connect to a peer.

    I don't know if I am explaining myself... :-)
  • ZunePodZunePod Posts: 1,031Registered Users
    edited February 2010
    Yep, that's out of my knowledge, I'll try to find out for you.
    <div align="center">Will code for food</div>
  • iPhoneSpainiPhoneSpain Posts: 41Registered Users
    edited February 2010
    ZunePod wrote: »
    Yep, that's out of my knowledge, I'll try to find out for you.

    I'd really be very thankful if yo did so :D
  • RegnitsRegnits Posts: 3New Users
    edited February 2010
    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.
    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.
    - (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.
    - (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.
  • iPhoneSpainiPhoneSpain Posts: 41Registered Users
    edited February 2010
    Yeah it worked!!!!!! :-)

    Thanks a lot. I am really impressed that you have taken a little bit out of your time to get this solved 4 me. I am a bit new posting in forums an all this stuff and when I come across people as helpful as you really blows me away :-).

    One more question by the way...

    How can I get the name of the iPhone whoose peerID is X? I mean...the only info I have about the iPhone I am hooked up with is the peerID, isn´t it? I would like to show the iPhone name as usual...

    You know something about this?
  • RegnitsRegnits Posts: 3New Users
    edited February 2010
    Try looking at the GKSession Class Reference, there is a method called "displayNameForPeer:" which does exactly what you want.
  • iPhoneSpainiPhoneSpain Posts: 41Registered Users
    edited February 2010
    Yeah I had already found it!

    Thanks one more time :-)
  • iPhoneSpainiPhoneSpain Posts: 41Registered Users
    edited February 2010
    Hi again!

    I have some problems when connecting more than two devices. Imagine three iPhones (1, 2 and 3).

    - I fire up 1.
    - I fire up 2. Then method didChangeState is called into GKPeerStateAvailable case. As I call connectToPeer, then didChangeState is called again into GKPeerStateConnected this time. Everything is OK. Two devices connected.

    The problem is when I fire up 3. didChangeState is always called into GKPeerStateAvailable but sometimes it connects (didChangeState is called again into GKPeerStateConnected) once and then when I fire it up again it never connects.... But if I switched off devicew 2, then 1 and 3 can connect.

    ¿is it only possible to connect to devices with Gamekit? I don't think so...

    So somebody can figure out what's happening?

    Thanks again
  • intomointomo Posts: 62Registered Users
    edited November 2010
    Thanks for all this. It was very helpful.

    I've got bluetooth working just fine.
    Am I correct in assuming that this doesn't work over WIFI?
    I do all the same with my interface with BT turned off, and I do not see any available peers.

    In order to connect over WIFI, must I then use Bonjour directly?
    Any suggestions are appreciated.
    Thanks in advance.
  • lxx719lxx719 Posts: 15Registered Users
    edited April 2011
    Now I have the same question, but it has been years since the discussion.
    I want to know if you still work with it.
    I don't like the GKPeerPickerController default UI. But without it, I can't detect weather the bluetooth is open, because I want to communication just with bluetooth other than wifi. How can I implement it. Using BT, we know the found people is around, while he can be anywhere by wifi.
    Thank you!!
    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
  • lxx719lxx719 Posts: 15Registered Users
    edited April 2011
    Now I have the same question, but it has been years since the discussion.
    I want to know if you still work with it.
    I don't like the GKPeerPickerController default UI. But without it, I can't detect weather the bluetooth is open, because I want to communication just with bluetooth other than wifi. How can I implement it. Using BT, we know the found people is around, while he can be anywhere by wifi. or there is any API to open bluetooth and communicate with BT?
    Thank you!!
    Regnits wrote: »
    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.
    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.
    - (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.
    - (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.
  • intomointomo Posts: 62Registered Users
    edited April 2011
    Did you implement the class that you are doing that in as a GKSessionDelegate? Otherwise your delegate methods aren't going to fire.

    @interface SessionConnector : NSObject <GKSessionDelegate>{
    
    	GKSession *_session;
    
    }
    
    @property (nonatomic, retain) GKSession *session;
    

    Also, do all your devices have BT turned on?
    Do you have more than 1 device?

    I have used GameKit on 3 devices and I've gotten it to work fine.

    HTH
    lxx719 wrote: »
    Now I have the same question, but it has been years since the discussion.
    I want to know if you still work with it.
    I don't like the GKPeerPickerController default UI. But without it, I can't detect weather the bluetooth is open, because I want to communication just with bluetooth other than wifi. How can I implement it. Using BT, we know the found people is around, while he can be anywhere by wifi. or there is any API to open bluetooth and communicate with BT?
    Thank you!!
  • lxx719lxx719 Posts: 15Registered Users
    edited April 2011
    GKSessionDelegate is used to create a session and communicate. I just want to open the BT in the code and search people around. or let the user to open BT by GKPeerController. When BT is close, the iOS will show a default dialog to allow the user to open the BT, and then the default pick and connect dialog appears. I don't want to select a user to connect. How could I design the dialog and just let the user to open BT? This is a APP, so I can't open every BT in every device!
    Thank you every much.
    intomo wrote: »
    Did you implement the class that you are doing that in as a GKSessionDelegate? Otherwise your delegate methods aren't going to fire.

    @interface SessionConnector : NSObject <GKSessionDelegate>{
    
    	GKSession *_session;
    
    }
    
    @property (nonatomic, retain) GKSession *session;
    

    Also, do all your devices have BT turned on?
    Do you have more than 1 device?

    I have used GameKit on 3 devices and I've gotten it to work fine.

    HTH
  • intomointomo Posts: 62Registered Users
    edited April 2011
    I don't think the SDK allows you to turn on BT except manually via the Settings panel on each device.
    This is or security.
    You certainly can't do it through GameKit.
    And GameKit will not be able to see any devices where BT is not turned on.

    HTH
    lxx719 wrote: »
    GKSessionDelegate is used to create a session and communicate. I just want to open the BT in the code and search people around. or let the user to open BT by GKPeerController. When BT is close, the iOS will show a default dialog to allow the user to open the BT, and then the default pick and connect dialog appears. I don't want to select a user to connect. How could I design the dialog and just let the user to open BT? This is a APP, so I can't open every BT in every device!
    Thank you every much.
  • lxx719lxx719 Posts: 15Registered Users
    edited May 2011
    Thanks for your reply!
    intomo wrote: »
    I don't think the SDK allows you to turn on BT except manually via the Settings panel on each device.
    This is or security.
    You certainly can't do it through GameKit.
    And GameKit will not be able to see any devices where BT is not turned on.

    HTH
  • zero_zero_ Posts: 99Registered Users
    edited September 2011
    Regnits wrote: »
    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.
    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.
    - (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.
    - (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.
    Hi,

    very interesting thread.
    I've my own piece of code in a test app that make only voice chat.
    It works very well also in backgroud, but if i receive a call when it finish voice chat is not funcitionally
    I've notice that GKStateDisconnet is not be called during the call.
    I must work on didEnterFore/Background method?
    Or there are some other optional delegate method of GKSession that may help me?

    Thank's.
    <a href="http://itunes.apple.com/it/app/high-altimeter-and-stuff/id368770019?mt=8" target="_blank">high - GPS Tracker and Altimeter Stuff v2.3.1 - new GPS Tracker and other cool Stuff!!!</a>
  • fhsjaagshsfhsjaagshs Posts: 93Registered Users
    edited December 2011
    I was having issues with this kind of Bluetooth connection. I came across this thread. Here's my situation:

    I have these methods written into my app delegate. I also have the GKPeerPickerController in a viewController.

    It worked well when I used an older version of my app that did not have the app delegate with the code from this forum. As soon as I used two devices with the same version of my app (with the app delegate having the code from this forum), the GKPeerPickerController says "waiting for /Device name" for five minutes. No connection dialogue.

    How can I: make the code from this forum (when implemented in the AppDelegate) work with a GKPeerPickerController that is in one of my view controllers?
    <b><b>My apps</b></b><br />
    <a href="http://itunes.apple.com/us/app/rest-counter/id464985741?ls=1&mt=8" target="_blank">Rest Counter - Count bars of rest in orchestra with ease!!!</a><br />
    <a href="http://itunes.apple.com/us/app/swiftload/id4697629
  • brunnerbrunner Posts: 1New Users
    edited March 2012
Sign In or Register to comment.