• Posted on March 27, 2012
  • Category Misc

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,


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


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
[super viewDidLoad];


pickerController=[[GKPeerPickerController alloc] init];
[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;

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,

[ currentSession disconnectFromAllPeers];

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

– (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.

(Visited 6 times, 1 visits today)

Related Posts:

Author - admin_specindia



  • 2018
  • 2017
  • 2016
  • 2015
  • 2014
  • 2013
  • 2012
  • 2011
  • 2010
  • 2009
  • 2008
  • Related Blogs

    Current Openings
    Scroll Up