蓝牙

niOS中提供了4个框架用于实现蓝牙连接
  • GameKit.framework(用法简单)
ü只能用于iOS设备之间的连接,多用于游戏(比如五子棋对战),从iOS7开始过期
p
  • MultipeerConnectivity.framework
ü只能用于iOS设备之间的连接,从iOS7开始引入,主要用于文件共享(仅限于沙盒的文件)
p
  • ExternalAccessory.framework
ü可用于第三方蓝牙设备交互,但是蓝牙设备必须经过苹果MFi认证(国内较少)
p
  • CoreBluetooth.framework(时下热门)
ü可用于第三方蓝牙设备交互,必须要支持蓝牙4.0
²硬件至少是4s,系统至少是iOS6
²蓝牙4.0以低功耗著称,一般也叫BLE(Bluetooth Low Energy)
ü目前应用比较多的案例:运动手坏、嵌入式设备、智能家居
 
-----------------------------------------------------------------------------------------

/**

 *  GameKit

 *

    只能用于同一个应用程序之间在iOS设备上的连接

 */

#import "ViewController.h"

#import <GameKit/GameKit.h>

@interface ViewController ()<UINavigationControllerDelegate, UIImagePickerControllerDelegate, GKPeerPickerControllerDelegate>

- (IBAction)connectBtn;

- (IBAction)choosePicBtn;

- (IBAction)sendBtn;

@property (weak, nonatomic) IBOutlet UIImageView *imageView;

@property (nonatomic, strong) GKSession *session; // 会话

@end

@implementation ViewController

- (void)viewDidLoad {

    [super viewDidLoad];

    //

    

}

/**

 *  连接

 */

- (IBAction)connectBtn

{

    // 1. 创建选择其他蓝牙设备控制器

    GKPeerPickerController *peerCon = [[GKPeerPickerController alloc] init];

    

    // 2. 成为该控制器的代理

    peerCon.delegate = self;

    

    // 3. 显示蓝牙控制器

    [peerCon show];

}

#pragma mark - GKPeerPickerControllerDelegate

/**

 *   4. 实现代理方法  -- 当蓝牙设备连接成功就会调用次方法

 *

 *  @param picker  触发事件的控制器对象

 *  @param peerID  连接蓝牙设备 的ID

 *  @param session 连接蓝牙的会话(可用通讯)

 */

- (void)peerPickerController:(GKPeerPickerController *)picker didConnectPeer:(NSString *)peerID toSession:(GKSession *)session

{

    LogMagenta(@"%@",peerID);

    // 1. 保存会话

    self.session = session;

    

    /**

     *  --- 设置监听接收数据

     *

     *  @param id 接收者

     *

     */

    [self.session setDataReceiveHandler:self withContext:nil];

    

    // 2. 关闭 显示蓝牙设备

    [picker dismiss];

    

}

/**

 *   取消连接

 **/

- (void)peerPickerControllerDidCancel:(GKPeerPickerController *)picker

{

    LogYellow(@"%s",__func__);

}

/**

 *  5. 接收到其他设备传递过来的数据 就会调用

 *

 *  @param data    传递过来的数据

 *  @param peer    传过来的设备ID

 *  @param session 会话

 *  @param context 注册监听时 传递的数据

 */

- (void) receiveData:(NSData *)data fromPeer:(NSString *)peer inSession: (GKSession *)session context:(void *)context

{

    LogRed(@"%s",__func__);

    // data 为发送过来的 图片

    UIImage *image = [UIImage imageWithData:data];

    

}

/**

 *  发送

 */

- (IBAction)sendBtn

{

    // 取出数据

    NSData *imageData = UIImageJPEGRepresentation(self.imageView.image, 2000);

    // 发送数据 - session

    /**

     GKSendDataReliable,  ---  数据安全的发送模式

     GKSendDataUnreliable, --- 数据不安全的发送模式  --- 视频播放, 游戏等

     */

    [self.session sendDataToAllPeers:imageData withDataMode:GKSendDataReliable error:nil];

    

}

/**

 *  选择图片

 */

- (IBAction)choosePicBtn

{

    // 1. 创建图片选择控制器

    UIImagePickerController *imagePickerCon = [[UIImagePickerController alloc] init];

    

    // 设置代理

    imagePickerCon.delegate = self;

    

    // 2. 判断图库是否可以打开

    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {

        // 3. 设置打开图库的类型

        imagePickerCon.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;

        

        // 4. 打开图片选择控制器

        [self presentViewController:imagePickerCon animated:YES completion:nil];

    }

}

#pragma mark - UIImagePickerControllerDelegate

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info

{

//    LogRed(@"%@",info);

    self.imageView.image = info[UIImagePickerControllerOriginalImage];

    [self dismissViewControllerAnimated:YES completion:nil];

}

 
原文地址:https://www.cnblogs.com/guangleijia/p/4835520.html