#import <MultipeerConnectivity/MultipeerConnectivity.h>
/*
1. 注册一个广告 告诉别人 我的设备是可以被发现
2. 扫描蓝牙设备 需要实现代理方法
3. 使用一个MCSession对象存储当前会话 需要实现代理方法
4. 使用MCSession 对象 发送和接收数据
*/
#define SERVICE_TYPE @"xmg"
@interface ViewController ()<MCBrowserViewControllerDelegate,UINavigationControllerDelegate,UIImagePickerControllerDelegate,MCSessionDelegate>
@property (weak, nonatomic) IBOutlet UIImageView *showImg;
// 保存会话
@property (nonatomic, strong)MCSession *m_session;
/** 发送广告 */
@property (nonatomic, strong) MCAdvertiserAssistant *assistant;
/** 当前连接到的设备 */
@property (nonatomic, strong) MCPeerID *peerId;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// 初始化 会话
// 获取设备的名字
NSString *displayName = [UIDevice currentDevice].name;
// 设备的id
MCPeerID *perrID = [[MCPeerID alloc]initWithDisplayName:displayName];
self.m_session = [[MCSession alloc]initWithPeer:perrID];
self.m_session.delegate = self;
}
// click events
- (IBAction)connect:(id)sender {
MCBrowserViewController *browser = [[MCBrowserViewController alloc]initWithServiceType:SERVICE_TYPE session:self.m_session];
browser.delegate = self;
[self presentViewController:browser animated:YES completion:nil];
}
- (IBAction)selecte:(id)sender {
UIImagePickerController *imgPicker = [[UIImagePickerController alloc]init];
imgPicker.delegate = self;
imgPicker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
[self presentViewController:imgPicker animated:YES completion:nil];
}
- (IBAction)sendImage:(id)sender {
if (!self.showImg.image) return;
// 发送数据
[self.m_session sendData:UIImagePNGRepresentation(self.showImg.image) toPeers:@[self.peerId] withMode:MCSessionSendDataUnreliable error:nil];
NSLog(@"===%@" ,self.m_session);
}
// 设置可被发现
- (IBAction)found:(id)sender {
UISwitch *s = (UISwitch *)sender;
if (s.isOn) {
// 注册广告 可能一个app 发送了多个广告, 所以需要给光绑定唯一标示
self.assistant = [[MCAdvertiserAssistant alloc]initWithServiceType:SERVICE_TYPE discoveryInfo:nil session:self.m_session];
[self.assistant start];
}
}
#pragma mark - 会话的代理方法
// 接收到的数据
- (void)session:(MCSession *)session didReceiveData:(NSData *)data fromPeer:(MCPeerID *)peerID
{
dispatch_async(dispatch_get_main_queue(), ^{
self.showImg.image = [UIImage imageWithData:data];
});
}
- (void)session:(MCSession *)session peer:(MCPeerID *)peerID didChangeState:(MCSessionState)state
{
}
#pragma mark - 扫描设备的代理
// 连接成功
- (void)browserViewControllerDidFinish:(MCBrowserViewController *)browserViewController
{
NSLog(@"%s %d",__func__,__LINE__);
[browserViewController dismissViewControllerAnimated:YES completion:nil];
}
// 退出连接
- (void)browserViewControllerWasCancelled:(MCBrowserViewController *)browserViewController
{
}
// 连接哪个设备
- (BOOL)browserViewController:(MCBrowserViewController *)browserViewController
shouldPresentNearbyPeer:(MCPeerID *)peerID
withDiscoveryInfo:(nullable NSDictionary<NSString *, NSString *> *)info
{
NSLog(@"%s %d",__func__,__LINE__);
self.peerId = peerID;
return YES;
}
#pragma mark - imageDelegate
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info
{
self.showImg.image = info[UIImagePickerControllerOriginalImage];
[picker dismissViewControllerAnimated:YES completion:nil];
}
@end
/*
1. 注册一个广告 告诉别人 我的设备是可以被发现
2. 扫描蓝牙设备 需要实现代理方法
3. 使用一个MCSession对象存储当前会话 需要实现代理方法
4. 使用MCSession 对象 发送和接收数据
*/
#define SERVICE_TYPE @"xmg"
@interface ViewController ()<MCBrowserViewControllerDelegate,UINavigationControllerDelegate,UIImagePickerControllerDelegate,MCSessionDelegate>
@property (weak, nonatomic) IBOutlet UIImageView *showImg;
// 保存会话
@property (nonatomic, strong)MCSession *m_session;
/** 发送广告 */
@property (nonatomic, strong) MCAdvertiserAssistant *assistant;
/** 当前连接到的设备 */
@property (nonatomic, strong) MCPeerID *peerId;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// 初始化 会话
// 获取设备的名字
NSString *displayName = [UIDevice currentDevice].name;
// 设备的id
MCPeerID *perrID = [[MCPeerID alloc]initWithDisplayName:displayName];
self.m_session = [[MCSession alloc]initWithPeer:perrID];
self.m_session.delegate = self;
}
// click events
- (IBAction)connect:(id)sender {
MCBrowserViewController *browser = [[MCBrowserViewController alloc]initWithServiceType:SERVICE_TYPE session:self.m_session];
browser.delegate = self;
[self presentViewController:browser animated:YES completion:nil];
}
- (IBAction)selecte:(id)sender {
UIImagePickerController *imgPicker = [[UIImagePickerController alloc]init];
imgPicker.delegate = self;
imgPicker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
[self presentViewController:imgPicker animated:YES completion:nil];
}
- (IBAction)sendImage:(id)sender {
if (!self.showImg.image) return;
// 发送数据
[self.m_session sendData:UIImagePNGRepresentation(self.showImg.image) toPeers:@[self.peerId] withMode:MCSessionSendDataUnreliable error:nil];
NSLog(@"===%@" ,self.m_session);
}
// 设置可被发现
- (IBAction)found:(id)sender {
UISwitch *s = (UISwitch *)sender;
if (s.isOn) {
// 注册广告 可能一个app 发送了多个广告, 所以需要给光绑定唯一标示
self.assistant = [[MCAdvertiserAssistant alloc]initWithServiceType:SERVICE_TYPE discoveryInfo:nil session:self.m_session];
[self.assistant start];
}
}
#pragma mark - 会话的代理方法
// 接收到的数据
- (void)session:(MCSession *)session didReceiveData:(NSData *)data fromPeer:(MCPeerID *)peerID
{
dispatch_async(dispatch_get_main_queue(), ^{
self.showImg.image = [UIImage imageWithData:data];
});
}
- (void)session:(MCSession *)session peer:(MCPeerID *)peerID didChangeState:(MCSessionState)state
{
}
#pragma mark - 扫描设备的代理
// 连接成功
- (void)browserViewControllerDidFinish:(MCBrowserViewController *)browserViewController
{
NSLog(@"%s %d",__func__,__LINE__);
[browserViewController dismissViewControllerAnimated:YES completion:nil];
}
// 退出连接
- (void)browserViewControllerWasCancelled:(MCBrowserViewController *)browserViewController
{
}
// 连接哪个设备
- (BOOL)browserViewController:(MCBrowserViewController *)browserViewController
shouldPresentNearbyPeer:(MCPeerID *)peerID
withDiscoveryInfo:(nullable NSDictionary<NSString *, NSString *> *)info
{
NSLog(@"%s %d",__func__,__LINE__);
self.peerId = peerID;
return YES;
}
#pragma mark - imageDelegate
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info
{
self.showImg.image = info[UIImagePickerControllerOriginalImage];
[picker dismissViewControllerAnimated:YES completion:nil];
}
@end