GKSession - 杀死和更新会话

时间:2012-01-28 20:43:24

标签: ios bluetooth gksession

为了读取对等体的新显示名称,我需要杀死并更新GKSession。将其设置为nil并重新启动它不起作用。在下面的代码中,未调用 for-loop 中的NSLog来显示可用的对等体(没有错误消息):

-(IBAction) btnRefresh:(id) sender {

    self.currentSession = nil;

    self.currentSession = [[GKSession alloc] initWithSessionID:@"anything" displayName:name sessionMode:GKSessionModePeer];
    self.currentSession.delegate = self;
    self.currentSession.available = YES;
    self.currentSession.disconnectTimeout = 0;
    [self.currentSession setDataReceiveHandler:self withContext:nil];

    peerListAvailable = [[NSMutableArray alloc] initWithArray:[currentSession peersWithConnectionState:GKPeerStateAvailable]];

    for (NSString *peer in peerListAvailable) {
       NSLog(@"found available peer; checking name and ID... %@, %@",[currentSession displayNameForPeer:peer], peer);
    }

将currentSession设置为nil并重新启动它有什么问题? 也许您知道续订GKSession的另一种方法吗? 非常感谢提前。

1 个答案:

答案 0 :(得分:3)

以下方法说明了GKSession设置和拆解:

- (void)setupSession
{
    gkSession = [[GKSession alloc] initWithSessionID:nil displayName:nil sessionMode:GKSessionModePeer];
    gkSession.delegate = self;
    gkSession.disconnectTimeout = 5;
    gkSession.available = YES;
}

- (void)teardownSession
{
    gkSession.available = NO;
    [gkSession disconnectFromAllPeers];
}

如果您对深入研究感兴趣,请查看GKSessionP2P,这是一个演示应用程序,用于说明GKSession的ad-hoc网络功能。该应用程序都在本地网络上进行通告,并自动连接到可用的对等方,建立对等网络。