iOS 蓝牙开发之(mutipeerConnectivity)
蓝牙
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)的更多相关文章
- iOS蓝牙开发(二)蓝牙相关基础知识
原文链接: http://liuyanwei.jumppo.com/2015/07/17/ios-BLE-1.html iOS蓝牙开发(一)蓝牙相关基础知识: 蓝牙常见名称和缩写 MFI ====== ...
- iOS 蓝牙开发资料记录
一.蓝牙基础认识: 1.iOS蓝牙开发: iOS蓝牙开发:蓝牙连接和数据读写 iOS蓝牙后台运行 iOS关于app连接已配对设备的问题(ancs协议的锅) iOS蓝牙空中 ...
- iOS蓝牙开发(4.0)详解
最近由于项目需要, 一直在研究蓝牙4.0,在这儿分享给大家, 望共同进步. 一.关于蓝牙开发的一些重要的理论概念: 1.当前ios中开发蓝牙所运用的系统库是<CoreBluetooth/Core ...
- iOS 蓝牙开发详解
目前iOS智能硬件的开发交互方式主要分为两种,一种是基于低功耗的蓝牙4.0技术(由于耗电低,也称作为BLE(Bluetooth Low Energy))对应iOS的框架为CoreBluetooth,另 ...
- iOS蓝牙开发总结-4
蓝牙开发总结 只要熟悉蓝牙的流程,和蓝牙中每一个角色的作用,其实蓝牙通讯并没有想象中的难 1.蓝牙中心CBCentralManager:一般指得是iPhone手机 2.设备(外设)CBPeripher ...
- iOS蓝牙开发CoreBluetooth快速入门
在iOS开发中,实现蓝牙通信有两种方式,一种是使用传统的GameKit.framework,另一种就是使用在iOS 5中加入的CoreBluetooth.framework. 利用CoreBlueto ...
- ios蓝牙开发(三)ios连接外设的代码实现:手机app去读写蓝牙设备。
手机app去读写蓝牙设备....... 代码下载: 原文博客主提供Github代码连接,地址是:https://github.com/coolnameismy/demo ios连接外设的代码实现流程: ...
- iOS 蓝牙开发(二)iOS 连接外设的代码实现(转)
转载自:http://www.cocoachina.com/ios/20150917/13456.html 原文作者:刘彦玮 上一篇文章介 绍了蓝牙的技术知识,这里我们具体说明一下中心模式的应用场景. ...
- iOS 蓝牙开发(三)app作为外设被连接的实现(转)
转载自:www.cocoachina.com/ios/20151105/14071.html 原作者:刘彦玮 再上一节说了app作为central连接peripheral的情况,这一节介绍如何使用ap ...
- iOS蓝牙开发(一)蓝牙相关基础知识(转)
转载自:http://www.cocoachina.com/ios/20150915/13454.html 原文作者:刘彦玮 蓝牙常见名称和缩写 MFI ======= make for ipad , ...
随机推荐
- Linux笔记:crond
crontab中列出需要执行的命令及执行时间.由于指定的日期Day与星期Week不是相互包含的关系,而是有相交也有不相交的时间.当日期数与星期数均指定为确定值的时候,指令的执行时间到底是什么样的,之前 ...
- centos6.7下安装配置vnc
vnc是一款使用广泛的服务器管理软件,可以实现图形化管理,下面简单介绍一下如何在centos6.7下安装vnc. 1.安装vncserver yum install tigervnc tigervnc ...
- delphi 按位运算 not and or xor shl shr
delphi 按位运算 not and or xor shl shr unit Unit1; interface uses Windows, Messages, SysUtils, Var ...
- SQL 语句基础
一 查询常量1. SELECT 学生编号, 学生姓名,性别 FROM tb_Student2. SELECT 学生姓名 AS 姓名, 性别 AS 学生性别 FROM tb_Student3. SELE ...
- MFC 消息类型
标准(窗口)消息:窗口消息一般与窗口内部运作有关,如创建窗口,绘制窗口,销毁窗口,通常,消息是从系统发到窗口,或从窗口发到系统.发送函数SendMessage()或者PostMessage().除WM ...
- Git的提交忽略文件
.gitingore文件内容如下 /target//.settings//.classpath/.project/logs/
- centos7 下编译ffmpeg
下载包: wget https://ffmpeg.org/releases/ffmpeg-4.1.tar.gz GCC如果没有yasm: http://yasm.tortall.net/Downloa ...
- 【前端GUI】—— 网站美工必须掌握的PS知识点&思维导图
前言:前端离不开与设计的沟通,有时候还需要自己上手改动甚至设计网页,所以这里简单梳理一下近期学习的“网站美工”相关知识及练习.(工作用不上的时候,自己玩儿着也蛮有意思的,哈哈(*゚∀゚*)~) 一.P ...
- 1M网速等于多少K
http://zhidao.baidu.com/question/157400316.html&__bd_tkn__=65ac453b343794385019e962bfb06bb8c710d ...
- asp.net母版-页脚制作
1.母版创建流程略过 2.创建母版页css:Site.css body{ } .linkButton{ text-decoration:none; color:whitesmoke; } 3.母版页添 ...