http://www.iphone3.0sdk.com/blog/any-sample-code-bluetooth-communication Any Sample code for bluetooth communication Wed, 04/01/2009 - 17:09 \ Basavaraj Hello, Any one have sample code for bluetooth communication in iPhone sdk 3.0. If anybody have it, please share with us. Thanks Basavaraj Average: 0 Your rating: None * Basavaraj's blog Comments Fri, 06/12/2009 - 20:19 \ Anonymous (not verified) sample any samples that are not the single sample all over the internet for p2p? like phone to peripheral device? * reply Tue, 04/07/2009 - 12:04 \ Jose Hi, Here is the sample code for p2p connection, Please correct me if I am wrong. / BluetoothSampleAppDelegate.h // BluetoothSample // #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 // #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 * reply Tue, 04/07/2009 - 12:02 \ Jose Hi, Here is the sample code for p2p connection, Please correct me if I am wrong. / BluetoothSampleAppDelegate.h // BluetoothSample // #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 // #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