我正在使用GKSession, GKSessionDelegate
在多个 iX(iPod、iPad、iPhone 等)设备之间实现对等蓝牙连接。我想显示range中当前可用/连接的设备的列表。
我目前依赖
- (void)session:(GKSession *)session peer:(NSString *)peerID didChangeState:(GKPeerConnectionState)state
去做这个。不幸的是,当设备超出范围时,似乎无法可靠地调用此方法。是否有“更好”的方法来确定设备是否在范围内?
一些代码:
- (void)session:(GKSession *)session peer:(NSString *)peerID didChangeState:(GKPeerConnectionState)state {
NSString* connectionStateString=
(state==GKPeerStateAvailable)?@"available":
(state==GKPeerStateUnavailable)?@"unavailable":
(state==GKPeerStateConnected)?@"connected":
(state==GKPeerStateDisconnected)?@"disconnected":@"connecting";
// Add the peer to the Dictionary
NSArray* details=[NSArray arrayWithObjects:[session displayNameForPeer:peerID],connectionStateString,nil];
[connectionPeers setObject:details forKey:peerID];
if (state == GKPeerStateAvailable) {
NSLog(@"Adding peerID:%@",peerID);
[session connectToPeer:peerID withTimeout:60];//'connect' to everything, so data can be sent
}
else if (state == GKPeerStateUnavailable || state==GKPeerStateDisconnected) {
[connectionPeers removeObjectForKey:peerID];
}
[self listPlayers];
}