Tuesday, July 17, 2012

Bluetooth Programming

Bluetooth Programming:

The iPhone comes with built-in Bluetooth functionality, enabling it to communicate with other
Bluetooth devices, such as Bluetooth headsets, iPhone, iPod touch, and iPad. This blogs shows you how to write iPhone applications that use Bluetooth to communicate with another
device, performing tasks such as sending and receiving text messages, as well as voice chatting.
Daunting as it may sound, Bluetooth programming is actually quite simple using the iPhone
SDK. All the Bluetooth functionalists are encapsulated within the Game Kit framework.

One of the neat features available in the iPhone SDK is the Game Kit framework, which contains
APIs that enable communications over a Bluetooth network. You can use these APIs to
create peer-to-peer games and applications with ease. Unlike other mobile platforms, using.
Bluetooth as a communication channel in the iPhone is much easier than you might expect. In this
section, you will learn how to build a simple application that enables two iPhone (or iPad and iPod
touch) devices to communicate with each other.

Searching for Peer Devices:

Before any exchanges of data can take place, the first step to Bluetooth communication is for the
devices to locate each other. The following Try It Out shows you how to use the Game Kit framework
to locate your Bluetooth peer.

1. Using Xcode, create a new View-based Application project and name it Bluetooth.
2. Add the GameKit.framework to your project (see Figure B-1).
 
Image-B-1
3. Double-click BluetoothViewController.xib to edit it in Interface Builder. As shown in
Figure B-2, add the following views to the View window:

➤➤ Round Rect buttons (name them Connect, Disconnect, and Send)
➤➤ Label (name it “Enter message here”)
➤➤ Text Field

 
Image-B-2

4. In the BluetoothViewController.h file, add the following statements shown in bold:
#import <UIKit/UIKit.h>
#import <GameKit/GameKit.h>
@interface BluetoothViewController : UIViewController
<GKSessionDelegate,
GKPeerPickerControllerDelegate> {
GKSession *currentSession;
GKPeerPickerController *picker;
IBOutlet UITextField *txtMessage;
IBOutlet UIButton *connect;
IBOutlet UIButton *disconnect;
}
@property (nonatomic, retain) GKSession *currentSession ;
@property (nonatomic, retain) UITextField *txtMessage;
@property (nonatomic, retain) UIButton *connect;
@property (nonatomic, retain) UIButton *disconnect;
-(IBAction) btnSend:(id) sender;
-(IBAction) btnConnect:(id) sender;
-(IBAction) btnDisconnect:(id) sender;

5. Back in Interface Builder, perform the following actions:
➤➤ Control-click the File’s Owner item and drag and drop it over the Text Field view. Select
txtMessage.
➤➤ Control-click the File’s Owner item and drag and drop it over the Connect button. Select
connect.
➤➤ Control-click the File’s Owner item and drag
and drop it over the Disconnect button. Select
disconnect.
➤➤ Control-click the Send button and drag and
drop it over the File’s Owner item. Select
btnSend:.
➤➤ Control-click the Connect button and drag
and drop it over the File’s Owner item. Select
btnConnect:.
➤➤ Control-click the Disconnect button and drag
and drop it over the File’s Owner item. Select
btnDisconnect
Image-B-3
6. Right-click on the File’s Owner item to verify that
all the connections are made correctly (see Figure B-3).

7. In the BluetoothViewController.m file, add the following statements in bold: 

#import “BluetoothViewController.h”
@implementation BluetoothViewController
@synthesize currentSession;
@synthesize txtMessage;
@synthesize connect;
@synthesize disconnect;
- (void)viewDidLoad {
[connect setHidden:NO];
[disconnect setHidden:YES];
[super viewDidLoad];
}
-(IBAction) btnConnect:(id) sender {
picker = [[GKPeerPickerController alloc] init];
picker.delegate = self;
picker.connectionTypesMask = GKPeerPickerConnectionTypeNearby;
[connect setHidden:YES];
[disconnect setHidden:NO];
[picker show];
}
//---did connect to a peer---
-(void)peerPickerController:(GKPeerPickerController *)pk
didConnectPeer:(NSString *)peerID
toSession:(GKSession *)session {
self.currentSession = session;
session.delegate = self;
[session setDataReceiveHandler:self withContext:nil];
picker.delegate = nil;
[picker dismiss];
[picker autorelease];
}
-(void)peerPickerControllerDidCancel:(GKPeerPickerController *)pk {
picker.delegate = nil;
[picker autorelease];
[connect setHidden:NO];
[disconnect setHidden:YES];
}
//---connection was cancelled---
-(IBAction) btnDisconnect:(id) sender {
[self.currentSession disconnectFromAllPeers];
[self.currentSession release];
currentSession = nil;
[connect setHidden:NO];
[disconnect setHidden:YES];
}
//---session state changed---
-(void)session:(GKSession *)session
peer:(NSString *)peerID
didChangeState:(GKPeerConnectionState)state {
switch (state) {
case GKPeerStateConnected:
NSLog(@”connected”);
break;
case GKPeerStateDisconnected:
NSLog(@”disconnected”);
[self.currentSession release];
currentSession = nil;
[connect setHidden:NO];
[disconnect setHidden:YES];
break;
}
}
//---session failed with error---
-(void)session:(GKSession *)session
didFailWithError:(NSError *)error {
NSLog(@”%@”,[error description]);
}
- (void)dealloc {
[txtMessage release];
[currentSession release];
[connect release];
[disconnect release];
[super dealloc];
}
8. Deploy the application onto two devices(iPhone, ipad, ipod).
9. Once the application is deploy on two devices launch the application on both devices if Bluetooth is not turn on you will be asked to turn on(see figure B-4). Tap the connect Button on each devices.you will see the standard UI to dis cover the other devices.(see figure B-5).

 
B-4 and B-5

10. After a few seconds, both devices should be able to find each other (see Figure 17-6). Tap the name
of the found device and the application will attempt to connect to it.



No comments:

Post a Comment