/*
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>的更多相关文章

  1. wp———跳转系统设置页面的wifi、网络连接、蓝牙、飞行模式等

    通过 ConnectionSettingsType 的设置,可以跳转 到 wifi.蓝牙.飞行模式.以及网络连接 其他方案跳转 private async void Button_Click_1(ob ...

  2. Android Wi-Fi Peer-to-Peer(Android的Wi-Fi P2P对等网络)

    Wi-Fi peer-to-peer(P2P,对等网络),它同意具备对应硬件的Android 4.0(API level 14)或者更高版本号的设备能够直接通过wifi而不须要其他中间中转节点就能直接 ...

  3. MMORPG大型游戏设计与开发(服务器 游戏场景 掉落与网络连接)

    时间一点点的消逝,伴着自己空闲日子将要结束的时候我尽量的学习和分享场景和AI的知识给朋友们,不过很遗憾的是这些文章还有不足的地方,就是有的难点没有完全的分析到.掉落在游戏中必不可少的,同时网络连接也是 ...

  4. VMware Workstation中网络连接之桥接、NAT和Host-only

    在Windows XP系统中,安装好VMware Workstation虚拟机软件以后,我们可以查看一下"网络连接"窗口: 在窗口中多出了两块网卡: VMware Network ...

  5. [转]VMware Workstation网络连接的三种模式

    经常要使用VMWare Workstation来在本地测试不同的操作系统,以前也搞不清楚网络连接三种模式,最近看了几篇文章才算明白.现总结如下: 1. VMware Workstation的虚拟网络组 ...

  6. openstack(liberty): 简单网络连接图

    openstack起初的网络部分是和计算核心nova合在一起的,后来被拆分出来,独立成为一个模块, 现在名为Neutron. 本博文是学习记录,记录的是基于GRE tunnel技术的neutron和计 ...

  7. Win7+VMware Workstation环境下的CentOS-Linux网络连接设置

    Win7+VMware Workstation环境下的CentOS-Linux网络连接设置 http://blog.sciencenet.cn/blog-430991-507041.html   近日 ...

  8. iOS  如何判断当前网络连接状态  网络是否正常  网络是否可用

    网络资源:出处http://blog.csdn.net/mad1989/article/details/8987368   众所周知,我们在开发APP时,涉及网络连接的时候,都会想着提前判断一下当前的 ...

  9. linux虚拟机网络连接模式 bridged, host-only, NAT

    最近安装了fedora9.0,却一直不能连接到外网,我用的是3G无线网卡上网的,起初以为是linux不支持3G无线方式的,可后来装了虚拟机ubuntu却可以上网,在后来用有ADSL网络连接的电脑安装f ...

随机推荐

  1. C# string Stream 互转

    使用C#将字符串转化成流,将流转换成字符串,代码如下: using System.IO; using System.Text; namespace CSharpConvertString2Stream ...

  2. JavaScirpt(JS)——js介绍及ECMAScript

    一.JavaScript历史发展 JavaScript语言的历史:http://javascript.ruanyifeng.com/introduction/history.html 1994年12月 ...

  3. c++sizeof大全

    sizeof,一个其貌不扬的家伙,引无数菜鸟竟折腰,小虾我当初也没少犯迷糊,秉着“辛苦我一个,幸福千万人”的伟大思想,我决定将其尽可能详细的总结一下.但当我总结的时候才发现,这个问题既可以简单,又可以 ...

  4. 打杂程序员之nginx服务配置

    现在公司要在服务器上多加个网站用同一个nginx服务器,而且都是公用80端口. 因为服务器上跑着好几个网站了,所以配置文件配置完成时候要检测一下能不能用,用nginx -t:最好不要直接杀死nginx ...

  5. 正则表达式备忘录-Regular Expressions Cheatsheet中文版

    正则表达式备忘录Regular Expressions Cheatsheet中文版原文:https://www.maketecheasier.com/cheatsheet/regex/ 测试文件a.t ...

  6. 跨平台移动开发_PhoneGap 警告,通知,鸣叫,振动4 种通知类型

    创建鸣叫  使用 confirmation.beep 创建鸣叫 function playBeep() {     navigator.notification.beep(1); } 创建振动  使用 ...

  7. Windows资源管理器对物理内存的描述

    对每个进程的虚拟/物理内存使用描述: 1.硬错误/秒:在最后一分钟内每秒出现的平均硬页错误数 2.提交(KB):操作系统为内存保留的虚拟内存量,任务管理器中显示为:提交大小 3.工作集(KB):进程当 ...

  8. C# Process.WaitForExit()与死锁

    前段时间遇到一个问题,搞得焦头烂额,现在记录下来,希望对大家有所帮助. 程序里我使用Process类启动命令行,执行批处理文件 'Create.cmd'(当我手工将此文件拖入命令行执行时,一切正常). ...

  9. 安装PYTHON PIL包

    安装pillow而不是PIL pip install pillow 参考: https://github.com/python-pillow/Pillow

  10. selenium启动不了浏览器或者启动后不会写入网址,先更新下浏览器驱动

    平时自动化习惯用Chrome浏览器.有几个月没用selenium启动IE和Firefox,今天跑兼容性测试,需要验证其他浏览器.结果遇到两个异常: 1 IE启动不了,直接报错. 2 Firefox启动 ...