蓝牙 GameKit
一.准备工作
1.搭建UI

2.拖线
// 图片
@property (weak, nonatomic) IBOutlet UIImageView *imageView;
// 建立连接
- (IBAction)buildConnect:(id)sender{}
// 发送数据
- (IBAction)sendData:(id)sender{}
二.连接蓝牙
- 显示可以连接的蓝牙设备列表
- (IBAction)buildConnect:(id)sender {
// 创建弹窗
GKPeerPickerController *ppc = [[GKPeerPickerController alloc] init];
// 设置代理 @interface ViewController () <GKPeerPickerControllerDelegate>
ppc.delegate = self;
// 展示
[ppc show];
}
- 监听蓝牙的连接
#pragma mark -GKPeerPickerControllerDelegate
// 连接成功就会调用
- (void)peerPickerController:(GKPeerPickerController *)picker // 弹窗
didConnectPeer:(NSString *)peerID // 连接到的蓝牙设备号
toSession:(GKSession *)session // 连接会话(通过它进行数据交互)
{
NSLog(@"%s, line = %d", __FUNCTION__, __LINE__);
// 弹窗消失
[picker dismiss];
}
三.利用蓝牙传输数据
- 点击图片从相册中选择一张显示本机
- 可以修改imaV为Btn,也可以为imaV添加手势
- 1.修改imageView的用户交互

- 2.添加手势到图片上

- 3.拖出手势的响应事件

- 4.完善相册选择图片代码
- 1.修改imageView的用户交互
- 可以修改imaV为Btn,也可以为imaV添加手势
// 手势-点击从相册中选一张照片
- (IBAction)tapImage:(UITapGestureRecognizer *)sender {
NSLog(@"%s, line = %d", __FUNCTION__, __LINE__);
// 先判断是否有相册
if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
return;
}
// 创建弹出的控制器
UIImagePickerController *ipc = [[UIImagePickerController alloc] init];
// 设置图片来源为相册
ipc.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
// 设置代理 @interface ViewController () <UINavigationControllerDelegate, UIImagePickerControllerDelegate>
ipc.delegate = self;
// modal出来
[self presentViewController:ipc animated:YES completion:nil];
}
#pragma mark - UINavigationControllerDelegate, UIImagePickerControllerDelegate
// 选中某图片后调用
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info
{
// 控制器返回
[picker dismissViewControllerAnimated:YES completion:nil];
// 设置图片
self.imageView.image = info[UIImagePickerControllerOriginalImage];
}
- 点击发送数据完成图片显示到另一个蓝牙机器上
- 1.分析需要通过GKSession对象来传递数据,所以在
peerPickerController:didConnectPeer:didConnectPeer:的方法中保存session会话
- 1.分析需要通过GKSession对象来传递数据,所以在
@property (nonatomic, strong) GKSession *session; /**< 蓝牙连接会话 */
// 连接成功就会调用
- (void)peerPickerController:(GKPeerPickerController *)picker // 弹窗
didConnectPeer:(NSString *)peerID // 连接到的蓝牙设备号
toSession:(GKSession *)session // 连接会话(通过它进行数据交互)
{
NSLog(@"%s, line = %d", __FUNCTION__, __LINE__);
// 弹窗消失
[picker dismiss];
// 保存会话
self.session = session;
}
- 发送
// 发送数据
- (IBAction)sendData:(id)sender {
if (self.imageView.image == nil) return; // 有图片才继续执行
// 通过蓝牙链接会话发送数据到所有设备
[self.session sendDataToAllPeers:UIImagePNGRepresentation(self.imageView.image) // 数据
withDataMode:GKSendDataReliable // 枚举:发完为止
error:nil];
}
- 接收
// 连接成功就会调用
- (void)peerPickerController:(GKPeerPickerController *)picker // 弹窗
didConnectPeer:(NSString *)peerID // 连接到的蓝牙设备号
toSession:(GKSession *)session // 连接会话(通过它进行数据交互)
{
NSLog(@"%s, line = %d", __FUNCTION__, __LINE__);
// 弹窗消失
[picker dismiss];
// 保存会话
self.session = session;
// 处理接收到的数据[蓝牙设备接收到数据时,就会调用 [self receiveData:fromPeer:inSession:context:]]
// 设置数据接受者为:self
[self.session setDataReceiveHandler:self
withContext:nil];
}
#pragma mark - 蓝牙设备接收到数据时,就会调用
- (void)receiveData:(NSData *)data // 数据
fromPeer:(NSString *)peer // 来自哪个设备
inSession:(GKSession *)session // 连接会话
context:(void *)context
{
NSLog(@"%s, line = %d", __FUNCTION__, __LINE__);
// 显示
self.imageView.image = [UIImage imageWithData:data];
// 写入相册
UIImageWriteToSavedPhotosAlbum(self.imageView.image, nil, nil, nil);
}
四.注意
- 只能用于iOS设备之间的链接
- 只能用于同一个应用程序之间的连接
- 最好别利用蓝牙发送比较大的数据
蓝牙 GameKit的更多相关文章
- iOS 蓝牙(GameKit CoreBluetooth)
利用GameKit框架实现ios设备的蓝牙通讯,导入框架:#import <GameKit/GameKit.h> , 注意: 此框架只能用于ios设置间蓝牙通讯 如今苹果开放了接口来实现 ...
- IOS 蓝牙(GameKit、Core Bluetooth)
GameKit的蓝牙开发注意 ● 只能用于iOS设备之间的连接 ● 只能用于同一个应用程序之间的连接 ● 最好别利用蓝牙发送比较大的数据 /* 关于蓝牙的数据传输 1. 一次性传送,没有中间方法,所 ...
- 蓝牙实现对等网络连接 <GameKit/GameKit.h>
/* 1.设置UI界面 2.引入框架 3.点击选择照片 4.连接蓝牙设备 5.实现蓝牙的代理方法 6.发送照片 */ #import "ViewController.h" #imp ...
- iOS 蓝牙的GameKit用法
一.连接蓝牙 显示可以连接的蓝牙设备列表 - (IBAction)buildConnect:(id)sender { // 创建弹窗 GKPeerPickerController *ppc = [[G ...
- iOS开发系列--通讯录、蓝牙、内购、GameCenter、iCloud、Passbook系统服务开发汇总
--系统应用与系统服务 iOS开发过程中有时候难免会使用iOS内置的一些应用软件和服务,例如QQ通讯录.微信电话本会使用iOS的通讯录,一些第三方软件会在应用内发送短信等.今天将和大家一起学习如何使用 ...
- iOS--通讯录、蓝牙、内购、GameCenter、iCloud、Passbook等系统服务开发汇总
iOS开发过程中有时候难免会使用iOS内置的一些应用软件和服务,例如QQ通讯录.微信电话本会使用iOS的通讯录,一些第三方软件会在应用内发送短信等.今天将和大家一起学习如何使用系统应用.使用系统服务: ...
- iOS蓝牙开发CoreBluetooth快速入门
在iOS开发中,实现蓝牙通信有两种方式,一种是使用传统的GameKit.framework,另一种就是使用在iOS 5中加入的CoreBluetooth.framework. 利用CoreBlueto ...
- 2.OC蓝牙功能
一. 最早的蓝牙框架是GameKit,iOS7之前用的比较多,它有只能支持iOS设备间的传输,但是使用步骤简单,我们只需要搞清楚两个类就可以了. GKPeerPickerController:熟称浏 ...
- iOS开发——高级技术&蓝牙服务
蓝牙服务 蓝牙 随着蓝牙低功耗技术BLE(Bluetooth Low Energy)的发展,蓝牙技术正在一步步成熟,如今的大部分移动设备都配备有蓝牙4.0,相比之前的蓝牙技术耗电量大大降低.从iOS的 ...
随机推荐
- 转载:MATLAB画图常用调整代码
%单y轴 plot(t*1e+,abs(iGG)/max(abs(iGG)),); axis([-,,,]) xlabel('时间/ns'); ylabel('幅度/a.u.'); ,'FontNam ...
- wxWidgets Tutorial
wxWidgets Tutorial网站整理 两个重要的教程网站:1:点这里:2:点这里. 还有一个wxWidgets项目参考的网站:点这里. 已经翻译好的中文教程:点这里. 参考书籍:<wxW ...
- [解决]ASP.NET MVC 4/5 源码调试(source code debug)
========================ASP.NET MVC 4============================ ASP.NET MVC 4 source code download ...
- 【Hadoop学习】CDH5.2安装部署
[时间]2014年11月19日 [平台]Centos 6.5 [工具]scp [软件]jdk-7u67-linux-x64.rpm CDH5.2.0-hadoop2.5.0 [步骤] 1. 准备条件 ...
- 荔枝FM架构师刘耀华:异地多活IDC机房架构 - 极客头条 - CSDN.NET
荔枝FM架构师刘耀华:异地多活IDC机房架构 - 极客头条 - CSDN.NET 荔枝FM架构师刘耀华:异地多活IDC机房架构 - 极客头条 - CSDN.NET 途牛谭俊青:多数据中心状态同步&am ...
- CodeForces 696A(Lorenzo Von Matterhorn ) & CodeForces 696B(Puzzles )
A,给一棵完全二叉树,第一个操作,给两个点,两点路径上的所有边权值都增加w,第二个操作,给两个点,求两点路径上的所有边权值和. 我看一眼题就觉得是树链剖分,而我又不会树链剖分,扔掉. 后来查了题解,首 ...
- ACCESS-如何多数据库查询(跨库查询)
测试通过:ACCESSselect * from F:\MYk.mdb.tablename说明:1.查询语句2.来原于哪(没有密码是个路径)3.查询的表名 ====================== ...
- HBase 和 MongoDB在设计上的区别
转载:http://leongfans.iteye.com/blog/1019383 昨天搜一下mongodb的资料,介绍应用的比较多,原理介绍的不多. 粗略得看了一下,总体来说两者的设计思路差不多, ...
- hiberante中get和load方法的区别
1.从返回结果上对比: load方式检索不到的话会抛出org.hibernate.ObjectNotFoundException异常 get方法检索不到的话会返回null 2.从检索执行机制上对比: ...
- 如何用JAVA生成注册序列号
原文:http://blog.csdn.net/eagleking012/article/details/7099900 平常我们都接触过软件注册,输入序列号.激活码.注册码.授权码:对于这些字符码到 ...