2013-06-12 3 views
0

Gamekit을 사용하여 두 장치를 연결하고 소량의 데이터를 전송하려고합니다. GKPickerController를 사용하여이 작업을 수행 할 수 있었지만 이제 사용자 경험을 조금 더 향상시키기 위해이 작업을 자동으로 수행하려고합니다. 문제는 델리게이트 방법이 호출되지 않는다는 것입니다.GKPickercontroller를 사용하지 않고 두 장치를 연결할 수 없습니다.

- (void)session:(GKSession *)session peer:(NSString *)peerID didChangeState:(GKPeerConnectionState)state 

이 호출되지 않습니다.

두 가지 앱이 있습니다.

찾아주세요 아래 코드 :

iPad app 

AppDelegate.h 

@interface AppDelegate : UIResponder <UIApplicationDelegate,GKSessionDelegate> 

@property (strong, nonatomic) UIWindow *window; 
@property (nonatomic, retain) GKSession *connectSession; 
@end 

AppDelegate.m 

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{ 
    _peerDevice = [[NSMutableArray alloc]init]; 

    _connectSession = [[GKSession alloc]initWithSessionID:nil displayName:nil sessionMode:GKSessionModePeer]; 
    _connectSession.delegate = self; 
    _connectSession.available = YES; 
    _connectSession.disconnectTimeout = 0; 
    [_connectSession setDataReceiveHandler:self withContext:nil]; 

    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; 
    // Override point for customization after application launch. 
    self.viewController = [[xyz alloc] initWithNibName:@"xyz" bundle:nil]; 
    self.window.rootViewController = self.viewController; 
    [self.window makeKeyAndVisible]; 
    return YES; 
} 

- (void)session:(GKSession *)session peer:(NSString *)peerID didChangeState:(GKPeerConnectionState)state{ 


    if(state == GKPeerStateConnected){ 
     // Add the peer to the Array 
     [_peerDevice addObject:peerID]; 

     NSString *str = [NSString stringWithFormat:@"Connected with %@",[session displayNameForPeer:peerID]]; 
     UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Connected" message:str delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil]; 
     [alert show]; 

     // Used to acknowledge that we will be sending data 
     [session setDataReceiveHandler:self withContext:nil]; 

     [[self.window viewWithTag:12] removeFromSuperview]; 

    } 

} 

- (void)session:(GKSession *)session didReceiveConnectionRequestFromPeer:(NSString *)peerID 
{ 
    NSLog(@"didReceiveConnectionRequestFromPeer: %@", [session displayNameForPeer:peerID]); 

    [session acceptConnectionFromPeer:peerID error:nil]; 

} 

iPhone app 

AppDelegate.h 

@interface AppDelegate : UIResponder <UIApplicationDelegate, GKSessionDelegate,UIAlertViewDelegate> 
@property (nonatomic, strong) GKSession *connectSession; 
@property (strong, nonatomic) UIWindow *window; 

@end 

AppDelegate.m 

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{ 
    _peerDevice = [[NSMutableArray alloc]init]; 

    _connectSession = [[GKSession alloc]initWithSessionID:nil displayName:nil sessionMode:GKSessionModePeer]; 
    _connectSession.delegate = self; 
    _connectSession.available = YES; 
    _connectSession.disconnectTimeout = 0; 

    [_connectSession setDataReceiveHandler:self withContext:nil]; 

    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; 
    _nav = [[UINavigationController alloc]initWithNavigationBarClass:[CustomNavigationBar class] toolbarClass:nil]; 
    self.viewController = [[HCAViewController alloc] initWithNibName:@"HCAViewController" bundle:nil]; 
    [_nav pushViewController:self.viewController animated:YES]; 
    self.window.rootViewController = _nav; 
    [self.window makeKeyAndVisible]; 
    return YES; 
} 

- (void)session:(GKSession *)session peer:(NSString *)peerID didChangeState:(GKPeerConnectionState)state{ 

    _connectSession = session; 
    NSString * str; 
    UIAlertView * alert; 

    switch (state) { 

     case GKPeerStateAvailable: 
      [session connectToPeer:peerID withTimeout:0]; 
      break; 

     case GKPeerStateConnected: 
      [_peerDevice addObject:peerID]; 

      str = [NSString stringWithFormat:@"Connected with SFMedical"]; 
      alert = [[UIAlertView alloc] initWithTitle:@"Connected" message:str delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil]; 
      [alert show]; 

      // Used to acknowledge that we will be sending data 
      [session setDataReceiveHandler:self withContext:nil]; 

      [[self.window viewWithTag:12] removeFromSuperview]; 
      break; 

     default: 
      break; 
    } 


} 

- (void)session:(GKSession *)session didReceiveConnectionRequestFromPeer:(NSString *)peerID 
{ 
    NSLog(@"didReceiveConnectionRequestFromPeer: %@", [session displayNameForPeer:peerID]); 

    [session acceptConnectionFromPeer:peerID error:nil]; 

} 
+0

안녕하세요. 당신이 달성 한 정확한 해결책을 찾고 있습니다. 어떻게 실현했는지 설명해 주시겠습니까? 당신이 [여기] (http://stackoverflow.com/questions/18789186/establish-automatic-bluetooth-connection-while-coming-back-from-the-home-screen)을 볼 수 있다면, – GoCrazy

답변

0

Alright..figured이를. 세션 ID없이 작동하지 않는 몇 가지 이유로 동일한 애플 리케이션 ID를 두 애플 리케이션에 넣고 작동했습니다.