Using GameKit with MonoTouch

GameKit is a framework Apple created that provides peer to peer Bluetooth networking capability. It is meant for transmitting small messages between devices in close proximity. You can use GameKit on relatively newer iDevices including iPhone 3g, iPod Touch 2nd generation and iPad (and newer versions of each as well). Although it’s called GameKit, as the small messages between nearby devices are well suited to peer to peer gaming scenarios, it can be used for any purpose. For larger data transfer, you need to chunk data into many small messages (similar to what you typically do for an http chunking upload, if you’re familiar with that). I’ll show you a simple example here of using GameKit to create a minimalist chat application, the full source code for which is available at http://github.com/mikebluestein/GameKitChat.

GameKit abstracts all the networking details for you. It even provides the UI for device connectivity via the GKPeerPickerController class. You can also use your own UI as well. Additionally, if you want to support networking other than via Bluetooth, you can do so. GameKit doesn’t add any additional networking capability other than Bluetooth but it does give you the ability to hook in your own networking code (they call it “online mode”) and still take advantage of the common interface.

In MonoTouch GameKit lives under the MonoTouch.GameKit namespace. The main class you work with to use GameKit is the GKSession. The GKPeerPickController and the GKSession together make it very easy to request connections and manage data transfer. Internally these wrap Apple’s Bonjour technology, which is their config free networking stack.

In the example below I created a simple app with a UITextView as the chat log and a UITextField to enter new messages. A UIToolBar houses a single button to launch the peer picker. The pop up dialogs shown are all from the stock UI that you get for free with UIPeerPickerController.

The basic steps are:

– Create a GKSession
– Subscribe to events on the GKSession
  – ConnectionRequest to handle accepting a connection request from a peer
  – ReceiveData to handle incoming data from a peer
– Create a GKPeerPickerController
– Set the type of connection you are supporting on the GKPeerPickerController via its ConnectionTypesMask (nearby for Bluetooth)
– Set the GKPeerPickerController’s delegate (this is where I dismisses the picker in the example)

Here’s a code snippet showing the creation of the example’s GKSession and GKPeerPickerController:


GameKitSession = new GKSession ("com.mikebluestein.gamekitchat", 
                                 UIDevice.CurrentDevice.Name, 
                                 GKSessionMode.Peer);

GameKitSession.ReceiveData += (s, e) => { 
  AddMessage (NSString.FromData (e.Data, 
                       NSStringEncoding.UTF8).ToString ()); };

GameKitSession.ConnectionRequest += (s, e) => { 
  e.Session.AcceptConnection (e.PeerID, IntPtr.Zero); };

GKPeerPickerController peerPickerController = 
  new GKPeerPickerController ();

peerPickerController.Delegate = new PeerPickerDelegate (this);

peerPickerController.ConnectionTypesMask = GKPeerPickerConnectionType.Nearby;

peerPickerController.Show ();

The callback to ReceivedData takes a GKDataReceivedEventArgs that includes an NSData containing the message that was sent by a peer (in this case a text message that they entered in a UITextField). The data is sent via a call to GKSession.SendDataToAllPeers. The PeerPickerDelegate is a subclass of GKPeerPickerControllerDelegate. See the solution for the full implementation. Also, GameKit requires you to use the device for Bluetooth networking as this isn’t supported in the simulator, so you’ll need at least 2 devices. Here’s a demo video of the app running between an iPhone 3g and an iPad:

One other thing worth mentioning is the current GameKit stack also has support for peer-peer voice (think in game voice scenarios) and future versions will add more capabilities as well. The Apple GameKit Programing Guide is a good reference to learn more.

7 thoughts on “Using GameKit with MonoTouch

  1. Pingback: MonoTouch.Info
  2. hmmm… I suspect(!) that selecting NEARBY connects you over bluetooth OR local wifi (if bluetooth is off), unfortunately I have no idea how to change this behaviour (wifi connections are not very good)
    Regards

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s