iOS 藍(lán)牙實現(xiàn)照片的傳遞

ios 藍(lán)牙的實現(xiàn)方案

1 GameKit.framework ( ios 7 已經(jīng)過期了)

2 MultipeerConnectivity.framework (ios 7 開始引入)

3 ExternalAccessory.framework (必須經(jīng)過蘋果的認(rèn)證就是要給錢)

4 CoreBluetooth.framework ( 時下最熱門的 手機(jī)要4s ios6 以上 消耗的電量非常少 運動手環(huán),嵌入設(shè)備,智能家居)? ?

?第一種方式藍(lán)牙實現(xiàn)的代碼

// 藍(lán)牙建立連接的方法

- (IBAction)content:(id)sender {? ??

? ? //創(chuàng)建連接設(shè)備的控制器? ?

?GKPeerPickerController *picker = [[GKPeerPickerController alloc]init];? ?

?//? ? 設(shè)置代理? ? picker.delegate =self;? ??

[picker show]; }

// 藍(lán)牙控制器的方法

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

// 保留會話 到后面的發(fā)送圖片的時候要使用??

? self.session = session;? ?

?// 連接是成功? ? //? ?

?? peerID 每個藍(lán)牙的節(jié)點id? ? //? ? session? 建立會話來傳送數(shù)據(jù)? ? // 設(shè)置句柄(設(shè)置了句柄的話系統(tǒng)要求你必須要實現(xiàn)一個方法)? ?

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

?? ? [picker dismiss];}

// 選擇照片

- (IBAction)choseImage:(id)sender {? ? if([UIImagePickerControll

er isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]);?

? UIImagePickerController *imagePicker? = [[UIImagePickerController alloc]init];??

? imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;? ? [self presentViewController:imagePicker animated:YES completion:^{? ? ? ? ? ? }];??

? imagePicker.delegate = self;??

}

//選擇照片的代理方法

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

NSLog(@"%@",info);

self.imageView.image = info[UIImagePickerControllerOriginalImage];

[self dismissViewControllerAnimated:YES completion:^{

nil;

}];

}

// 發(fā)送圖片

- (IBAction)sendImage:(id)sender {

NSData *data = UIImageJPEGRepresentation(self.imageView.image, 1.0);

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

}

// 接受數(shù)據(jù)的方法

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

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

}

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

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容