Is it possible to have two instances of GKSession on the same device when using Bluetooth? - iphone

I am writing mutliplayer game and thought I would create two instances of GKSession. One session set to GKSessionModeServer and the other to GKSessionModeClient. The server is properly reported when I call peersWithConnectionState: on the client peer and each session has different peer ids. But when I try to connect to the server I receive the message session:connectionWithPeerFailed:withError: with the error description "Failed while pending outgoing invitation.".
Using two GKSessions works when Wi-Fi is available. I am afraid that there is some limitation with Bluetooth that disallows this but I am not sure. I could rewrite the code to use a single GKSession on the server but I would rather not code special cases if I know that someone else got it working with two instances.
If I turn off both Wi-Fi and Bluetooth i get the error "Network not available." which I don't get if either is turned on. This makes me believe it won't work when there is another iPhone is nearby either.

I rewrote the code so I only create one GKSession on the hosting device and send data between the server and the local client directly without using the GKSession. I could finally test this with two devices and they properly connect to each other. So the lesson is to have only one GKSession on the host and it will work with Bluetooth as well as Wi-Fi.

Related

Sending a contact from an iOS app via bluetooth

I am trying to allow a user, who is running my app, to send a contacts vcard (vcf) via bluetooth to another iOS device.
The problem is, that the receiving device should not be required to run the same app.
Is there a possibiliy to accomplish this?
If there is not - is it at least possible to have Gamekit to establish a session, when the receiving device is running the same app, but without requiring the (receiving) user to start the peerPicker-dialog?
Any help and hints are appreciated!
First option: seems to be impossible.
Second option: Works. I implemented a GKSessionDelegate for receiving incoming connections. This session is startet together with the app.
For establishing the connection with a receiver, I used the well known GKPeerPickerControllerDelegate.
One remainig problem is, that the sender sees itself in the list of available peers (because it has also a receiver running in the background). My solution for this is to set its GKSession.available=NO, as soon as the sender starts its GKPeerPickerController.

Troubleshooting to connect and disconnect to network

Suppose I have a application which fetch data from network and then showing those data in application. But sometimes internet becomes disconnected so I am showing message that 'Internet not available'. But I want to keep user busy with the application. So I want to do following things but there are some problem in implementation.
1) When network disconnected I want to show message 'network disconnect' and I want to store all data in db.
Problem: How will I know network is not available without happening any event?
2) And when network is connected I want to fetch data and store in buffer.
Problem : Same, how will I know network is available rightnow?
This is quite common question and is best solved using Reachability class.
You can find a nice manual here: Testing Network Reachability
And a quick answer is the accepted answer here:
iPhone reachability checking
But be aware that it's best to check for host availabiility. I tested this code when device was connected to AdHoc WiFi (with no internet connectivity) and the result was: internet available via WiFi. So if you want to be sure - use host status.
EDIT: and this is probably the best example on SO (answer from iWasRobbed):
How to check for an active Internet connection on iOS or OSX?

iPhone as proxy for data upload

I want to create an iPhone application that can call web services on a computer, and then upload those files over the iPhone 3g connection. Is there an elegant way to do this?
I tried connecting the phone to local Wifi, but connecting to Wifi disables 3g, even though the Wifi is local network only.
I assume the same problem would arise if I could network the phone with the computer via bluetooth.
Any other ideas? The purpose is to go up to a special demo PC we created, pull some data off, then upload to the Internet, all within proximity of the machine. Unfortunately, it appears that I can't use one connection for pulling data, and another for pushing.
Your assumption is correct. When connecting to both 3G and Wifi, the Wifi connection will be used. Sounds like your local network has no route to the Internet. Perhaps that could be fixed.
Apple does not prefer its iPhones to be used as routers.
If you're working on something that will not be distributed through the App Store, you may have some success looking at "tethering" applications and their source code, which can be found by some clever Googling.

Forcing a WWAN connection on iPhone, even when Wi-Fi is available

I need to make a network connection over WWAN (i.e. the mobile network) on an iPhone, even when the device is connected to a Wi-Fi network, however I can't find a way to do this.
I've tried going down to the socket level and iterating through the available interfaces, however when connected to Wi-Fi, the WWAN interface (pdp_ip0) disappears.
The solution needs to be App Store safe.
Enumerate the network interfaces, you will see that when you're connected on both wifi and 3g, there are 2 with different local ip addresses. You might be able to use one vs the other by forcing a bind of your socket on the right interface before sending the data. The kernel which tries to find the best interface to route your packet should be happy with your choice.
Disclaimer: I have not tried this, this is just a suggestion.
Perhaps you can use the Reachability code to determine if Wi-Fi is enabled, firing a UIAlertView to warn the users to quit the app, open the Settings app and switch off wireless manually. Not ideal, definitely.
There is no supported way to to do this. You need to tell the user to turn off the WiFi connection since "It's a non-negotiable operational requirement for the service the app needs to connect to."
In this scenario, the user is not likely to kill you with bad reviews if you are clear about why they have to disable WiFi.
-t

iPhone p2p - Is there a way to connect to more than 1 devices?

Is it possible to connect to more than 1 devices using the new GameKit framework? Till now all the examples I've seen show how we can connect to 1 device. Even the default connection interface lets the user select just one connect.
Thanks.
There's a fairly good overview here at the following link of how to configure and use GameKit for your App.
GameKit Programming Guide: Peer-to-Peer Connectivity
Unfortunately, you will not be able to use the GKPeerPickerController standard UI picker to configure the GKSession instance for you if you want to support more than 2 devices. Instead, you must develop your own UI elements to setup and configure the connections between the master/server and the multiple clients.
Here's a simple way to enable GameKit for more than 2 devices:
When you initialize a GKSession instance with initWithSessionID:displayName:sessionMode, use:
sessionMode:GKSessionModeServer for the server/master
sessionMode:GKSessionModeClient for the other (multiple) clients
sessionMode:GKSessionModePeer is the 3rd available option and is the option used in most sample code provided by Apple such as the GKTank demo.
Note: You can probably use GKSessionModePeer and still have a network of more than 2 devices since peer mode puts the device in server and client mode at the same time until a connection is made. Explicitly configuring one device as the server may simplify the setup which is useful for testing the GameKit API.
For the server:
GKSession *session = [[GKSession alloc] initWithSessionID:kTestSessionID displayName:nil sessionMode:GKSessionModeServer];
For the client(s):
GKSession *session = [[GKSession alloc] initWithSessionID:kTestSessionID displayName:nil sessionMode:GKSessionModeClient];
In the past I've read you can connect up to sixteen devices using Gamekit. I believe that you can only do two when in Peer mode, to do more than two requires one device be set as master and the others in client mode.
I'd look for examples that demonstrate use of master/client, the client side should apply to all three.
You can call +maxPlayersAllowedForMatchOfType: on GKMatchRequest (iOS6+) to get the actual number, which could vary per device so you should probably use that as an upper bounds.
To the best of my knowledge, when using the GameKit framework you can only couple a pair of devices, one acting as a server and the other as a client (if both are initialized as peers, you will have to decide which one will act as a server, either arbitrarily or by selecting one randomly).
In practice, there may be many devices all running your application, but connections will always be established through paired devices.
Of course, you can write your own bonjour-based networking code over wifi and overcome this limit though it will require much more coding.