I have made an iPhone game with Bluetooth mode, you can play 1 vs 1 via Bluetooth. My implementation for a picker is the following:
picker = [[GKPeerPickerController alloc] init];
picker.delegate = self;
picker.connectionTypesMask = GKPeerPickerConnectionTypeNearby;
[picker show];
I don't know which code gives the error so I'll also paste my code for all the other methods that have something to do with the picker:
- (void) receiveData:(NSData *)data fromPeer:(NSString *)peer inSession: (GKSession *)session context:(void *)context {
NSString *dataString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSDictionary *dataDictionary = [NSDictionary dictionaryWithObjects: [NSArray arrayWithObjects:dataString, peer, session, nil] forKeys:[NSArray arrayWithObjects:@"data", @"peer", @"session", nil]];
[dataString release];
[self performSelectorOnMainThread:@selector(receivedData:) withObject:dataDictionary waitUntilDone:YES];
}
- (GKSession *)peerPickerController:(GKPeerPickerController *)picker
sessionForConnectionType:(GKPeerPickerConnectionType)type {
// Create a new session if one does not already exist
if (!self.currentSession) {
self.currentSession = [[[GKSession alloc] initWithSessionID:@"Session" displayName:nil sessionMode:GKSessionModePeer] autorelease];
self.currentSession.delegate = self;
}
return self.currentSession;
}
-(void)peerPickerController:(GKPeerPickerController *)pk didConnectPeer:(NSString *)peerID toSession:(GKSession *)session {
self.currentSession = session;
session.delegate = self;
[session setDataReceiveHandler:self withContext:nil];
picker.delegate = nil;
[picker dismiss];
[picker autorelease];
}
-(void)peerPickerControllerDidCancel:(GKPeerPickerController *)pk {
picker.delegate = nil;
[picker autorelease];
[self.navigationController popViewControllerAnimated:YES];
}
// FAIL
- (void)session:(GKSession *)session didFailWithError:(NSError *)error {
NSLog(@"error : %@", [error description]);
}
// SESSION VIND ANDERE SESSION -> CONNECT
-(void)session:(GKSession *)session peer:(NSString *)peerID didChangeState:(GKPeerConnectionState)state {
switch (state) {
case GKPeerStateConnected:
NSLog(@"connect met peer %@", [currentSession displayNameForPeer:peerID]);
[self generateRandomNumberAndSendIt];
break;
case GKPeerStateDisconnected:
NSLog(@"disconnected");
[self.currentSession disconnectFromAllPeers];
currentSession = nil;
[self.navigationController popViewControllerAnimated:YES];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Whoopsie" message:@"The connection failed" delegate:nil cancelButtonTitle:@"Okido" otherButtonTitles:nil];
[alert show];
[alert release];
[super viewDidDisappear:YES];
break;
}
}
Sometimes when I accept an incoming request, the picker removes itself on one device, and on another device I get the error: wait_fences: failed to receive reply: 10004003
. I think it has something to do with the alertview itself. I have got other alertviews set up in this view.
I hope you guys can help me.
Thanks in advance.