蓝牙

mutipeerConnectivity

iOS7 引入的一个全新框架

替代GameKit框架

多用于文件传输

iOS设备不联网也能给附近的人聊天

搜索和传输的方式

* 双方WIFI和蓝牙都没有打开:无法实现
* 双方都开启蓝牙:通过蓝牙发现和传输
* 双方都开启WIFI:通过WIFI Direct发现和传输,速度接近AirDrop
* 双方同时开启了WIFI和蓝牙:模拟AirDrop,通过低功耗蓝牙技术扫描发现握手,然后通过WIFI Direct传输

示例代码

步骤

.注册一个广播  告诉别人 我的设备是可以被发现的
.扫描蓝牙设备 需要实现代理方法
.使用一个MCSession对象 存储当前的会话 需要实现代理方法
.使用MCSession对象 发送和接收数据

连接设备

创建MCSession对象用于存放当前连接的会话

//初始化会话
MCPeerID *peerId = [[MCPeerID alloc] initWithDisplayName:[UIDevice currentDevice].name];
self.mySession = [[MCSession alloc] initWithPeer:peerId];

开启广播对象,通知正在搜索的设备他们是可用的

//设置可被发现
- (IBAction)fount:(id)sender {
UISwitch *s = (UISwitch *)sender;
if (s.isOn) {
//注册广告
/*
服务类型 可能一个app发送了多个广告 所以需要给广告绑定一个唯一标识
     session:当前会话
*/
_assistant = [[MCAdvertiserAssistant alloc]initWithServiceType:SERVICE_TYPE discoveryInfo:nil session:_mySession];
//开启广播
[_assistant start]; }
}

开始搜索蓝牙设备

- (IBAction)lianjieshebei:(id)sender {
//创建搜索蓝牙设备设备控制器
MCBrowserViewController *browser = [[MCBrowserViewController alloc] initWithServiceType:SERVICE_TYPE session:_mySession];
browser.delegate = self;
[self presentViewController:browser animated:YES completion:nil];
}

连接以后选择数据发送

- (IBAction)xuanzewenjian:(id)sender {
UIImagePickerController *imagepicker = [[UIImagePickerController alloc] init];
imagepicker.delegate = self;
imagepicker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
[self presentViewController:imagepicker animated:YES completion:nil];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info {
self.imageView.image = info[UIImagePickerControllerOriginalImage];
[picker dismissViewControllerAnimated:YES completion:nil];
}

在搜索控制器的连接完成代理方法中隐藏搜索设备控制器

#pragma mark - 扫描设备的代理
//发现设备以后可以通过代理方法来实现传送数据 扫描设备的代理
//链接成功
- (void)browserViewControllerDidFinish:(MCBrowserViewController *)browserViewController{
[browserViewController dismissViewControllerAnimated:YES completion:nil];
}

实现搜索控制器的代理方法,获取连接上的设备ID

#pragma mark - MCBrowserViewControllerDelegate
/**
* 连接成功
*
* @param browserViewController 搜索控制器
* @param peerID 连接上的设备ID
* @param info 连接的信息
*
* @return YES : 只发送连接上的用户
*/
- (BOOL)browserViewController:(MCBrowserViewController *)browserViewController
shouldPresentNearbyPeer:(MCPeerID *)peerID
withDiscoveryInfo:(nullable NSDictionary<NSString *, NSString *> *)info
{
self.peerID = peerID;
NSLog(@"info == %@ peer = %@",info, peerID);
return YES;
}

将选择好的文件发送到连接到的设备ID上

- (IBAction)sendImage {

    // 判断是否有图片
if (!self.showImage.image) return; // 获取图片
UIImage *image = self.showImage.image;
// 将图片转换成NSData类型
NSData *data = UIImagePNGRepresentation(image);
/**
* 发送数据
* toPeers : 发给的设备ID的数组
* withMode: 发送模式,是否是安全模式
*/
if (self.peerID != nil) {
[self.mc_Session sendData:data toPeers:@[self.peerID] withMode:MCSessionSendDataUnreliable error:nil];
}
}

接收数据

#pragma mark MCSessionDelegate
//接收到数据
- (void)session:(MCSession *)session didReceiveData:(NSData *)data fromPeer:(MCPeerID *)peerID {
dispatch_async(dispatch_get_main_queue(), ^{
self.imageView.image = [UIImage imageWithData:data];
}); }
//必须实现的
- (void)session:(MCSession *)session peer:(MCPeerID *)peerID didChangeState:(MCSessionState)state { }

iOS 蓝牙开发之(mutipeerConnectivity)的更多相关文章

  1. iOS蓝牙开发(二)蓝牙相关基础知识

    原文链接: http://liuyanwei.jumppo.com/2015/07/17/ios-BLE-1.html iOS蓝牙开发(一)蓝牙相关基础知识: 蓝牙常见名称和缩写 MFI ====== ...

  2. iOS 蓝牙开发资料记录

    一.蓝牙基础认识:   1.iOS蓝牙开发:  iOS蓝牙开发:蓝牙连接和数据读写   iOS蓝牙后台运行  iOS关于app连接已配对设备的问题(ancs协议的锅)          iOS蓝牙空中 ...

  3. iOS蓝牙开发(4.0)详解

    最近由于项目需要, 一直在研究蓝牙4.0,在这儿分享给大家, 望共同进步. 一.关于蓝牙开发的一些重要的理论概念: 1.当前ios中开发蓝牙所运用的系统库是<CoreBluetooth/Core ...

  4. iOS 蓝牙开发详解

    目前iOS智能硬件的开发交互方式主要分为两种,一种是基于低功耗的蓝牙4.0技术(由于耗电低,也称作为BLE(Bluetooth Low Energy))对应iOS的框架为CoreBluetooth,另 ...

  5. iOS蓝牙开发总结-4

    蓝牙开发总结 只要熟悉蓝牙的流程,和蓝牙中每一个角色的作用,其实蓝牙通讯并没有想象中的难 1.蓝牙中心CBCentralManager:一般指得是iPhone手机 2.设备(外设)CBPeripher ...

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

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

  7. ios蓝牙开发(三)ios连接外设的代码实现:手机app去读写蓝牙设备。

    手机app去读写蓝牙设备....... 代码下载: 原文博客主提供Github代码连接,地址是:https://github.com/coolnameismy/demo ios连接外设的代码实现流程: ...

  8. iOS 蓝牙开发(二)iOS 连接外设的代码实现(转)

    转载自:http://www.cocoachina.com/ios/20150917/13456.html 原文作者:刘彦玮 上一篇文章介 绍了蓝牙的技术知识,这里我们具体说明一下中心模式的应用场景. ...

  9. iOS 蓝牙开发(三)app作为外设被连接的实现(转)

    转载自:www.cocoachina.com/ios/20151105/14071.html 原作者:刘彦玮 再上一节说了app作为central连接peripheral的情况,这一节介绍如何使用ap ...

  10. iOS蓝牙开发(一)蓝牙相关基础知识(转)

    转载自:http://www.cocoachina.com/ios/20150915/13454.html 原文作者:刘彦玮 蓝牙常见名称和缩写 MFI ======= make for ipad , ...

随机推荐

  1. Codeforces 583 DIV2 Asphalting Roads 模拟

    原题链接:http://codeforces.com/problemset/problem/583/A 题意: 很迷很迷,表示没看懂..但是你看样例就秒懂了 题解: 照着样例模拟就好 代码: #inc ...

  2. Wildcard Matching - LeetCode

    Implement wildcard pattern matching with support for '?' and '*'. '?' Matches any single character. ...

  3. jquery_final

    第一章 jquery入门 1,jquery的引入 <script type="text/javascript" src="js/jquery-3.3.1.min.j ...

  4. codeigniter 使用

    CodeIgniter系列 记录count和分页 对于某个表的不带条件的count,可以简单的用 $total = $this->db->count_all($table_name) 来获 ...

  5. Delphi Integer 转成单字节

    整形不能超过256 b:=Byte(StrToInt(n)); var   s: string;   b: Byte; begin   s := Edit1.Text;   b := Byte(Str ...

  6. 状态压缩DP常遇到的位运算

    位操作一共有6种形式:<<,>>,&,|,^,~; 1.左移操作符<<:左移操作符将整数的二进制向左移若干位,将最高若干位挤掉,并在低位补0 如: ; // ...

  7. vim批量缩进功能

    注释符号:" :set tabstop=4 设定tab宽度为4个字符 :set shiftwidth=4 设定自动缩进为4个字符 :set expandtab 用space替代tab的输入 ...

  8. setOnFocusChangeListener的使用

    类似于文本框里面hint文字在初始化的时候显示或者隐藏的操作,就要用到setOnFocusChangeListener的 首先我认为不是太必要- 毕竟当你输入东西时,默认文字自然会消失 当然假设你执意 ...

  9. context:exclude-filter spring事宜【经典-转】

    context:exclude-filter spring事务 如果带上事务,那么用annotation方式的事务注解和bean配置,事务会失效,要将service bean配置到xml文件中才行. ...

  10. Hadoop实战: Linux报 tmp 磁盘存储不足

    Linux 权限真是一大堆呀.在Linux下进行试验,突然来了个tmp磁盘存储不足..... .. .. ..... ... . 然而.我却没有权限给tmp添加容量.......... .. . 仅仅 ...