使用GameKit实现IOS设备之间的蓝牙通信

  该工程是使用苹果官方的GameKit框架来实现蓝牙设别之间的通信,首先当然是要在项目中加入GameKit.framework框架。工程的截图如下:

ViewController.h如下:

//
// ViewController.h
// GameKitDemo
//
// Created by Fox on 12-3-14.
// Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import <UIKit/UIKit.h>
#import <GameKit/GKSession.h>
#import <GameKit/GKPeerPickerController.h>

@interface ViewController : UIViewController<GKSessionDelegate,GKPeerPickerControllerDelegate>{

GKSession *currentSession;//GKSession对象用于表现两个蓝牙设备之间连接的一个会话,你也可以使用它在两个设备之间发送和接收数据。

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;
-(void)mySendDataToPeers:(NSData *)data;

@end

ViewController.m如下:

//
// ViewController.m
// GameKitDemo
//
// Created by Fox on 12-3-14.
// Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "ViewController.h"
#import <GameKit/GKPeerPickerController.h>

@implementation ViewController
@synthesize currentSession;
@synthesize connect;
@synthesize disconnect;
@synthesize txtMessage;

GKPeerPickerController *picker;//GKPeerPickerController类提供了一个标准的UI使的应用程序发现并连接到其它蓝牙设备

- (void)viewDidLoad
{
[connect setHidden:NO];
[disconnect setHidden:YES];
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}

- (void)dealloc {
[txtMessage release];
[currentSession release];

[super dealloc];

}

/*
*建立回话后,开始发送信息
*/
-(IBAction) btnSend:(id) sender{
NSData* data;
NSString *str = [NSString stringWithString:txtMessage.text];
data = [str dataUsingEncoding: NSASCIIStringEncoding];
[self mySendDataToPeers:data];
}

/*
*利用GKPeerPickerController框架搜索附近的蓝牙设备
*/
-(IBAction) btnConnect:(id) sender{

picker= [[GKPeerPickerController alloc] init];
picker.delegate = self;
picker.connectionTypesMask = GKPeerPickerConnectionTypeNearby;
//connectionTypesMask用户可以选择的连接类型,包括两种类型:GKPeerPickerConnectionTypeNearby和GKPeerPickerConnectionTypeOnline。对于蓝牙通信,使用GKPeerPickerConnectionTypeNearby常量,GKPeerPickerConnectionTypeOnline常量表示基于互联网的连接。
[connect setHidden:YES];
[disconnect setHidden:NO];
[picker show];

}


/*
*从一个Apple设备中断开连接
*/
-(IBAction) btnDisconnect:(id) sender{
[self.currentSession disconnectFromAllPeers];
[self.currentSession release];
currentSession = nil;
[connect setHidden:NO];
[disconnect setHidden:YES];

}


/*
*检测到远程蓝牙设备,用户选择并连接到其中一个蓝牙设备时调用该函数
*/
- (void)peerPickerController:(GKPeerPickerController *)picker didConnectPeer:(NSString *)peerID toSession:(GKSession *) session {

self.currentSession = session;//将蓝牙连接的回话session保存为当前的session
session.delegate = self;
[session setDataReceiveHandler:self withContext:nil];
picker.delegate = nil;
[picker dismiss];
[picker autorelease];
}


/*
*点击取消时调用该函数
*/
- (void)peerPickerControllerDidCancel:(GKPeerPickerController *)picker{

picker.delegate = nil;
[picker autorelease];
[connect setHidden:NO];
[disconnect setHidden:YES];
}

/*
*设备连接或断开连接时调用该方法
*/
- (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;
default:
break;
}
}

/*
*向连同的一端发送数据
*/
-(void)mySendDataToPeers:(NSData *) data{
if (currentSession)
[self.currentSession sendDataToAllPeers:data withDataMode:GKSendDataReliable error:nil];
}


/*
*接受发送过来的数据
*/
-(void)receiveData:(NSData *)data fromPeer:(NSString *)peer inSession:(GKSession *)session context:(void *)context {

NSString* str;
str = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Data received"
message:str
delegate:self
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
[alert release];
}




@end

界面如下:

点击连接后,设备开始搜索附近的IOS蓝牙设备:

双方设备搜索并连接成功后,就可以通过输入框来发送消息了。

GameKit主要用于点对点的通信,常用于游戏中。所以向使用这中方法来实现IOS设备的蓝牙同其他非IOS蓝牙设备之间

的通信是比较困难的。
参考来源:http://www.cnblogs.com/KidReborn/archive/2011/10/17/2215641.html

posted @ 2012-03-15 14:27  FoxBabe  阅读(5581)  评论(0编辑  收藏  举报