iPhone SDK 3.0 中的 GameKit [英] GameKit in iPhone SDK 3.0

查看:19
本文介绍了iPhone SDK 3.0 中的 GameKit的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

需要使用Peer Picker 在新的 iPhone SDK 3.0 中寻找同行?

Do I need to use the Peer Picker to find peers in the new iPhone SDK 3.0?

我真的不想使用它,但我确实想使用点对点蓝牙连接.是否有任何示例代码可以在不使用 Peer Picker 的情况下演示蓝牙连接?Apple 提供的游戏 GKTank 使用 Peer Picker,所以我不能使用它.

I don't really want to use it, but I do want to use the peer-to-peer Bluetooth connection. Is there any sample code that demonstrates the Bluetooth connection without using Peer Picker? The game GKTank that Apple provides uses the Peer Picker, so I can't use that.

推荐答案

有两种方法可以做到这一点.

There are two ways to do this.

第一种方式使用 GameKit API.您首先拥有两个独立的类,一个实现 GKSessionDelegate 协议并充当 GameKit/蓝牙处理程序",另一个充当演示 UI(很可能是某种带有 tableview 的视图控制器).您连接它的方式是处理程序管理 GameKit 通知等,然后调用 UI 上的委托方法以在对等连接/断开等时更新表视图.这样,随着设备的来来去去,您的选择器列表应该更新以显示周围的人.

The first way uses GameKit API. You start by having two separate classes, one that implements the GKSessionDelegate protocol and acts as a GameKit/Bluetooth "handler" and the other as the presentation UI (most likely some sort of viewcontroller with a tableview). The way you would wire it up is the handler manages the GameKit notifications etc and then calls delegate methods on the UI to update the table view when a peer connects/drops off, etc. That way, as devices come and go, your picker list should update to show who's around.

以下是一些帮助您入门的代码:

Below is some code to get you started:

- (BOOL) startPeer
{
    BOOL result = NO;

    if (!_session) {
        _session = [[GKSession alloc] initWithSessionID:BLUETOOTHSESSION 
                                                displayName:nil 
                                                sessionMode:GKSessionModePeer];
        _session.delegate = self;
        [_session setDataReceiveHandler:self withContext:nil];
        _session.available = YES;
    result = YES;
    }
    return result;
}

- (void) stopPeer
{
    // Set up the session for the next connection
    //
    [_session disconnectFromAllPeers];
    _session.available = YES;

    [self cleanupProgressWindow];
}

- (void) loadPeerList 
{
    self.peerList = [[NSMutableArray alloc] initWithArray:[_session peersWithConnectionState:GKPeerStateAvailable]];
}


- (void)session:(GKSession *)session peer:(NSString *)peerID didChangeState:(GKPeerConnectionState)state
{
    BOOL peerChanged = NO;

    switch(state) {

        // When peer list changes, we adjust the available list
        //
        case GKPeerStateAvailable:
            if (_peerList) {
                [_peerList addObject:peerID];
                peerChanged = YES;
            }
            break;

        // When peer list changes, we adjust the available list
        //
        case GKPeerStateUnavailable:
            if (_peerList) {
                [_peerList removeObject:peerID];
                peerChanged = YES;
            }
            break;


        // Called when the peer has connected to us.
        //
        case GKPeerStateConnected:
                    // start reading and writing
            break;

        case GKPeerStateDisconnected:
        {
            if (_isWriter) {
                _isConnected = NO;
                _deviceToSend = nil;
                [self cleanupProgressWindow];
            } else {
                // Other side dropped, clean up local data and reset for next connection
                self.dataRead = nil;
            }
        }
            break;
    }

    // Notify peer list delegate that the list has changed so they can update the UI
    //
    if (peerChanged)
        CALLDELEGATE(_peerListDelegate, peerListChanged);           
}

第二种方法是使用标准的 Bonjour 服务选择机制.GameKit 是在 Bonjour 之上实现的(但通过蓝牙而不是 WiFi),所以一旦双方通过网络可达性并连接起来,它们就会在 Bonjour 下注册,并像任何 Bonjour 服务一样运行.GameKit 方式可能更简单一些,但如果您已经有 WiFi 代码,它也可以重复用于蓝牙.

The second way to do it is to use standard Bonjour service selection mechanisms. GameKit is implemented on top of Bonjour (but over Bluetooth instead of WiFi) so once the two sides have gone through network reachability with each other and connected they are registered under Bonjour and act like any Bonjour service would. The GameKit way is probably a little easier, but if you already have code for WiFi it can be reused for Bluetooth as well.

这篇关于iPhone SDK 3.0 中的 GameKit的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆