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
上一篇:分布式架构ActiveMQ的安装与使用(单节点)


下一篇:spring Cloud中,解决Feign/Ribbon整合Hystrix第一次请求失败的问题?