(九十三)蓝牙的基本使用
生活随笔
收集整理的這篇文章主要介紹了
(九十三)蓝牙的基本使用
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
藍牙在GameKit框架中實現(xiàn),可以實現(xiàn)文件傳遞和游戲通信等,藍牙的缺點是不能得到文件傳輸?shù)倪M度,因此不宜傳輸大文件。
使用藍牙的一般步驟如下:
①創(chuàng)建藍牙設備拾取器,類似于圖片拾取器,通過代理方法獲取拾取到的設備,調(diào)用show方法來顯示拾取器。
GKPeerPickerController *peerC = [[GKPeerPickerController alloc] init]; peerC.delegate = self; [peerC show];要遵循UINavigationControllerDelegate,GKPeerPickerControllerDelegate協(xié)議。
②通過代理方法獲取拾取到的設備,存儲這個會話,然后dismiss拾取器視圖,記得要存儲會話,注意為了接收數(shù)據(jù),應該調(diào)用會話的setDataReceiveHandler::方法:
- (void)peerPickerController:(GKPeerPickerController *)picker didConnectPeer:(NSString *)peerID toSession:(GKSession *)session{_session = session;// 設置誰來處理數(shù)據(jù)[session setDataReceiveHandler:self withContext:NULL];[picker dismiss];}
接收數(shù)據(jù)的方法既不是代理,也不是通知,因此必須和幫助文檔中寫的一樣,方法如下:
- (void) receiveData:(NSData *)data fromPeer:(NSString *)peer inSession: (GKSession *)session context:(void *)context{// data就是接收到的二進制數(shù)據(jù)}③要發(fā)送藍牙數(shù)據(jù),通過session的sendDataToAllPeers:::實現(xiàn),其中withDataMode用于選擇是可靠傳輸還是不可靠傳輸,類似TCP和UDP,下面的代碼演示了發(fā)送一張圖片的過程。
- (IBAction)send:(id)sender {NSData *data = UIImagePNGRepresentation(_imageView.image);NSError *err = nil;// 可靠連接可以保證一定送到,不可靠只負責發(fā)送[_session sendDataToAllPeers:data withDataMode:GKSendDataUnreliable error:&err];if (err) {NSLog(@"%@",err);}}
【實例】
下面的例子演示了一個發(fā)送圖片的例程,有一張圖片imageView,三個按鈕,分別是連接(connect方法)、選擇(choosePic)、發(fā)送(send),點擊選擇從照片圖庫中選擇一張圖片,點擊連接建立會話,點擊發(fā)送將圖片通過會話發(fā)送出去。
// // ViewController.m // 藍牙基本使用 // // Created by 11 on 7/27/15. // Copyright (c) 2015 soulghost. All rights reserved. //#import "ViewController.h" #import <GameKit/GameKit.h>@interface ViewController () <UIImagePickerControllerDelegate,UINavigationControllerDelegate,GKPeerPickerControllerDelegate>@property (weak, nonatomic) IBOutlet UIImageView *imageView; @property (weak, nonatomic) GKSession *session;@end@implementation ViewController- (void)viewDidLoad {[super viewDidLoad];}- (IBAction)connect:(id)sender {// 創(chuàng)建藍牙設備選擇器View,設置代理并且顯示、GKPeerPickerController *peerC = [[GKPeerPickerController alloc] init];peerC.delegate = self;[peerC show];} - (IBAction)choosePic:(id)sender {if(![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeSavedPhotosAlbum]){NSLog(@"圖庫不可用");return;}UIImagePickerController *imgPickerC = [[UIImagePickerController alloc] init];imgPickerC.delegate = self;imgPickerC.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;[self presentViewController:imgPickerC animated:YES completion:nil];} - (IBAction)send:(id)sender {NSData *data = UIImagePNGRepresentation(_imageView.image);NSError *err = nil;// 可靠連接可以保證一定送到,不可靠只負責發(fā)送[_session sendDataToAllPeers:data withDataMode:GKSendDataUnreliable error:&err];if (err) {NSLog(@"%@",err);}}- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{//NSLog(@"%@",info);UIImage *image = info[UIImagePickerControllerOriginalImage];_imageView.image = image;[self imagePickerControllerDidCancel:picker];}- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{[picker dismissViewControllerAnimated:YES completion:nil]; }- (void)peerPickerController:(GKPeerPickerController *)picker didConnectPeer:(NSString *)peerID toSession:(GKSession *)session{_session = session;// 設置誰來處理數(shù)據(jù)[session setDataReceiveHandler:self withContext:NULL];[picker dismiss];}// 通過看setDataReceiveHandler的幫助得到,只要實現(xiàn)了即可 // 接收到其他設備傳來的數(shù)據(jù)時調(diào)用 - (void) receiveData:(NSData *)data fromPeer:(NSString *)peer inSession: (GKSession *)session context:(void *)context{UIImage *image = [UIImage imageWithData:data];_imageView.image = image;}@end
轉(zhuǎn)載于:https://www.cnblogs.com/aiwz/p/6154102.html
總結(jié)
以上是生活随笔為你收集整理的(九十三)蓝牙的基本使用的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Oracle 关于事物的描述
- 下一篇: java的数据结构