ios 蓝牙的实现方案
1 GameKit.framework ( ios 7 已经过期了)
2 MultipeerConnectivity.framework (ios 7 开始引入)
3 ExternalAccessory.framework (必须经过苹果的认证就是要给钱)
4 CoreBluetooth.framework ( 时下最热门的 手机要4s ios6 以上 消耗的电量非常少 运动手环,嵌入设备,智能家居)
第一种方式蓝牙实现的代码
// 蓝牙建立连接的方法
- (IBAction)content:(id)sender {
//创建连接设备的控制器
GKPeerPickerController *picker = [[GKPeerPickerController alloc]init];
// 设置代理 picker.delegate =self;
[picker show]; }
// 蓝牙控制器的方法
- (void)peerPickerController:(GKPeerPickerController *)picker didConnectPeer:(NSString *)peerID toSession:(GKSession *)session {
// 保留会话 到后面的发送图片的时候要使用
self.session = session;
// 连接是成功 //
peerID 每个蓝牙的节点id // session 建立会话来传送数据 // 设置句柄(设置了句柄的话系统要求你必须要实现一个方法)
[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;
}];
}
// 发送图片
- (IBAction)sendImage:(id)sender {
NSData *data = UIImageJPEGRepresentation(self.imageView.image, 1.0);
[self.session sendDataToAllPeers:data withDataMode:GKSendDataReliable error:nil];
}
// 接受数据的方法
- (void) receiveData:(NSData *)data fromPeer:(NSString *)peer inSession: (GKSession *)session context:(void *)context{
self.imageView.image = [UIImage imageWithData:data];
}