每次我在安装应用程序后第一次尝试通过蓝牙连接设备时,连接需要花费太多时间,或者有时连接被拒绝。
这是我的代码:
(void)setUpSession {
@try {
if(mTimeOutTimer) {
[mTimeOutTimer invalidate];
mTimeOutTimer = nil;
}
self.mConnectionState = ConnectionStateDisconnected;
if(!mPeerPickerController) {
mPeerPickerController = [[GKPeerPickerController alloc] init];
mPeerPickerController.delegate = self;
mPeerPickerController.connectionTypesMask = GKPeerPickerConnectionTypeNearby;
}
//Present Peer Picker.
[mPeerPickerController show];
}
@catch (NSException *exception) {
NSLog(@"Expception Occured at path -> BlueToothManager/setUpSession");
NSLog(@"Exception Name -> %@",[exception name]);
NSLog(@"Exception Reason -> %@",[exception reason]);
}
}
(GKSession *)peerPickerController:(GKPeerPickerController *)picker sessionForConnectionType:(GKPeerPickerConnectionType)type {
NSString *deviceName = [[UIDevice currentDevice] name];
GKSession* gkSession = [[GKSession alloc] initWithSessionID:@"BluetoothStreamingVideo" displayName:deviceName sessionMode:GKSessionModePeer];
gkSession.delegate = self;
if(self.mGameKitSession) {
[self.mGameKitSession disconnectFromAllPeers];
if([self.mGameKitSession isAvailable]) {
[self.mGameKitSession setAvailable:FALSE];
}
[self.mGameKitSession setDataReceiveHandler:nil withContext:nil];
self.mGameKitSession = nil;
}
self.mGameKitSession = gkSession;
return [gkSession autorelease];
}
但是一旦连接设置好,下次连接设置好,持续时间不长。