@implementation PTPAppDelegate @synthesize window; - (void)applicationDidFinishLaunching:(UIApplication *)application { [window makeKeyAndVisible]; _session = [[GKSession alloc] initWithSessionID:nil displayName:nilsessionMode:GKSessionModePeer]; _session.delegate = self; GKPeerPickerController* controller = [GKPeerPickerController new]; controller.delegate = self; controller.connectionTypesMask = GKPeerPickerConnectionTypeNearby; [controller show]; } - (void)dealloc { [_session release]; [window release]; [super dealloc]; } #pragma mark - #pragma mark *** GKPeerPickerControllerDelegate *** - (void)peerPickerController:(GKPeerPickerController *)picker didSelectConnectionType:(GKPeerPickerConnectionType)type { NSLog(@"%@", NSStringFromSelector(_cmd)); } - (GKSession *)peerPickerController:(GKPeerPickerController *)picker sessionForConnectionType:(GKPeerPickerConnectionType)type { if (type == GKPeerPickerConnectionTypeNearby) return _session; returnnil; } - (void)peerPickerController:(GKPeerPickerController *)picker didConnectToPeer:(NSString *)peerID { NSLog(@"%@", NSStringFromSelector(_cmd)); } - (void)peerPickerControllerDidCancel:(GKPeerPickerController *)picker { NSLog(@"%@", NSStringFromSelector(_cmd)); } #pragma mark - #pragma mark *** GKSessionDelegate *** - (void)session:(GKSession *)session peer:(NSString *)peerID didChangeState:(GKPeerConnectionState)state { NSLog(@"%@", NSStringFromSelector(_cmd)); } - (void)session:(GKSession *)session didReceiveConnectionRequestFromPeer:(NSString *)peerID { NSLog(@"%@", NSStringFromSelector(_cmd)); } - (void)session:(GKSession *)session connectionWithPeerFailed:(NSString *)peerID withError:(NSError *)error { NSLog(@"%@", NSStringFromSelector(_cmd)); } - (void)session:(GKSession *)session didFailWithError:(NSError *)error { NSLog(@"%@", NSStringFromSelector(_cmd)); } @end - (void)applicationDidFinishLaunching:(UIApplication *)application { // Override point for customization after application launch [window makeKeyAndVisible]; picker = [[GKPeerPickerControlleralloc] init]; picker.delegate = self; picker.connectionTypesMask = GKPeerPickerConnectionTypeNearby | GKPeerPickerConnectionTypeOnline; [picker show]; } - (GKSession *)peerPickerController:(GKPeerPickerController *)picker sessionForConnectionType:(GKPeerPickerConnectionType)type { if (!session) { session = [[GKSession alloc] initWithSessionID:@"BluetoothTest" displayName :@"test"sessionMode:GKSessionModePeer]; session.delegate = self; } return session; } - (void)session:(GKSession *)thissession peer:(NSString *)peerID didChangeState:(GKPeerConnectionState)state { switch (state) { case GKPeerStateConnected: [thissession setDataReceiveHandler: self withContext: nil]; // record the peerID of the other peer. // inform your game that a peer has connected NSString *msg = @"Hi Friend"; NSData *data = [msg dataUsingEncoding: NSASCIIStringEncoding];// create your data packet here. [thissession sendDataToAllPeers: data withDataMode: GKSendDataReliable error: nil]; break; case GKPeerStateDisconnected: // inform your game that a peer has left. break; } } - (void) receiveData:(NSData *)data fromPeer:(NSString *)peer inSession: (GKSession *)session context:(void *)context { // Read the bytes in data and perform an application-specific action NSString* aStr; aStr = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding]; UIAlertView *alert = [[UIAlertView alloc] initWithTitle :aStr message :nildelegate:nilcancelButtonTitle:@"OK"otherButtonTitles:nil]; [alert show]; } // // BluetoothSampleAppDelegate.h // BluetoothSample // // Created by Travis True on 3/18/09. // Copyright University of Nevada Las Vegas 2009. All rights reserved. // #import "EAGLView.h" #import @interface BluetoothSampleAppDelegate : NSObject { UIWindow *window; EAGLView *glView; GKPeerPickerController *picker; GKSession *session; int myNumber; NSData *myData; UILabel *textView; } @property (nonatomic, retain) IBOutlet UIWindow *window; @property (nonatomic, retain) IBOutlet EAGLView *glView; @property (nonatomic, retain) GKPeerPickerController *picker; @property (nonatomic, retain) GKSession *session; - (void)mySendData; @end // // BluetoothSampleAppDelegate.m // BluetoothSample // // Created by Travis True on 3/18/09. // Copyright University of Nevada Las Vegas 2009. All rights reserved. // #import "BluetoothSampleAppDelegate.h" @implementation BluetoothSampleAppDelegate @synthesizepicker; @synthesizesession; @synthesizewindow; @synthesizeglView; - (void)applicationDidFinishLaunching:(UIApplication *)application { // setup the text view myNumber = 0; textView = [[UILabel alloc] initWithFrame:CGRectMake(10.0f, 10.0f, 640.0f, 12.0f)]; textView.text = [NSString stringWithFormat:@"myNumber: %i\n", myNumber]; [window addSubview:textView]; [window bringSubviewToFront:textView]; // start the EAGLView glView.animationInterval = 1.0 / 60.0; [glView startAnimation]; // allocate the NSData myData = [[NSData alloc] initWithBytes:&myNumber length:sizeof(int)]; // allocate and setup the peer picker controller picker = [[GKPeerPickerControlleralloc] init]; picker.delegate = self; picker.connectionTypesMask = GKPeerPickerConnectionTypeNearby | GKPeerPickerConnectionTypeOnline; [picker show]; } - (void)applicationWillResignActive:(UIApplication *)application { glView.animationInterval = 1.0 / 5.0; } - (void)applicationDidBecomeActive:(UIApplication *)application { glView.animationInterval = 1.0 / 60.0; } - (void)peerPickerController:(GKPeerPickerController *)picker didSelectConnectionType:(GKPeerPickerConnectionType)type { if(type == GKPeerPickerConnectionTypeOnline) { [self.picker dismiss]; [self.picker release]; self.picker = nil; // Display your own UI here. } } - (GKSession *) peerPickerController:(GKPeerPickerController *)picker sessionForConnectionType:(GKPeerPickerConnectionType)type { session = [[GKSession alloc] initWithSessionID:@"FR" displayName:nilsessionMode:GKSessionModePeer]; session.delegate = self; return session; } - (void)session:(GKSession *)session peer:(NSString *)peerID didChangeState:(GKPeerConnectionState)state { switch (state) { case GKPeerStateConnected: [self.session setDataReceiveHandler :selfwithContext:nil]; [self mySendData]; // start off by sending data upon connection break; case GKPeerStateDisconnected: break; } } - (void)peerPickerController:(GKPeerPickerController *)picker didConnectToPeer:(NSString *)peerID { printf("connection was successful! start the game.\n"); } - (void)peerPickerControllerDidCancel:(GKPeerPickerController *)picker { printf("connection attempt was canceled\n"); } - (void)mySendData { // allocate the NSData myNumber++; myData = [[NSData alloc] initWithBytes:&myNumber length:sizeof(int)]; [session sendDataToAllPeers :myDatawithDataMode:GKSendDataReliable error:nil]; printf("send data: %i\n", myNumber); textView.text = [NSString stringWithFormat:@"myNumber: %i\n", myNumber]; } http://macgeeks.cn/bbs/viewtopic.php?id=7582 - (void) receiveData:(NSData *)data fromPeer:(NSString *)peer inSession: (GKSession *)session context:(void *)context { // Read the bytes in data and perform an application-specific action, then free the NSData object [data getBytes:&myNumber length:sizeof(int)]; printf("received data: %i from: %s\n", myNumber, [peer UTF8String]); textView.text = [NSString stringWithFormat:@"myNumber: %i\n", myNumber]; [self mySendData]; } - (void)dealloc { [picker release]; [session release]; [textView release]; [glView release]; [window release]; [super dealloc]; } @end