iOS-藍牙(GameKit)

GameKit框架簡介

1.使用GameKit框架,可以在游戲中增加對等鏈接,又稱對端鏈接或點對點鏈接,Peer To Peer
2.使用GameKit框架中的對等網絡鏈接API,可以在游戲玩家之間建立一個對等網絡,并在游戲/應用實例之間交換數據
3.GameKit框架可以使用藍牙在玩家之間創(chuàng)建網絡,玩家甚至不需要連接到互聯(lián)網,就可以彼此對戰(zhàn)

代碼

//
//  ViewController.m
//  藍牙(GameKit)
//
//  Created by ws on 2017/6/14.
//  Copyright ? 2017年 王松. All rights reserved.
//

#import "ViewController.h"
#import <GameKit/GameKit.h>
@interface ViewController ()<UINavigationControllerDelegate,UIImagePickerControllerDelegate,GKPeerPickerControllerDelegate>
@property (strong, nonatomic) IBOutlet UIImageView *imageView;
/**保留會話用于發(fā)送數據*/
@property(nonatomic,strong)GKSession*session;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
}

#pragma mark 鏈接設備
-(IBAction)connectDevice:(id)sender{
//1.創(chuàng)建查找設備的控制器
    GKPeerPickerController *ppc = [[GKPeerPickerController alloc]init];
//2.設置代理
    ppc.delegate = self;
//3.彈出控制器
    [ppc show];
}
#pragma mark 鏈接成功后會調用此方法
/**
 鏈接成功后會調用此方法
 @param peerID 設備節(jié)點ID
 @param session 會話(使用該會話對象來相互傳輸數據)
 */
- (void)peerPickerController:(GKPeerPickerController *)picker didConnectPeer:(NSString *)peerID toSession:(GKSession *)session{
    //NSLog(@"---%@",peerID);
    //1.保留會話
    self.session = session;
    //2.設置數據接受者
    [self.session setDataReceiveHandler:self withContext:nil];
    
    //2.退出選擇設備的控制器
    [picker dismiss];
}

#pragma mark 選擇照片
-(IBAction)pickImage{
/**
 UIImagePickerControllerSourceTypePhotoLibrary 圖片庫
 UIImagePickerControllerSourceTypeCamera 相機
 UIImagePickerControllerSourceTypeSavedPhotosAlbum 相冊
 */
//1.判斷照片源是否可以
    if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeSavedPhotosAlbum])return;
//2.創(chuàng)建照片選擇控制器
    UIImagePickerController *ipc = [[UIImagePickerController alloc]init];
//3.設置照片源
    ipc.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
//4.設置代理 獲取照片
    ipc.delegate = self;
//5.彈出控制器
    [self presentViewController:ipc animated:YES completion:nil];
}
#pragma mark 當選中某一個照片的時候會調用這個代理方法
/**
 當選中某一個照片的時候會調用這個代理方法
 @param info 存放著照片信息
 */
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
    // 1.取出照片
    self.imageView.image = info[UIImagePickerControllerOriginalImage];
    //2.退出控制器
    [picker dismissViewControllerAnimated:YES completion:nil];
}

#pragma mark 發(fā)送照片
-(IBAction)sendImage{
    //0.判斷image如果為空直接返回
    if (!self.imageView.image) return;

    //1.將UIImage轉化成NSData
   //NSData *data = UIImagePNGRepresentation(self.imageView.image);
    NSData *data = UIImageJPEGRepresentation(self.imageView.image, 0.5);//參數2:表示壓縮質量
    //2.發(fā)送照片
    /**
     GKSendDataReliable //可靠傳輸(數據一定會被傳達,如果中間網絡放生狀況,會重新連接,再次傳輸)-->相當于TCP(數據傳輸通道)
     GKSendDataUnreliable,//不可靠傳輸(數據只會發(fā)送一次,如果中間網絡放生狀況,沒有收到就算啦)-->相當于UDP(數據傳輸通道)
     */
    [self.session sendDataToAllPeers:data withDataMode:GKSendDataReliable error:nil];
    
        //self.session sendData:<#(NSData *)#> toPeers:(NSArray *) withDataMode:<#(GKSendDataMode)#> error:<#(NSError *__autoreleasing *)#>
}

#pragma mark 接受數據
/**
 當接受到數據的時候會調用該方法
 @param data 接受到的數據
 @param peer 從哪一個節(jié)點接受到的數據
 @param session 會話
 */
- (void) receiveData:(NSData *)data fromPeer:(NSString *)peer inSession: (GKSession *)session context:(void *)context{
    //1.將NSData轉換成UIImage對象
    UIImage *receiveImage = [UIImage imageWithData:data];
    //2.設置到imageView上
    self.imageView.image = receiveImage;
    //3.將圖片保存到相冊
    UIImageWriteToSavedPhotosAlbum(receiveImage, nil, nil, nil);
}
@end

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

  • iOS中提供了4個框架用于實現藍牙連接 GameKit.framework(用法簡單)只能用于iOS設備之間的連接...
    biyuhuaping閱讀 602評論 0 2
  • Guide to BluetoothSecurity原文 本出版物可免費從以下網址獲得:https://doi.o...
    公子小水閱讀 8,767評論 0 6
  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 179,007評論 25 709
  • 一曲悠揚一曲歌 盛夏清風送蹉跎 瑤池風雨無覓處 獨遇夢里一葉荷 碧玉嬌羞傾絕世 清香縷縷斷塵埃 唯有小荷翹首盼 含...
    許之日閱讀 658評論 8 3
  • 作者:謝楓華 動畫監(jiān)督宮地昌幸(千與千尋監(jiān)督助手、亡念的扎姆德監(jiān)督)作為嘉賓出席TBS廣播『荻上チキ?Sessio...
    AnimeTamashii閱讀 636評論 0 1

友情鏈接更多精彩內容