蓝牙
mutipeerConnectivity
简介
* iOS 7引入的一个全新框架
* 多点连接
* 替代GameKit框架
* 多用于文件的传输
* iOS设备不联网也能跟附近的人聊天
* FireChat
* See You Around
* 以上近场聊天App都是基于mutipeerConnectivity框架
* 搜索和传输的方式
* 双方WIFI和蓝牙都没有打开:无法实现
* 双方都开启蓝牙:通过蓝牙发现和传输
* 双方都开启WIFI:通过WIFI Direct发现和传输,速度接近AirDrop
* 双方同时开启了WIFI和蓝牙:模拟AirDrop,通过低功耗蓝牙技术扫描发现握手,然后通过WIFI Direct传输
案例界面搭建
连接设备
// 创建MCSession对象
// initWithPeer:设备的ID
// 用于存放当前的连接的会话
self.mc_Session = [ [MCSession alloc]initWithPeer:[[MCPeerID alloc] initWithDisplayName:[UIDevice currentDevice].name]];
- (IBAction)foundConnect:(id)sender {
UISwitch *switchBtn = (UISwitch *)sender;
if (switchBtn.isOn) {
if (self.advertiserAssistant == nil) {
self.advertiserAssistant = [[MCAdvertiserAssistant alloc] initWithServiceType:SERVICE_TYPE discoveryInfo:nil session:self.mc_Session];
}
[self.advertiserAssistant start];
self.connectBT.enabled = NO;
}else{
self.connectBT.enabled = YES;
}
}
- (IBAction)connectBlueTooth {
MCBrowserViewController *mbVC = [[MCBrowserViewController alloc]initWithServiceType:SERVICE_TYPE session:self.mc_Session];
mbVC.delegate = self;
[self presentViewController:mbVC animated:YES completion:nil];
}
选择数据
- (IBAction)selectImage {
UIImagePickerController *imgPicker = [[UIImagePickerController alloc]init];
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeSavedPhotosAlbum]) {
imgPicker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
imgPicker.delegate = self;
[self presentViewController:imgPicker animated:YES completion:nil];
}
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info
{
self.showImage.image = info[UIImagePickerControllerOriginalImage];
[picker dismissViewControllerAnimated:YES completion:nil];
}
/**
* 连接完成
*
* @param browserViewController 搜索控制器
*/
- (void)browserViewControllerDidFinish:(MCBrowserViewController *)browserViewController
{
[browserViewController dismissViewControllerAnimated:YES completion:nil];
NSLog(@"%s %d",__func__,__LINE__);
}
发送数据
#pragma mark - MCBrowserViewControllerDelegate
/**
* 连接成功
*
* @param browserViewController 搜索控制器
* @param peerID 连接上的设备ID
* @param info 连接的信息
*
* @return YES : 只发送连接上的用户
*/
- (BOOL)browserViewController:(MCBrowserViewController *)browserViewController
shouldPresentNearbyPeer:(MCPeerID *)peerID
withDiscoveryInfo:(nullable NSDictionary<NSString *, NSString *> *)info
{
self.peerID = peerID;
NSLog(@"info == %@ peer = %@",info, peerID);
return YES;
}
- (IBAction)sendImage {
UIImage *image = self.showImage.image;
NSData *data = UIImagePNGRepresentation(image);
/**
* 发送数据
* toPeers : 发给的设备ID的数组
* withMode: 发送模式,是否是安全模式
*/
if (self.peerID != nil) {
[self.mc_Session sendData:data toPeers:@[self.peerID] withMode:MCSessionSendDataUnreliable error:nil];
}
}
接收数据
- (void)session:(MCSession *)session didReceiveData:(NSData *)data fromPeer:(MCPeerID *)peerID
{
NSLog(@"%s %d",__func__,__LINE__);
NSLog(@"data = %@ , peer = %@",data,peerID);
}
显示数据
- (void)session:(MCSession *)session didReceiveData:(NSData *)data fromPeer:(MCPeerID *)peerID
{
NSLog(@"%s %d",__func__,__LINE__);
UIImage *image = [[UIImage alloc]initWithData:data];
if(image != nil){
dispatch_async(dispatch_get_main_queue(), ^{
self.showImage.image = image;
});
}
}