Bluetooth

Bluetooth : Communication between devices

In iPhone SDK, it is possible to connect two iOS devices and to share data between them using bluetooth programmatically. There are options to do that.
(1) Bump API
(2) Bonjour networking
(3) GameKit framework(iOS 3.0 and later)

Here, we will learn how to use GameKit framework.
GameKit, the name suggests that it has been made for games but it can be used for other purposes too. It provides peer-to-peer connectivity between iOS devices. Everything is set up for you and you can send data between two devices in a few lines of code.
Let’s take a look how.
First create a project named Bluetooth. Now,you need to import the GameKit framework to your project.
In the ViewController.h file, import GameKit framework and declare the following objects and actions,

#import
#import

@interface ViewController : UIViewController
{
GKSession *currentSession;
GKPeerPickerController *pickerController;
IBOutlet UIButton *connectButton;
IBOutlet UIButton *disconnectButton;
}

-(IBAction)connectButtonClicked:(id)sender;
-(IBAction)disconnectButtonClicked:(id)sender;

The GKSession object is used to represent a session between two connected Bluetooth devices. It will be used when you will send and receive data between the two devices.
The GKPeerPickerController class provides a standard UI to let application discover, select and connect to another Bluetooth device.

Add following code in the ViewController.m file.

- (void)viewDidLoad
{
disconnectButton.hidden=TRUE;
[super viewDidLoad];
}

-(IBAction)connectButtonClicked:(id)sender
{
connectButton.hidden=TRUE;
disconnectButton.hidden=FALSE;

pickerController=[[GKPeerPickerController alloc] init];
pickerController.delegate=self;
pickerController.connectionTypesMask=GKPeerPickerConnectionTypeNearby;
[pickerController show];
}

There are two types connection types:
GKPeerPickerConnectionTypeNearby : Use for Bluetooth communication. GKPeerPickerConnectionTypeOnline : It indicates an Internet-based connection.

GKPeerPickerController methods:

- (void)peerPickerController:(GKPeerPickerController *)picker
didConnectPeer:(NSString *)peerID
toSession:(GKSession *) session
{
currentSession = session;
session.delegate = self;
[session setDataReceiveHandler:self withContext:nil];
pickerController.delegate = nil;
[pickerController dismiss];
}

- (void)peerPickerControllerDidCancel:(GKPeerPickerController *)picker
{
pickerController.delegate = nil;
connectButton.hidden=FALSE;
disconnectButton.hidden=TRUE;
}

When the you has connected to the peer Bluetooth device, save the GKSession object to the currentSession object. It is used when you will share data with connected device.

Add method for disconnecting devices,

-(IBAction)disconnectButtonClicked:(id)sender
{
connectButton.hidden=FALSE;
disconnectButton.hidden=TRUE;
[ currentSession disconnectFromAllPeers];
currentSession=nil;
}

We have completed one part.
Now sending and receiving data between connected devices:

Add following action in the ViewController.h file.

-(IBAction) sendButtonClicked:(id) sender;

Add following code in ViewController.m file.

-(IBAction) sendButtonClicked:(id) sender
{
//— NSString object to NSData—
NSData* data;
NSString *string = [NSString stringWithString:@"Data from iPhone via bluetooth"];
data = [string dataUsingEncoding: NSASCIIStringEncoding];

if (currentSession)
[currentSession sendDataToAllPeers:data
withDataMode:GKSendDataReliable
error:nil];
}

- (void) receiveData:(NSData *)data fromPeer:(NSString *)peer inSession:(GKSession *)session
context:(void *)context
{
//—NSData to NSString—
NSString* string;
string = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@”Data received” message:string
delegate:self cancelButtonTitle:@”OK” otherButtonTitles:nil];
[alert show];

}

SendDatatoAllPeers method of GKSession is used for sending data to peer device.

Testing application:

You are now ready to test the application. For testing this application, I assume you have two devices or simulator and a device.
Once the application is deployed to the two devices, launch the application on both devices. On each device, tap the Connect button. The GKPeerPickerController will display the standard UI to discover other devices. Once you discovered a device, connect to it and share data between them.

Tags: , , , , , ,

Comments are closed.