#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 蓝牙开发之传输图片的更多相关文章

  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 ...

随机推荐

  1. 翻译 Tri-Ace:在Shader里近似渲染公式

    继上一篇:次世代基于物理渲染的反射模型,本篇是Tri-Ace 在cedec2014上最近发布的, 主要内容如名称所示,解释了他们在实现基于物理渲染时,对shader的渲染公式所做的近似工作.     ...

  2. Memcache技术分享:介绍、使用、存储、算法、优化、命中率

    1.memcached 介绍 1.1 memcached 是什么? memcached 是以LiveJournal旗下Danga Interactive 公司的Brad Fitzpatric 为首开发 ...

  3. 图解SQL多表关联查询

      图解SQL多表关联查询     网上看了篇文章关于多表连接的,感觉很好,记录下来,以便日后自己学习  内连接     左连接     右连接       全外连接   1. 查两表关联列相等的数据 ...

  4. 耦合 Coupling the object-oriented paradigm && data coupling

    Computer Science An Overview _J. Glenn Brookshear _11th Edition 耦 两个人一起耕地 one of the benefits of the ...

  5. Xlib 窗口属性

    Xlib 窗口属性 转, 无法找到原作者 所有的 InputOutput 窗口都可以有零个或者多个像素的边框宽度,一个可选的背景,一个事件压制掩码(它压制来自孩子的事件传播),和一个 property ...

  6. 下载大图的demo by apple,值得研究和参考

    https://developer.apple.com/library/content/samplecode/LargeImageDownsizing/Introduction/Intro.html ...

  7. linux环境下的伪分布式环境搭建

    本文的配置环境是VMware10+centos2.5. 在学习大数据过程中,首先是要搭建环境,通过实验,在这里简短粘贴书写关于自己搭建大数据伪分布式环境的经验. 如果感觉有问题,欢迎咨询评论. 一:伪 ...

  8. 【Android测试】【第十四节】Appium——简述

    ◆版权声明:本文出自胖喵~的博客,转载必须注明出处. 转载请注明出处:http://www.cnblogs.com/by-dream/p/5124340.html 前言 同样的,这一篇我要介绍的也是一 ...

  9. zepto源码--matches--学习笔记

    zepto的第一个函数,zepto.matches: 作用:用来匹配dom元素是否匹配某css selector. 它为后面的一些高级方法的实现提供了基础支持,比如事件代理,parent, close ...

  10. 【android学习3】解决Android界面布局添加EditView之后无法预览问题

    在设计登陆界面时,拖入一个EditView之后发现界面无法预览 问题分析: 进入xml源文件里发现一个警告,提示添加inputType或者hint元素,添加后界面仍然无法预览... 仔细查看了当前使用 ...