我有一个关于将currentSession传递到下一页的问题。 我已经测试并在第一页视图控制器上成功建立了蓝牙会话。现在我的问题是,如何将当前会话传递到下一页,因为导航到下一页后,蓝牙连接丢失了。因此,在导航到下一页后,当前会话将丢失。通过发送带有两部手机的随机图像进行测试。但是,如果第二次在nextpageController上提示蓝牙连接,则可以发送图片。
查看controller.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;
}
答案 0 :(得分:1)
我不知道我是否以正确的方式做到了,但我有一个有很多页面的应用程序,我需要在整个过程中保持这种联系。因此,我将所有蓝牙连接代码放在每个视图控制器可以与之通信的单例类中。这样它就可以处理所有通信,每个页面只能告诉它想要发送的内容。
所以这是一个很好的单例类教程 http://www.galloway.me.uk/tutorials/singleton-classes/
就蓝牙而言,看起来你拥有所有正确的方法。把它们放在你的单身人士课堂上。然后,当您引用类*(MyManager sharedManager = [MyManager sharedManager];)时,只需创建一个sendCommand方法,即可发送您想要的任何通信[sharedManager sendCommand:data];
所以你有蓝牙课程。你可以在其中添加一个类似这个代码的bluetoothconnect方法
picker = [[GKPeerPickerController alloc] init];
picker.delegate = self;
picker.connectionTypesMask = GKPeerPickerConnectionTypeNearby;
[picker show];
然后从您的视图控制器类中引用您的单例类,类似于教程所示的内容。
MyManager sharedManager = [MyManager sharedManager];
然后你可以调用你的新方法:[sharedManager bluetoothconnect];