蓝牙实现对等网络连接 <GameKit/GameKit.h>
2024-08-24 17:08:29
/*
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(, , self.view.bounds.size.width - , self.view.bounds.size.width - )];
_imgView.backgroundColor = [UIColor lightGrayColor];
[self.view addSubview:_imgView]; _selectImgBtn = [UIButton buttonWithType:UIButtonTypeSystem];
_selectImgBtn.frame = CGRectMake(, CGRectGetMaxY(_imgView.frame) + , , );
[_selectImgBtn setTitle:@"选择照片" forState:UIControlStateNormal];
[self.view addSubview:_selectImgBtn];
[_selectImgBtn addTarget:self action:@selector(clickSelectImgBtnAction) forControlEvents:UIControlEventTouchUpInside]; _connectionDeviceBtn = [UIButton buttonWithType:UIButtonTypeSystem];
_connectionDeviceBtn.frame = CGRectMake(, , , );
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 - , CGRectGetMinY(_selectImgBtn.frame), , );
[_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;
}
最新文章
- 数据结构与算法分析 java语音描述(引论)
- Oracle闪回技术详解
- Python之路,Day2 - Python基础2
- iptables防火墙作为基本需求的配置
- xcode6以后, 使用.pch
- java提高篇---LinkedList
- 复杂事件处理引擎—Esper入门
- execute immediate的简单用法(oracle)
- [LeetCode]题解(python):017-Letter Combinations of a Phone Number
- Git 开发新的功能分支
- snmp之GenericAddress
- Kinect v2(Microsoft Kinect for Windows v2 )配置移动电源解决方案
- 1.1 java语言有什么优点
- Python import this : The Zen of Python
- 界面渐变特效 -- CSS实现 -- 兼容IE8
- java实验-3
- Holt-Winters
- 05Hadoop 概论
- 力扣(LeetCode) 27. 移除元素
- idea环境下js、css中文乱码