关于蓝牙:iPhone SDK 3.0中的GameKit

GameKit in iPhone SDK 3.0

我是否需要使用Peer Picker在新的iPhone SDK 3.0中查找对等方?

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


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

第一种方法使用GameKit API。首先要有两个单独的类,一个实现GKSessionDelegate协议并充当GameKit / Bluetooth"处理程序",另一个作为表示UI(很可能是带有tableview的某种viewcontroller)。连接方式是处理程序管理GameKit通知等,然后在对等方连接/断开等情况下在UI上调用委托方法以更新表视图。这样,随着设备的出现,您的选择器列表应该更新以显示周围的人。

下面是一些入门的代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
- (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代码,那么它也可以重用于蓝牙。


为什么不想使用它?

我不知道有什么方法可以直接自己建立蓝牙连接,它提供了一种通过其他方式查找连接的替代方法。它提供了一个非常不错的系统,可以在多个iPod / Touches之间建立网络,并让您定义该关系是真正的对等还是主/从...。