蓝牙实现对等网络连接 <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 ...
随机推荐
- node.js中的模板引擎jade、handlebars、ejs
使用node.js的Express脚手架生成项目默认是jade模板引擎,jade引擎实在是太难用了,这么难用还敢设为默认的模板引擎,过分了啊!用handlebars模板引擎写还说的过去,但笔者更愿意使 ...
- Java进程内缓存
今天和同事聊到了缓存,在Java中实现进程缓存.这里主要思想是,用一个map做缓存.缓存有个生存时间,过期就删除缓存.这里可以考虑两种删除策略,一种是起一个线程,定期删除过期的key.第二个是,剔除模 ...
- 封装微信jssdk自定义分享代码
var protocol = window.location.protocol; //获取协议 var host = window.location.host; //获取域名 var posuDoma ...
- js中的this问题
this this的指向在函数定义的时候是确定不了的,只有函数执行的时候才能确定this到底指向谁,实际上 this的最终指向的是那个调用它的对象(这里其实并不完全对,this的指向有时候会很微妙,得 ...
- Eclipse Startup
First of all, we take a look at class EclipseStarter /********************************************** ...
- RBG灯颜色渐变(颜色要尽可能多)程序分析
相信很多调过RBG灯的朋友都是通过分别改变R.B.G的占空比来改变颜色的,但是不是发现了一个问题,那就是不管怎样调都很难实现几十种颜色的变化,一般只有是7种颜色的渐变.下面给朋友们分享一个可以实现几十 ...
- angularJS 单页面 两个及以上个 ng-app 的处理方式
<div ng-app="myApp1" ng-controller="myCtrl1"> 名: <input type="text ...
- 关于String s = new String("xyz");创建了几个字符串对象?的问题
引用自这位朋友:http://blog.sina.com.cn/s/blog_6a6b14100100zn6r.html 首先让我们了解几个概念: 栈:由JVM分配区域,用于保存线程执行的动作和数据引 ...
- android中View点击和触摸事件的处理
android中的事件类型分为按键事件和屏幕触摸事件,Touch事件是屏幕触摸事件的基础事件,有必要对它进行深入的了解. 一个最简单的屏幕触摸动作触发了一系列Touch事件:ACTION_DOWN-& ...
- 描边时消除锯齿SetSmoothingMode
SmoothingModeAntiAlias 指定消除锯齿的呈现. SmoothingModeDefault 指定默认模式. SmoothingModeHighQuality 指定高质量.低速度呈现. ...