蓝牙实现对等网络连接 <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(, , 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;
}

蓝牙实现对等网络连接 <GameKit/GameKit.h>的更多相关文章
- wp———跳转系统设置页面的wifi、网络连接、蓝牙、飞行模式等
通过 ConnectionSettingsType 的设置,可以跳转 到 wifi.蓝牙.飞行模式.以及网络连接 其他方案跳转 private async void Button_Click_1(ob ...
- Android Wi-Fi Peer-to-Peer(Android的Wi-Fi P2P对等网络)
Wi-Fi peer-to-peer(P2P,对等网络),它同意具备对应硬件的Android 4.0(API level 14)或者更高版本号的设备能够直接通过wifi而不须要其他中间中转节点就能直接 ...
- MMORPG大型游戏设计与开发(服务器 游戏场景 掉落与网络连接)
时间一点点的消逝,伴着自己空闲日子将要结束的时候我尽量的学习和分享场景和AI的知识给朋友们,不过很遗憾的是这些文章还有不足的地方,就是有的难点没有完全的分析到.掉落在游戏中必不可少的,同时网络连接也是 ...
- VMware Workstation中网络连接之桥接、NAT和Host-only
在Windows XP系统中,安装好VMware Workstation虚拟机软件以后,我们可以查看一下"网络连接"窗口: 在窗口中多出了两块网卡: VMware Network ...
- [转]VMware Workstation网络连接的三种模式
经常要使用VMWare Workstation来在本地测试不同的操作系统,以前也搞不清楚网络连接三种模式,最近看了几篇文章才算明白.现总结如下: 1. VMware Workstation的虚拟网络组 ...
- openstack(liberty): 简单网络连接图
openstack起初的网络部分是和计算核心nova合在一起的,后来被拆分出来,独立成为一个模块, 现在名为Neutron. 本博文是学习记录,记录的是基于GRE tunnel技术的neutron和计 ...
- Win7+VMware Workstation环境下的CentOS-Linux网络连接设置
Win7+VMware Workstation环境下的CentOS-Linux网络连接设置 http://blog.sciencenet.cn/blog-430991-507041.html 近日 ...
- iOS 如何判断当前网络连接状态 网络是否正常 网络是否可用
网络资源:出处http://blog.csdn.net/mad1989/article/details/8987368 众所周知,我们在开发APP时,涉及网络连接的时候,都会想着提前判断一下当前的 ...
- linux虚拟机网络连接模式 bridged, host-only, NAT
最近安装了fedora9.0,却一直不能连接到外网,我用的是3G无线网卡上网的,起初以为是linux不支持3G无线方式的,可后来装了虚拟机ubuntu却可以上网,在后来用有ADSL网络连接的电脑安装f ...
随机推荐
- /Date(1410019200000+0800)/如何转换为date对象
<script type="text/javascript">var s = '/Date(1410019200000+0800)/ '; s.replace(/Dat ...
- 【U1结业机试题】新闻内容管理系统:解析XML文件读取Html模版生成网页文件
一.作业要求: 1.在xml文件中创建新闻节点news,包含标题.作者.日期.正文等信息 2.创建HTML模板文件 3.读取xml中所有新闻信息,并使用新闻信息替换模板文件中占位符,从而为每一条新闻生 ...
- mysql四-2:多表查询
一.介绍 本节主题: 多表连接查询 复合条件连接查询 子查询 准备表: #建表 create table department( id int, name ) ); create table empl ...
- Windows 10:开机显示C:\WINDOWS\system32\config\systemprofile\Desktop不可用 的解决方法
今晨起来开机,开完机一看,弹出啦一个框框上面写着“C:\WINDOWS\system32\config\systemprofile\Desktop不可用...” 当我点击了确定之后,发现屏幕一片黑,只 ...
- 【阿里云产品公测】简单粗暴30S完成PTS测试配置附tornado服务器测试结果
作者:阿里云用户morenocjm [阿里云产品公测]简单粗暴 30S完成PTS测试配置(附tornado服务器测试结果) -------------------------------------- ...
- Android上使用RecyclerView实现顶部悬浮标题效果的Sticky Title View
目前很多的项目都在使用顶部悬浮标题的效果,很明显,这的确是一个比较人性化,用户体验效果比较好的UI交互效果,对于这个效果,有很多种实现方式,如果说要用RecyclerView来实现一个分类信息展示,并 ...
- 机器学习(一)——K-近邻(KNN)算法
最近在看<机器学习实战>这本书,因为自己本身很想深入的了解机器学习算法,加之想学python,就在朋友的推荐之下选择了这本书进行学习. 一 . K-近邻算法(KNN)概述 最简单最初级的 ...
- CCF201712-1 最小差值
试题编号: 201712-1 试题名称: 最小差值 时间限制: 1.0s 内存限制: 256.0MB 问题描述: 问题描述 给定n个数,请找出其中相差(差的绝对值)最小的两个数,输出它们的差值的绝对值 ...
- SharePoint 2010配置PDF文件全文检索
一.安装Adobe PDF 64 bit IFilter version 9合Adobe Reader 9下载地址: http://www.adobe.com/support/downloads/de ...
- 设计模式之适配器模式(Adapter)
1. 概述 将一个类的接口转换成客户希望的另外一个接口.Adapter模式使得原本由于接口不兼容而不能一起工作的那些类可以在一起工作. 2. 解决的问题 即Adapter模式使得原本由于接口不兼容而不 ...