What you need to do is to create a new project from OpenGL ES Application of iPhone OS 3.0 SDK template called it BluetoothSample
and past the codes as below and test, it should work on 2 iPhone 3G and iPod Touch 2nd gen only.
Then you need to add GameKit framework to build and go
The source is from Travis True
- BluetoothSampleAppDelegate.h Select all
 //
 // BluetoothSampleAppDelegate.h
 // BluetoothSample
 //
 #import "EAGLView.h"
 #import <GameKit/GameKit.h>
 @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 Select all
 //
 // BluetoothSampleAppDelegate.m
 // BluetoothSample
 //
 #import "BluetoothSampleAppDelegate.h"
 @implementation BluetoothSampleAppDelegate
 @synthesize picker;
 @synthesize session;
 @synthesize window;
 @synthesize glView;
 - (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 = [[GKPeerPickerController alloc] 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:nil sessionMode:GKSessionModePeer];
 session.delegate = self;
 
 return session;
 }
 - (void)session:(GKSession *)session peer:(NSString *)peerID didChangeState:(GKPeerConnectionState)state {
 switch (state) {
 case GKPeerStateConnected:
 [self.session setDataReceiveHandler :self withContext: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 :myData withDataMode:GKSendDataReliable error:nil];
 printf("send data: %i\n", myNumber);
 textView.text = [NSString stringWithFormat:@"myNumber: %i\n", myNumber];
 }
 - (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
 
 


