Can't see why this is happening, but when I use Bluetooth to send data to connected peers, they receive the data perfectly well but then immediately disconnect making further interaction impossible. Can anyone give a开发者_JS百科ny indication as to why this might be happening? I tried changing the timeout to 60 seconds, I tried reducing the amount of data sent to less than 500 bytes, and still I get this strange situation where peers will immediately disconnect from one another. Is it maybe because I'm using a GKSession to initialise the network, then a GKPeerPicker for people to connect to it?
Here's my data sending-and-receiving code. This is mostly temporary and will probably change to accommodate large amounts of data, but right now I just want to establish a stable connection.
-(void)transmitWholeClusterToPeer:(NSString *)peerID{
NSMutableData *data = [NSMutableData data];
NSKeyedArchiver *encoder = [[NSKeyedArchiver alloc]initForWritingWithMutableData:data];
[encoder encodeInt:0 forKey:@"Mode"];
[encoder encodeObject:clusterMap.header forKey:@"Header"];
[encoder finishEncoding];
NSArray *peerList = [[[NSArray alloc] initWithObjects:peerID, nil] copy];
[network sendData:data toPeers:peerList withDataMode:GKSendDataReliable error:nil];
[encoder autorelease];
[peerList autorelease];
}
-(void)receiveData:(NSData *)data fromPeer:(NSString *)peer inSession:(GKSession *)session
context:(void *)context{
NSKeyedUnarchiver *decoder = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];
int mode = [decoder decodeIntForKey:@"Mode"];
switch (mode) {
case 0:
clusterMap.header = [decoder decodeObjectForKey:@"Header"];
break;
default:
break;
}
}
I can't say for sure this is the definitive answer, but I have solved the problem after a fashion by removing GKPeerPicker and implementing my own Client-Server setup code that generates 'keep alive' pings itself. I think the problem really was only that I was setting up a session manually, then using the Peer Picker to connect to it. It seems that Peer Pickers are only stable and useful when they are connecting to other Peer Pickers.
精彩评论