iOS蓝牙篇 GameKit,在iOS7.0之前的设备上的主流开发(必须是同一个应用程序),现已过时,仅用于学习

流程是:

1、显示蓝牙设备选择控制器,设置代理协议

2、实现代理方法

3、发送数据,设置接收函数

GKsession使用




#import "ViewController.h"

#import <GameKit/GameKit.h>


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

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

@property (nonatomic,strong) GKSession *session;

- (IBAction)selectPic:(id)sender;

- (IBAction)sendPic:(id)sender;


@end


@implementation ViewController


- (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view, typically from a nib.

    

    //显示其他蓝牙设备

    GKPeerPickerController *ppController=[[GKPeerPickerController alloc init];

    ppController.delegate=self;

    [ppController show];

    

    

    

}

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

{

    self.session=session;

    [session setDataReceiveHandler:self withContext:nil];

    [picker dismiss];

}


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

{

    self.pic.image=[UIImage imageWithData:data];

    

}


- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


- (IBAction)selectPic:(id)sender {

    

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

    pickerVC.delegate=self;

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

    

}

- (void)imagePickerController:(nonnull UIImagePickerController *)picker didFinishPickingMediaWithInfo:(nonnull NSDictionary<NSString *,id> *)info

{

    UIImage *img=info[UIImagePickerControllerOriginalImage];

    self.pic.image=img;

    [picker dismissViewControllerAnimated:YES completion:nil];

}

-(void)imagePickerControllerDidCancel:(nonnull UIImagePickerController *)picker

{

    [picker dismissViewControllerAnimated:YES completion:nil];

}

- (IBAction)sendPic:(id)sender {

    

    

    UIImage *img=self.pic.image;

    NSData *data=UIImagePNGRepresentation(img);

    [self.session sendDataToAllPeers:data withDataMode:GKSendDataUnreliable error:nil];

    

    

    

    

}

@end


评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值