Welcome to YLOAN.COM
yloan.com » iPhone 3 » Bluetooth With Iphones - Iphone Bluetooth Compatibility
iPhone 5 iPhone 4 iPhone 3 iPad iMac iPod Nano iPod Touch i-accessories iapplications

Bluetooth With Iphones - Iphone Bluetooth Compatibility

iPhone application will allow us to do Communication between devices using Bluetooth technology.


In iPhone SDK, it is possible to connect two iOS devices and to share data between them using bluetooth programmatically. There are following options available

GameKit framework(iOS 3.0 and later)

Bump API


Bonjour networking

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.

iPhone Canada, learn how can share data between two devices using bluetooth in Game kit.

1.First create a project named Bluetooth. Now, you need to import the GameKit framework to your project.

2.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.

3.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,

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;

}

4.When 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 establish the connection between devices. Now sending and receiving data between connected devices:

5.Add following action in the ViewController.h 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.

6.You are now ready to test the application. For testing this application, we at iPhone USassume 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.

by: Spec India
Iphone Application Development,iphone App Development,iphone Applications Development Houston,texas Block Phone Number On Iphone Iphone Application Development Chain Of New Tools With Unique And Customized Features Iphone Parts Repair And Replacement Service Iphone Repair And Replacement Services Iphone App Developers Introduce Passbook App A Digital Wallet Wondershare Video Converter Ultimate 6.0 Adds Support For Iphone 5 And Ipod Touch 5 Iphone Application Development For Business & Pleasure Cheapest Iphone 5 Deals-all Hail To The Almighty! Apple Iphone 4s: Brings Technology To Your Hands How Iphone Apps Can Enhance The Business? New York,new York Era Of Mobile Apps Development Android Application Development, Iphone Application Development Are Iphone App Developers Aware About The New Iphones Call Blocking Feature?
print
www.yloan.com guest:  register | login | search IP(216.73.216.19) California / Anaheim Processed in 0.027329 second(s), 7 queries , Gzip enabled , discuz 5.5 through PHP 8.3.9 , debug code: 180 , 5377, 15,
Bluetooth With Iphones - Iphone Bluetooth Compatibility Anaheim