最近看了一下关于手机蓝牙的相关东西,涉及到了GameKit这个东西,不知道干嘛用的,一眼望过去以为是游戏的东西,不过看过文档后,其实也可以用在游戏中的。不过这里主要是蓝牙这方面的
关于聊天,其实就是就几个过程1,连接;2,传输数据和处理数据
关于这个过程其实我们可以直接自己写一个辅助类来解决就好了
我这边摘录了以为大牛的帮助类
GameKitHelper.h
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
|
#import <Foundation/Foundation.h> #import <UIKit/UIKit.h> #import <GameKit/GameKit.h>
#define DO_DATA_CALLBACK(X, Y) if (self.dataDelegate && [self.dataDelegate respondsToSelector:@selector(X)]) [self.dataDelegate performSelector:@selector(X) withObject:Y]; #define showAlert(format, ...) myShowAlert(__LINE__, (char *)__FUNCTION__, format, ##__VA_ARGS__) #define BARBUTTON(TITLE, SELECTOR) [[UIBarButtonItem alloc] initWithTitle:TITLE style:UIBarButtonItemStylePlain target:self action:SELECTOR]
@protocol GameKitHelperDataDelegate <NSObject> @optional -(void) connectionEstablished; -(void) connectionLost; -(void) sentData: (NSString *) errorMessage; -(void) receivedData: (NSData *)data; @end
@interface GameKitHelper : NSObject<GKPeerPickerControllerDelegate, GKSessionDelegate> { NSString *sessionID; id<GameKitHelperDataDelegate> dataDelegate; UIViewController *viewController; GKSession *session; BOOL isConnected; }
@property (retain) id dataDelegate; @property (retain) UIViewController *viewController; @property (retain) NSString *sessionID; @property (retain) GKSession *session; @property (assign) BOOL isConnected;
+(void) connect; +(void) disconnect; +(void) sendData: (NSData *)data; +(void) assignViewController: (UIViewController *) aViewController; +(GameKitHelper *) sharedInstance; @end
|
GameKitHelper.m
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
|
#import "GameKitHelper.h"
@implementation GameKitHelper @synthesize dataDelegate; @synthesize viewController; @synthesize session; @synthesize sessionID; @synthesize isConnected;
void myShowAlert(int line, char *functname, id formatstring, ...) { va_list arglist; if(!formatstring)return; va_start(arglist, formatstring); id outstring = [[NSString alloc] initWithFormat:formatstring arguments:arglist]; va_end(arglist); UIAlertView *av = [[UIAlertView alloc] initWithTitle:outstring message:nil delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil]; [av show]; }
#pragma mark - Shared Instance static GameKitHelper *sharedInstance = nil;
+(GameKitHelper *) sharedInstance { if(!sharedInstance) { sharedInstance = [[self alloc] init]; } return sharedInstance; }
#pragma mark - Data Sharing -(void) sendDataToPeers: (NSData *)data { NSError *error; BOOL didSend = [self.session sendDataToAllPeers:data withDataMode:GKSendDataReliable error:&error]; if(!didSend) { NSLog(@"Error sending data to peers: %@", [error localizedDescription]); } DO_DATA_CALLBACK(sentData:, (didSend ? nil : [error localizedDescription])); }
-(void) receiveData:(NSData *)data fromPeer:(NSString *)peer inSession:(GKSession *)session context:(void *)context { DO_DATA_CALLBACK(receivedData:, data); }
#pragma mark - Connections -(void) startConnection { if(self.isConnected) { GKPeerPickerController *picker = [[GKPeerPickerController alloc] init]; picker.delegate = self; picker.connectionTypesMask = GKPeerPickerConnectionTypeNearby; [picker show]; if(self.viewController) { self.viewController.navigationItem.rightBarButtonItem = nil; } } }
-(void) peerPickerControllerDidCancel:(GKPeerPickerController *)picker { if(self.viewController) { self.viewController.navigationItem.rightBarButtonItem = BARBUTTON(@"Connect", @selector(startConnection)); } }
-(void) peerPickerController:(GKPeerPickerController *)picker didConnectPeer:(NSString *)peerID toSession:(GKSession *)session { [picker dismiss]; [self.session setDataReceiveHandler:self withContext:nil]; isConnected = YES; DO_DATA_CALLBACK(connectionEstablished, nil); }
-(GKSession *) peerPickerController:(GKPeerPickerController *)picker sessionForConnectionType:(GKPeerPickerConnectionType)type { if(!self.session) { self.session = [[GKSession alloc] initWithSessionID:(self.sessionID ? self.sessionID : @"Sample Session") displayName:nil sessionMode:GKSessionModePeer]; self.session.delegate = self; } return self.session; }
#pragma mark - Session Handling -(void) disconnect { [self.session disconnectFromAllPeers]; self.session = nil; }
-(void) session:(GKSession *)session peer:(NSString *)peerID didChangeState:(GKPeerConnectionState)state {
NSArray *states = [NSArray arrayWithObjects:@"Available", @"Unavailable", @"Connected", @"Disconnected", @"Connecting", nil]; NSLog(@"Peer state is now %@",[states objectAtIndex:state]); if(state == GKPeerStateConnected) { if(self.viewController) { self.viewController.navigationItem.rightBarButtonItem = BARBUTTON(@"Disconnect", @selector(disconnect)); } if(state == GKPeerStateDisconnected) { self.isConnected = NO; showAlert(@"Lost connection with peer. You are no longer connected to another device."); [self disconnect]; if(self.viewController) { self.viewController.navigationItem.rightBarButtonItem = BARBUTTON(@"Connect", @selector(startConnection)); DO_DATA_CALLBACK(connectionLost, nil); } } } }
-(void) assignViewController: (UIViewController *)aViewController { self.viewController = aViewController; self.viewController.navigationItem.rightBarButtonItem = BARBUTTON(@"Connect", @selector(startConnection)); }
#pragma mark - Class utility methods +(void) connect { [[self sharedInstance] startConnection]; }
+(void) disconnect { [[self sharedInstance] disconnect]; }
+(void) sendData: (NSData *)data { [[self sharedInstance] sendDataToPeers:data]; }
+(void) assignViewController: (UIViewController *) aViewController { [[self sharedInstance] assignViewController:aViewController]; }
@end
|
然后使用这里类,做了一个简单的示例,我因为就一个iphone,真实的两个手机之间的连接与信息通讯我没有测试过,这个在项目中是不可取的,不过这里我们就是做一个如何来使用这个GameKit的工具,如何实现这个过程,先感受一下,看下面的代码吧:
GameKitHelperViewController.h
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
|
#import <UIKit/UIKit.h> #import "GameKitHelper.h"
@interface GameKitHelperViewController : UIViewController<GameKitHelperDataDelegate,UITextViewDelegate> @property (strong, nonatomic) IBOutlet UITextView *sendView; @property (strong, nonatomic) IBOutlet UITextView *receviceView;
@end
|
GameKitHelperViewController.m
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
|
#import "GameKitHelperViewController.h" #define COOKBOOK_PURPLE_COLOR [UIColor colorWithRed:0.20392f green:0.19607f blue:0.61176f alpha:1.0f]
@interface GameKitHelperViewController ()
@end
@implementation GameKitHelperViewController @synthesize sendView; @synthesize receviceView;
- (void)viewDidLoad { [super viewDidLoad]; self.navigationController.navigationBar.tintColor = COOKBOOK_PURPLE_COLOR; self.navigationItem.leftBarButtonItem = BARBUTTON(@"Clear", @selector(clear)); [GameKitHelper sharedInstance].sessionID = @"Typing Together"; [GameKitHelper sharedInstance].dataDelegate = self; [GameKitHelper assignViewController:self]; [sendView becomeFirstResponder]; }
- (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; }
-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { }
#pragma mark - methods of textview -(void) textViewDidChange:(UITextView *)textView { if(![GameKitHelper sharedInstance].isConnected) { return; } NSString *text = self.sendView.text; if(!text || (text.length == 0)) { text = @"clear"; } NSData *textData = [text dataUsingEncoding:NSUTF8StringEncoding]; [GameKitHelper sendData:textData]; }
-(void) receivedData:(NSData *)data { NSString *text = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; receviceView.text = [text isEqualToString:@"clear"] ? @"" : text; }
-(void) clear { sendView.text = @""; }
@end
|
这里我是使用了storyboard,然后里面加了两个UITextView