GKSession - 如果我关闭了蓝牙和Wi-Fi怎么办?

bpa*_*apa 11 iphone p2p bluetooth wifi gksession

我正在开发一个允许点对点连接的iPhone应用程序.据我所知,我可以选择使用GKPeerPicker还是GKSession.我不喜欢使用PeerPicker的想法,因为我想显示一个自定义界面,所以我决定使用GKSession,嘿,BONUS是它也适用于Wi-Fi,而Peer Picker则没有.

好的,问题是......如果用户同时关闭蓝牙和Wi-Fi怎么办?在Peer Picker中,有一个提示在没有离开应用程序的情况下打开蓝牙.GKSession没有它......但是等待一秒钟,似乎我甚至无法检查蓝牙是否打开了!

Carpe Cocoa声称没问题,只需使用Delegate的session:didFailWithError:方法.但是,正如它在评论中所解释的那样......似乎不再起作用了!根据我的经验,我同意.

是否有其他方法以编程方式检查蓝牙是否已打开?这是我应该利用Reachability的东西吗?或者它只是苹果尚未修复的错误?

更具体地说,我正在创建我的会话:

GKSession *aSession = [[GKSession alloc] initWithSessionID:nil
                                                  displayName:user.displayName 
                                                  sessionMode:GKSessionModePeer];

self.gkSession = aSession;
[aSession release];

self.gkSession.delegate = self;
self.gkSession.available = YES;

[self.gkSession setDataReceiveHandler:self withContext:NULL];
Run Code Online (Sandbox Code Playgroud)

该类实现了GKSessionDelegate,我知道它正在工作,因为当我打开蓝牙时,委托方法被称为没问题.我已经实现了它们:

#pragma mark -
#pragma mark GKSessionDelegate methods

- (void)session:(GKSession *)session peer:(NSString *)peerID didChangeState:(GKPeerConnectionState)state {
     if (GKPeerStateAvailable == state) {
          [session connectToPeer:peerID withTimeout:10];
     } else if (GKPeerStateConnected == state) {
          // gets user

          NSError *error = nil;
          [session sendData:user.connectionData
                      toPeers:[NSArray arrayWithObjects:peerID,nil]
                withDataMode:GKSendDataReliable error:&error];
          if (error)
               NSLog(@"%@",error);
     }
}



- (void)session:(GKSession *)session didReceiveConnectionRequestFromPeer:(NSString *)peerID {
     NSError *error = nil;
     [session acceptConnectionFromPeer:peerID error:&error];
     if (error)
          NSLog(@"%@",error);
}

- (void)session:(GKSession *)session connectionWithPeerFailed:(NSString *)peerID withError:(NSError *)error {
     NSLog(@"%@",error);
}

- (void)session:(GKSession *)session didFailWithError:(NSError *)error {
     NSLog(@"%@",error);
}
Run Code Online (Sandbox Code Playgroud)

没有打印任何日志语句,我在每种方法中都设置了断点,但是当用户同时关闭了蓝牙和Wi-Fi时,没有一个断点.我希望触发会话会发生一些事情:didFailWithError:这样我就可以提示用户打开蓝牙或连接到Wi-Fi网络.

slf*_*slf 1

我同意 Martin Gordon 的观点,但解决方法可能是使用Apple 的可达性