iPhone SDK 3.0의 GameKit
-
20-08-2019 - |
해결책
이를 수행하는 두 가지 방법이 있습니다.
첫 번째 방법은 GameKit API를 사용합니다. 당신은 두 개의 개별 클래스를 가지고있는 것으로 시작합니다. 하나는 GKSessionDelegate
프로토콜 및 GameKit/Bluetooth "Handler"역할을하고 다른 하나는 프레젠테이션 UI (대부분 테이블 뷰를 가진 viewController) 역할을합니다. 당신이 그것을 연결하는 방법은 핸들러가 GameKit 알림 등을 관리 한 다음 UI에서 Delegate Methods를 호출하여 피어가 연결/떨어질 때 테이블보기를 업데이트합니다. 누가 주위에 있는지 보여주기 위해 업데이트해야합니다.
아래는 시작하기위한 몇 가지 코드입니다.
- (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 (그러나 Wi -Fi 대신 Bluetooth를 통해) 위에 구현되므로 양측이 서로 네트워크 도달 가능성을 겪고 연결하면 Bonjour에 등록되어 Bonjour 서비스와 같이 행동합니다. GameKit Way는 아마도 조금 더 쉽지만 Wi -Fi에 대한 코드가 이미 있다면 Bluetooth에도 재사용 할 수 있습니다.
다른 팁
왜 그것을 사용하고 싶지 않습니까?
Bluetooth 연결을 직접 수행하는 방법이 있다는 것을 모르겠습니다. 다른 방법으로 연결을 찾는 대안을 제시합니다. 그것은 많은 iPod / 터치 사이에 네트워크를 설정하는 꽤 좋은 시스템을 제공하고 관계가 진정으로 피어인지 마스터 / 슬레이브인지 정의 할 수 있습니다 ...