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 ...
随机推荐
- HDU 1016 DFS
很简单的深搜 只要看出来是深搜... 注意判断最后一点是否与加一为质数 #include<stdio.h> #include<string.h> #include<alg ...
- DS实验题 Missile
题目: 提示:并没有精度问题. 原题 NOIP2010 导弹拦截 思路 设源点为A(x1, y1)和B(x2, y2). 第一步,用结构体存节点,包括以下元素: 1.横坐标x 2.纵坐标y 3.节点和 ...
- Web--RSS
--用处:SEO,让别人订阅你的文章
- SqlServer 高版本数据库 降级
SQL Server2012 1 首先就来介绍一下SQL Server2012数据库的降级方法,成功之后突然感觉很简单,但是没成功之前很郁闷.废话少说,直接开始. 打开SQL Server2012,连 ...
- Linguistic corpora 种子语料库-待分析对象-分析与更新语料库
Computational Linguistics http://matplotlib.org/ https://github.com/matplotlib/matplotlib/blob/maste ...
- 二 mybatis 动态sql
动态sql应用 一 .什么是动态sql 1.where条件 动态查询 根据姓名或年龄或地址查询 UserMapper.xml 1 <select id="findUser" ...
- mysql-zabbix-agent
使用Zabbix监控MySQL服务器方法 01/27/2014 从Zabbix 2.2开始,Zabbix官方已经支持了MySQL监控,但是MySQL监控默认是不可用的,需要经过额外的设置才可以使用.K ...
- Qt配置信息设置(QSettings在不同平台下的使用路径)
在Windows操作系统中,大多把配置文件信息写在注册表当中,或写在*.ini文件中,对于这两种操作都有相应的Windows API函数,在以前的文章中都提及过,这里就不多说了~ 在Qt中,提供了一个 ...
- ASP.NET MVC3 Areas 分离项目 同名控制器(同名Controller) 演示demo
为什么需要分离? 我们知道MVC项目各部分职责比较清晰,相比较ASP.NET Webform而言,MVC项目的业务逻辑和页面展现较好地分离开来,这样的做法有许多优点,比如可测试,易扩展等等.但是在实际 ...
- 【C++】字符串的大小写转换
#include<string> #include<algorithm> #include<iostream> using namespace std; int m ...