728x90
iphone간 bluetooth 연결 sample
Sameple Code : need GameKit Framework
BluetoothSampleAppDelegate.h
BluetoothSampleAppDelegate.m
Sameple Code : need GameKit Framework
BluetoothSampleAppDelegate.h
//
// 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.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
//
// 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
// 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
728x90
'BlueTooth > 기본기' 카테고리의 다른 글
quoted-printable decoder (0) | 2013.06.03 |
---|---|
synergy MessageSendLater (0) | 2011.04.19 |
bluelab stereo 2009.R2 Inquiry시 iPhone이 검색되면 panic (0) | 2011.03.30 |
Apple 개발문서 (4) | 2010.03.15 |
Peer-to-Peer Connectivity (0) | 2010.03.04 |
아이폰 블루투스 프로그래밍 (3) | 2010.03.03 |
bluetooth keypad (0) | 2010.02.10 |
Bluetooth 2.0 under에서 Pincode없이 연결 (0) | 2009.12.28 |
bluetooth hfp 정리 (0) | 2009.07.24 |
BlueTooth 관련용어 (0) | 2009.07.20 |