iOS 蓝牙开发之传输图片
#import <UIKit/UIKit.h> @interface AppDelegate : UIResponder <UIApplicationDelegate> @property (strong, nonatomic) UIWindow *window; @end
/**
* 注意: 适用于ios7或更高系统
*
* 首先在工程里导入MultipeerConnectivity.framework框架
*
*/
#import "AppDelegate.h"
#import "RootViewController.h"
@interface AppDelegate () @end @implementation AppDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.window.backgroundColor = [UIColor whiteColor]; UINavigationController *navi = [[UINavigationController alloc] initWithRootViewController:[[RootViewController alloc] init]];
self.window.rootViewController = navi; [self.window makeKeyAndVisible];
return YES;
} @end
#import <UIKit/UIKit.h> @interface RootViewController : UIViewController @end
#import "RootViewController.h"
#import <MultipeerConnectivity/MultipeerConnectivity.h> @interface RootViewController ()<MCAdvertiserAssistantDelegate,MCSessionDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate,MCBrowserViewControllerDelegate> @property (nonatomic, strong) MCSession *session;
@property (nonatomic, strong) MCAdvertiserAssistant *advertiserAssistant;
@property (nonatomic, strong) UIImagePickerController *imagePickerController; @property (nonatomic, strong) UIImageView *imageView;
@property (nonatomic, strong) MCBrowserViewController *browserController; @end @implementation RootViewController - (void)viewDidLoad {
[super viewDidLoad]; MCPeerID *peerID = [[MCPeerID alloc] initWithDisplayName:@"蓝牙测试中..."];
self.session = [[MCSession alloc] initWithPeer:peerID];
self.session.delegate = self;
//创建广播
self.advertiserAssistant = [[MCAdvertiserAssistant alloc] initWithServiceType:@"cmj-stream" discoveryInfo:nil session:self.session];
self.advertiserAssistant.delegate = self; self.imageView = [[UIImageView alloc] initWithFrame:CGRectMake(, , , )];
self.imageView.backgroundColor = [UIColor lightGrayColor];
[self.view addSubview:self.imageView]; UIBarButtonItem *rightBtn = [[UIBarButtonItem alloc] initWithTitle:@"开启蓝牙" style:UIBarButtonItemStylePlain target:self action:@selector(startBtnAction:)];
self.navigationItem.rightBarButtonItem = rightBtn; UIBarButtonItem *leftBtn = [[UIBarButtonItem alloc] initWithTitle:@"寻找设备" style:UIBarButtonItemStylePlain target:self action:@selector(startAdvertiser:)];
self.navigationItem.leftBarButtonItem = leftBtn; UIButton *startBtn = [UIButton buttonWithType:UIButtonTypeCustom];
startBtn.frame = CGRectMake(, , , );
[startBtn setTitle:@"选择图片" forState:];
[startBtn setTitleColor:[UIColor greenColor] forState:];
[startBtn addTarget:self action:@selector(choosePhoto:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:startBtn]; UIButton *sendBtn = [UIButton buttonWithType:UIButtonTypeCustom];
sendBtn.frame = CGRectMake(, , , );
[sendBtn setTitle:@"发送图片" forState:];
[sendBtn setTitleColor:[UIColor greenColor] forState:];
[sendBtn addTarget:self action:@selector(sendBtnAction:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:sendBtn];
} - (void)sendBtnAction:(UIButton *)sender{ if (self.imageView.image) { NSError *error = nil;
[self.session sendData:UIImagePNGRepresentation(self.imageView.image) toPeers:[self.session connectedPeers] withMode:MCSessionSendDataUnreliable error:&error];
if (error) {
NSLog(@"发送有误:%@",error);
}
}
} - (void)startBtnAction:(UIBarButtonItem *)sender{
self.browserController = [[MCBrowserViewController alloc] initWithServiceType:@"cmj-stream" session:self.session];
self.browserController.delegate = self;
[self presentViewController:self.browserController animated:YES completion:nil];
} - (void)choosePhoto:(UIButton *)sender{
self.imagePickerController = [[UIImagePickerController alloc] init];
self.imagePickerController.delegate = self;
[self presentViewController:self.imagePickerController animated:YES completion:nil];
} - (void)startAdvertiser:(UIBarButtonItem *)sender{
NSLog(@"开始广播");
[self.advertiserAssistant start];
} #pragma mark - UIImagePickerController delegate -
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
self.imageView.image = image;
[self.imagePickerController dismissViewControllerAnimated:YES completion:nil];
} - (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
[self.imagePickerController dismissViewControllerAnimated:YES completion:nil];
} #pragma mark -- MCSession delegate --
-(void)session:(MCSession *)session peer:(MCPeerID *)peerID didChangeState:(MCSessionState)state{
NSLog(@"改变状态");
switch (state) {
case MCSessionStateConnected:
NSLog(@"连接成功");
break;
case MCSessionStateConnecting:
NSLog(@"连接中...");
break;
default:
NSLog(@"连接失败");
break;
}
} - (void)session:(MCSession *)session didReceiveData:(NSData *)data fromPeer:(MCPeerID *)peerID{
UIImage *image = [UIImage imageWithData:data];
self.imageView.image = image;
UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);
} #pragma mark -- MCBrowserViewController delegate --
- (void)browserViewControllerDidFinish:(MCBrowserViewController *)browserViewController{
NSLog(@"已选中");
[self.browserController dismissViewControllerAnimated:YES completion:nil];
} - (void)browserViewControllerWasCancelled:(MCBrowserViewController *)browserViewController{
NSLog(@"取消");
[self.browserController dismissViewControllerAnimated:YES completion:nil];
} - (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
} @end
iOS 蓝牙开发之传输图片的更多相关文章
- 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 ...
随机推荐
- Nginx 笔记与总结(8)Location:归纳总结
首先对 URI 进行精准匹配,如果匹配上则退出匹配,返回精准匹配结果: 如果没有匹配上则寻找普通匹配,如果命中多个普通匹配,则记忆最长的匹配结果(不考虑 location 的顺序): 如果后面还有正则 ...
- 归纳整理 PHP大神的十大优良习惯
2015年03月04日11:26 来源:程序师 作者:TECHUG 编辑:覃里 查看全文 赞(0)评论(0) 分享 标签: PHP , 编程指南 , PHP开源项目 [IT168 资讯] 1.多阅读手 ...
- memory_limit session.cache_expire ecshop初始化注释说明
memory_limit session.cache_expire ecshop初始化注释说明 memory_limit = 128M; 一个脚本所能够申请到的最大内存字节数(可以使用K和M作为单位) ...
- web.xml总结整理
web.xml 配置的详细解读 web.xml (部署描述符文件) 整理参考: 加载顺序 ServletContext-->listener->filter->srvlet ...
- JVM内存配置
JVM内存主要分为两个部分,分别是PermanentSapce和HeapSpace. PermantSpace主要负责存放加载的Class类级对象如class本身,method,field等反射对象, ...
- nRF51822之模拟IIC
使用的工程为是基于sdk10工程 在将以nRF51_SDK_10.0.0_dc26b5e\examples\peripheral\twi_sensor作为模版 修改代码main.c #include ...
- BLE-NRF51822-实现简单扫描器
在sdk目录 XXX:\Keil_v5\ARM\Pack\NordicSemiconductor\nRF_Examples\9.0.0\ble_central 下有官方提供的主从连接的demo. 官 ...
- 通过nginx配置文件抵御攻击
通过nginx配置文件抵御攻击 囧思九千 · 2013/11/12 12:22 0x00 前言 大家好,我们是OpenCDN团队的Twwy.这次我们来讲讲如何通过简单的配置文件来实现nginx防御攻击 ...
- Naming Service 与 Zookeeper
命名服务是指通过指定的名字来获取资源或者服务的地址,提供者的信息.利用Zookeeper很容易创建一个全局的路径,而这个路径就可以作为 一个名字,它可以指向集群中的集群,提供的服务的地址,远程对象 ...
- Qt 之 设置窗口边框的圆角(使用QSS和PaintEvent两种方法)
Qt在设置窗口边框圆角时有两种方式,一种是设置样式,另一种是在paintEvent事件中绘制窗口.下面分别叙述用这两种方式来实现窗口边框圆角的效果. 一.使用setStyleSheet方法 this- ...