currentSession を次のページに渡すことに関して質問があります。最初のページビューコントローラーでBluetoothセッションをテストし、正常に確立しました。ここで私の質問は、次のページに移動した後、Bluetooth 接続が失われるため、現在のセッションを次のページに渡すにはどうすればよいかということです。したがって、次のページに移動した後、現在のセッションは失われます。これは、2 台の電話でランダムな画像を送信することによってテストされました。ただし、nextpageController で 2 回目の Bluetooth 接続を要求すると、画像が送信される可能性があります。
ビューコントローラー.M:
-(IBAction)btnBluetoothConnect:(id)sender {
picker = [[GKPeerPickerController alloc] init];
picker.delegate = self;
picker.connectionTypesMask = GKPeerPickerConnectionTypeNearby;
[picker show];
}
- (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];
}
- (void)peerPickerControllerDidCancel:(GKPeerPickerController *)pk {
picker.delegate = nil;
}
- (void)session:(GKSession *)session peer:(NSString *)peerID didChangeState:(GKPeerConnectionState)state {
switch (state) {
case GKPeerStateConnected:
NSLog(@"connected");
if (GKPeerStateConnected) {
self.progress = [[ProgressAlertView alloc] init];
progress.delegate = self;
progress.task = @selector(compute);
[progress start];
}
break;
case GKPeerStateDisconnected:
NSLog(@"disconnected");
currentSession = nil;
default:
break;
}
}
-(void)compute{
[self updateUIWithProgress:0.0 andMessage:@"3"];
[NSThread sleepForTimeInterval:3];
if([progress isCancelled]){
self.progress = nil;
return;
}
[self updateUIWithProgress:0.4 andMessage:@"2"];
[NSThread sleepForTimeInterval:1];
if([progress isCancelled]){
self.progress = nil;
return;
}
[self updateUIWithProgress:0.6 andMessage:@"1"];
[NSThread sleepForTimeInterval:1];
if([progress isCancelled]){
self.progress = nil;
return;
}
[self updateUIWithProgress:0.8 andMessage:@"0"];
[NSThread sleepForTimeInterval:1];
if([progress isCancelled]){
self.progress = nil;
return;
}
else
[self updateUIWithProgress:1.0 andMessage:@"Start!"];
NSLog(@"Woohahaha");
**[self performSegueWithIdentifier:@"next page" sender:self];**
//process the data here or send a notification for data processing
//when this method returns, the progress alert view disappears.
}
NextPageController.m:
-(IBAction)btnSend:(id)sender{
if(currentSession){
NSData *imageData = UIImageJPEGRepresentation([UIImage imageNamed:[NSString stringWithFormat:@"4.png"]], .7);
[self.currentSession sendDataToAllPeers:imageData withDataMode:GKSendDataReliable error:nil];
}
}
- (void) receiveData:(NSData *)data fromPeer:(NSString *)peer inSession: (GKSession *)session context:(void *)context{
imageView.image = [UIImage imageWithData:data];
}
- (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];
}
- (void)peerPickerControllerDidCancel:(GKPeerPickerController *)pk {
picker.delegate = nil;
}