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 ...
随机推荐
- CSS3:动画大全
和过渡的区别 页面不用明显js调用: 过渡:必须有:hover visited 等伪类调用.(本质还是事件驱动) 动画:页面加载上就可以. 页面有js调用: 7个参数,*为可选 animation-n ...
- 《Java核心技术卷一》笔记 多线程
有时,我们需要在一个程序中同时并行的处理多个任务,如播放器一边要播放音乐同时还要不断更新画面显示,或者是一边执行耗时任务,UI还能一边继续响应各种事件.还有的时候,一个任务需要很长时间才能完成,如果分 ...
- PHP 表单添加隐藏 Token 阻止外部提交
Token 法:通过一个隐藏可变的 Token 加大攻击的难度,每次提交都需要和服务器校对,如果不通过,则为外部提交(也可以通过 session + 隐藏域来验证). 代码: form.php < ...
- centos7 安装及配置
第一步 下载centoshttps://www.centos.org/download/CentOS-7.0-1406-x86_64-DVD.iso:这个镜像(DVD image)包括了那些可以用安装 ...
- debug和release转载
Debug和Release区别 转自草原和大树 VC下Debug和Release区别 最近写代码过程中,发现 Debug 下运行正常,Release 下就会出现问题,百思不得其解,而Release 下 ...
- wordpress 自定义面板显示不了挂件区问题
刚才在写一个wordpress主题,遇到一个问题.注册好的挂件区在控制面板(dashboard)上显示,在自定义面板上却不显示. 查询了下,发现几个老外朋友也遇到了这个问题: http://wordp ...
- 20145235 《Java程序设计》第6周学习总结
教材学习内容总结 10.1 InputStream与OutputStream 串流设计的概念 Java将输入/输出抽象化为串流,数据有来源及目的地,衔接两者的是串流对象. 从应用程序角度来看,如果要将 ...
- linux 自动登录脚本
#!/usr/bin/expect set port 22 set user xiaoming set password xiaoming123 set host 111.222.22.33 set ...
- Linux下使用inode删除文件
Linux 下有时候某些文件无法使用 rm 直接删除, 比如该文件的文件名含有终端不能正确显示的字符.# ls -litotal 0441511 -rw-r--r-- 1 root root 0 Ap ...
- <?php set_time_limit(10);
<?php include('w_fun.php'); set_time_limit(10); Fatal error: Maximum execution time of 10 seconds ...