蓝牙实现对等网络连接 <GameKit/GameKit.h>
/* 1.设置UI界面 2.引入框架 3.点击选择照片 4.连接蓝牙设备 5.实现蓝牙的代理方法 6.发送照片 */ #import "ViewController.h" #import <GameKit/GameKit.h> @interface ViewController ()<UINavigationControllerDelegate, UIImagePickerControllerDelegate, GKPeerPickerControllerDelegate> @property (nonatomic, strong) UIImageView *imgView; @property (nonatomic, strong) UIButton *selectImgBtn; @property (nonatomic, strong) UIButton *connectionDeviceBtn; @property (nonatomic, strong) UIButton *sendImgBtn; @property (nonatomic, strong) GKSession *seccion; // first deprecated in iOS 7.0 @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; _imgView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 20, self.view.bounds.size.width - 20, self.view.bounds.size.width - 20)]; _imgView.backgroundColor = [UIColor lightGrayColor]; [self.view addSubview:_imgView]; _selectImgBtn = [UIButton buttonWithType:UIButtonTypeSystem]; _selectImgBtn.frame = CGRectMake(10, CGRectGetMaxY(_imgView.frame) + 30, 60, 30); [_selectImgBtn setTitle:@"选择照片" forState:UIControlStateNormal]; [self.view addSubview:_selectImgBtn]; [_selectImgBtn addTarget:self action:@selector(clickSelectImgBtnAction) forControlEvents:UIControlEventTouchUpInside]; _connectionDeviceBtn = [UIButton buttonWithType:UIButtonTypeSystem]; _connectionDeviceBtn.frame = CGRectMake(0, 0, 60, 30); CGPoint p = CGPointMake(self.view.center.x, _selectImgBtn.center.y); _connectionDeviceBtn.center = p; [_connectionDeviceBtn setTitle:@"连接设备" forState:UIControlStateNormal]; [self.view addSubview:_connectionDeviceBtn]; [_connectionDeviceBtn addTarget:self action:@selector(clickConnectionDeviceBtnAction) forControlEvents:UIControlEventTouchUpInside]; _sendImgBtn = [UIButton buttonWithType:UIButtonTypeSystem]; _sendImgBtn.frame = CGRectMake(self.view.bounds.size.width - 70, CGRectGetMinY(_selectImgBtn.frame), 60, 30); [_sendImgBtn setTitle:@"发送照片" forState:UIControlStateNormal]; [self.view addSubview:_sendImgBtn]; [_sendImgBtn addTarget:self action:@selector(clickSendImgBtnAction) forControlEvents:UIControlEventTouchUpInside]; } // 选择图片 - (void)clickSelectImgBtnAction { // 0.判断照片是否可用 if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) { return; } // 1.创建控制器 UIImagePickerController *ipc = [UIImagePickerController new]; // 2.设置图片源 ipc.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; // 3.设置代理 ipc.delegate = self; // 4.显示控制器 [self presentViewController:ipc animated:YES completion:nil]; } // 连接设备 - (void)clickConnectionDeviceBtnAction { // 1.创建控制器 GKPeerPickerController *pic = [GKPeerPickerController new]; // first deprecated in iOS 7.0 // 2.连接设备获取数据 pic.delegate = self; // 3.显示控制器 [pic show]; } // 发送图片 - (void)clickSendImgBtnAction { // 0.需要一个data对象,把图片转化成data数据 // CGFloat compressionQuality 精度,压缩比 NSData *data = UIImageJPEGRepresentation(self.imgView.image, 0.2); // 1.通过session发送数据 /* 数据报(数据包,小块) GKSendDataReliable, 网络数据发送有误的时候,可以保证消息按照发送的顺序到达 GKSendDataUnreliable 只发一次 */ [self.seccion sendDataToAllPeers:data withDataMode:GKSendDataReliable error:nil]; // first deprecated in iOS 7.0
}
#pragma 相册的代理
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info {
NSLog(@"--info-- = %@", info);
UIImage *image = info[UIImagePickerControllerOriginalImage];
_imgView.image = image;
[picker dismissViewControllerAnimated:YES completion:nil];
}
#pragma mark - GKPeerPickerControllerDelegate
- (void)peerPickerController:(GKPeerPickerController *)picker didConnectPeer:(NSString *)peerID toSession:(GKSession *)session {
self.seccion = session;
// 1.设置session会话,Handler(句柄,类似代理)
[session setDataReceiveHandler:self withContext:nil];
// 2.控制器的移除
[picker dismiss];
}
// 从setDataReceiveHandler里面找出来
- (void)receiveData:(NSData *)data formPeer:(NSString *)peer inSession:(GKSession *)session context:(void *)context {
UIImage *image = [UIImage imageWithData:data];
_imgView.image = image;
}