一.准备工作

  • 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.完善相册选择图片代码
    // 手势-点击从相册中选一张照片
- (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会话
@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的更多相关文章

  1. iOS 蓝牙(GameKit CoreBluetooth)

    利用GameKit框架实现ios设备的蓝牙通讯,导入框架:#import <GameKit/GameKit.h>  , 注意: 此框架只能用于ios设置间蓝牙通讯 如今苹果开放了接口来实现 ...

  2. IOS 蓝牙(GameKit、Core Bluetooth)

    GameKit的蓝牙开发注意 ● 只能用于iOS设备之间的连接 ● 只能用于同一个应用程序之间的连接 ● 最好别利用蓝牙发送比较大的数据 /* 关于蓝牙的数据传输  1. 一次性传送,没有中间方法,所 ...

  3. 蓝牙实现对等网络连接 <GameKit/GameKit.h>

    /* 1.设置UI界面 2.引入框架 3.点击选择照片 4.连接蓝牙设备 5.实现蓝牙的代理方法 6.发送照片 */ #import "ViewController.h" #imp ...

  4. iOS 蓝牙的GameKit用法

    一.连接蓝牙 显示可以连接的蓝牙设备列表 - (IBAction)buildConnect:(id)sender { // 创建弹窗 GKPeerPickerController *ppc = [[G ...

  5. iOS开发系列--通讯录、蓝牙、内购、GameCenter、iCloud、Passbook系统服务开发汇总

    --系统应用与系统服务 iOS开发过程中有时候难免会使用iOS内置的一些应用软件和服务,例如QQ通讯录.微信电话本会使用iOS的通讯录,一些第三方软件会在应用内发送短信等.今天将和大家一起学习如何使用 ...

  6. iOS--通讯录、蓝牙、内购、GameCenter、iCloud、Passbook等系统服务开发汇总

    iOS开发过程中有时候难免会使用iOS内置的一些应用软件和服务,例如QQ通讯录.微信电话本会使用iOS的通讯录,一些第三方软件会在应用内发送短信等.今天将和大家一起学习如何使用系统应用.使用系统服务: ...

  7. iOS蓝牙开发CoreBluetooth快速入门

    在iOS开发中,实现蓝牙通信有两种方式,一种是使用传统的GameKit.framework,另一种就是使用在iOS 5中加入的CoreBluetooth.framework. 利用CoreBlueto ...

  8. 2.OC蓝牙功能

    一.  最早的蓝牙框架是GameKit,iOS7之前用的比较多,它有只能支持iOS设备间的传输,但是使用步骤简单,我们只需要搞清楚两个类就可以了. GKPeerPickerController:熟称浏 ...

  9. iOS开发——高级技术&蓝牙服务

    蓝牙服务 蓝牙 随着蓝牙低功耗技术BLE(Bluetooth Low Energy)的发展,蓝牙技术正在一步步成熟,如今的大部分移动设备都配备有蓝牙4.0,相比之前的蓝牙技术耗电量大大降低.从iOS的 ...

随机推荐

  1. JDK1.5新特性(六)……Generics

    概述 Generics - This long-awaited enhancement to the type system allows a type or method to operate on ...

  2. Idiomatic Python手记一: average in FP way

    方法一: import operator def average(*args): return reduce(operator.add, args) / len(args) if args else ...

  3. bzoj 2656 [Zjoi2012]数列(sequence)(高精度)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=2656 [题意] 计算大数递推式 [思路] 高精度 [代码] #include<c ...

  4. 中断——中断描述符表的定义和初始化(二) (基于3.16-rc4)

    上篇博文对中断描述符表(IDT)中异常和非屏蔽中断部分的初始化做了说明,这篇文章将分析外部中断部分的初始化. 在上篇博文中,可以看到,内核在setup_once汇编片段中,对中断和异常部分做了初步的初 ...

  5. 【HBase学习】Apache HBase 参考手册 中文版

    正在撰写,稍后来访……

  6. 使用SecureCRT远程 SSH 登陆 CentOS 和 Ubuntu

    1.CentOS下安装SSH 使用下列命令查看当前系统是否已经安装 ssh 和 rsync.rsync是一个远程数据同步工具,可通过LAN/WAN快速同步多台主机间的文件. rpm –qa | gre ...

  7. [Objective-c 基础 - 2.2] OC弱语法、类方法

    A.OC弱语法 1.在运行的时候才会检查方法的声明和实现 2.没有声明只有实现的方法也能正常运行,只要在调用之前定义即可 3.类的声明必须存在,否则会出现运行时错误   B.类方法 1.是类名调用的方 ...

  8. Java字节码(.class文件)格式详解(一)

    原文链接:http://www.blogjava.net/DLevin/archive/2011/09/05/358033.html 小介:去年在读<深入解析JVM>的时候写的,记得当时还 ...

  9. CSS背景与列表

    CSS中背景的使用 CSS中列表的使用 15.1 CSS中背景的使用         属性名称                             属性值                      ...

  10. NGUI学习笔记(二):基础笔记

    精灵(Sprite).图集(Atlas)和贴图(Texture)的区别 图集:由多张小图拼合而成的一张大图,其好处是降低DrawCall的次数.减少载入内存的次数和方便管理同一类型的小图.一般图集都会 ...