Online Book Reader

Home Category

iPhone Game Development - Chris Craft [96]

By Root 1535 0
or another, you should have Bluetooth on at this point. The next screen in the workflow waits for nearby players to join (Figure 7.9). P2P Chat uniquely identifies itself with a session ID so only nearby devices that have P2P Chat loaded will show up.

As soon as another player is found, the name of that player's device appears in a list (Figure 7.10). One player can then select the other player he wishes to connect with and wait for that player to click Accept on the screen (Figure 7.11).

Once connected, the devices can send messages to each other (Figure 7.12). Okay, sending text messages to someone only a foot or two away isn't exactly revolutionary. However, this illustrates a straightforward implementation of the peer-to-peer connection process.

FIGURE 7.9

You see this screen when peer picker is waiting on nearby instances of P2P Chat to advertise their session.


FIGURE 7.10

The peer picker list of nearby sessions


Dissecting the code behind P2P Chat

Now that you are more familiar with P2P Chat, let's look at the code and see what makes it tick. P2P Chat utilizes features of the Game Kit frameworks to connect devices. Game Kit introduces features that allow different devices to connect. At the time of this writing, Game Kit includes two technologies:

Peer-to-peer connectivity. This enables you to create a Bluetooth network between two devices. Even though it was designed for games, Apple encourages its use for any application that can benefit from an ad hoc network between two devices.

Note

As of the iPhone 3.0 OS, peer-to-peer has a two-device limit.

In game voice. This provides voice communication between multiple devices over any network. This can be layered on top of a Bluetooth connection or a wireless Internet connection.

In P2P Chat we used the peer-to-peer connectivity currently available in Game Kit. When utilizing this feature you need to utilize two new classes introduced in the frameworks:

GKPeerPickerController. This controller manages the peer picker we looked at earlier. You will need to activate an instance of the class as well as implement a delegate that it provides to receive and respond to the messages it sends.

GKSession. This is the class that implements the Bluetooth network between two devices. Use an instance of this class to configure and manage the connection between the two devices. GKSession also supplies a delegate that is literally used to receive messages.

FIGURE 7.11

The peer picker “accept connection” prompt


FIGURE 7.12

P2P Chat is connected and communicating.


Introducing the GKPeerPickerController

Before working with a session, we need to connect to another device. Thankfully, Apple has provided the GKPeerPickerController that does just that. The peer picker comes complete with its own user interface for locating and accepting connections.

Here are the steps needed to add peer picker to the P2P Chat example:

1. Whenever the Find button is clicked, create an instance to the class GKPeerPickerController if one did not already exist:

GKPeerPickerController *picker =

[[GKPeerPickerController alloc] init];

2. Attach the delegate:

picker.delegate = self;

3. Configure the network types that you would like to allow:

picker.connectionTypesMask = GKPeerPickerConnectionTypeNearby |

GKPeerPickerConnectionTypeOnline;

Note

GKPeerPickerConnectionTypeOnline is included for demonstration purposes. If you leave it out, nearby connections will be used by default and there will be one less step in the connection process. To leave it out, replace the code for Step 3 with this:

picker.connectionTypesMask = GKPeerPickerConnectionTypeNearby;

4. Set the instance property and release the locale reference:

self.peerPicker = picker;

[picker release];

5. Finally, disable the Find button to keep the user from clicking it again, and then call show to launch the Peer Picker dialog box:

findButton.enabled = false;

[self.peerPicker show];

These additions will get the peer picker to show on the screen, but you still need to implement

Return Main Page Previous Page Next Page

®Online Book Reader