iOS 蓝牙(GameKit CoreBluetooth)
利用GameKit框架实现ios设备的蓝牙通讯,导入框架:#import <GameKit/GameKit.h> , 注意: 此框架只能用于ios设置间蓝牙通讯
如今苹果开放了接口来实现不仅限于在苹果设备间的蓝牙通讯,需要导入框架:#import <CoreBluetooth/CoreBluetooth.h>
首先,利用GameKit框架实现ios设备蓝牙通讯
基本属性和方法:
属性:
- 是否可见:visible
- 蓝牙连接类型:connectionTypesMask
方法:
- 显示蓝牙控制器:- (void)show;
- 隐藏蓝牙控制器:- (void)dismiss;
- 蓝牙连接成功会调用:- (void)peerPickerController:(GKPeerPickerController *)picker didConnectPeer:(NSString *)peerID toSession:(GKSession *)session
- 取消蓝牙连接:- (void)peerPickerControllerDidCancel:(GKPeerPickerController *)picker
- 接收到数据会调用:- (void) receiveData:(NSData *)data fromPeer:(NSString *)peer inSession: (GKSession *)session context:(void *)context
- 发送数据给特定的对端蓝牙设备:self.session sendData:<#(NSData *)#> toPeers:<#(NSArray *)#> withDataMode:<#(GKSendDataMode)#> error:<#(NSError *__autoreleasing *)#>
- 发送数据给所有蓝牙设备:self.session sendDataToAllPeers:<#(NSData *)#> withDataMode:<#(GKSendDataMode)#> error:<#(NSError *__autoreleasing *)#>
实现:
#import "WYSViewController.h"
#import <GameKit/GameKit.h> @interface WYSViewController () <GKPeerPickerControllerDelegate> // 会话
@property (nonatomic,strong) GKSession *session; @end @implementation WYSViewController - (void)viewDidLoad
{
[super viewDidLoad]; } - (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
} - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{ // 创建蓝牙控制器
GKPeerPickerController *peerPk = [[GKPeerPickerController alloc] init]; // 代理
peerPk.delegate = self; // 显示蓝牙控制器
[peerPk show];
} #pragma mark - 蓝牙代理方法
// 蓝牙连接成功会调用
- (void)peerPickerController:(GKPeerPickerController *)picker didConnectPeer:(NSString *)peerID toSession:(GKSession *)session
{
// 保存回话
self.session = session; // 设置接收者
[self.session setDataReceiveHandler:self withContext:nil]; // 关闭控制器
[picker dismiss];
} // 取消连接调用
- (void)peerPickerControllerDidCancel:(GKPeerPickerController *)picker
{ } // 接收到数据会调用
- (void) receiveData:(NSData *)data fromPeer:(NSString *)peer inSession: (GKSession *)session context:(void *)context
{
NSLog(@"%@",data);
} - (IBAction)sendData
{
// 发送数据去某些蓝牙设备
// NSData * :要发送的数据
// NSArray * : 蓝牙设备
// GKSendDataMode:枚举模式
// self.session sendData:<#(NSData *)#> toPeers:<#(NSArray *)#> withDataMode:<#(GKSendDataMode)#> error:<#(NSError *__autoreleasing *)#> // 发送数据去所有蓝牙设备
// self.session sendDataToAllPeers:<#(NSData *)#> withDataMode:<#(GKSendDataMode)#> error:<#(NSError *__autoreleasing *)#>
}
利用CoreBluetooth实现蓝牙通讯:
基本的思路:创建中心设备-->扫描外设-->连接外设-->扫描外设的服务和特征-->利用特征和外设进行数据交互-->断开连接
基本方法和属性:
属性:
- 中心管理设备类:CBCentralManager
- 中心管理设备代理:id<CBCentralManagerDelegate> delegate
- 中心管理设备状态:CBCentralManagerState state
- 外设类:CBPeripheral
- 服务类:CBService
- 特征类:CBCharacteristic
方法:
- 扫描外设:- (void)scanForPeripheralsWithServices:(NSArray *)serviceUUIDs options:(NSDictionary *)options;
- 停止扫描:- (void)stopScan;
- 连接外设:- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI
- 外设连接成功调用:- (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral
- 外设连接失败调用:- (void)centralManager:(CBCentralManager *)central didDisconnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error
- 扫描到服务就会调用:- (void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error
- 扫描到特征就会调用:- (void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error
具体实现:
#import "WYSViewController.h"
#import <CoreBluetooth/CoreBluetooth.h> @interface WYSViewController () <CBCentralManagerDelegate,CBPeripheralDelegate> // 中心设备
@property (nonatomic,strong) CBCentralManager *cbMgr; // 全部的外部设备
@property (nonatomic,strong) NSMutableArray *Peripherals; @end @implementation WYSViewController // 懒加载
- (NSMutableArray *)Peripherals
{
if (!_Peripherals){ _Peripherals = [NSMutableArray array];
} return _Peripherals;
} - (void)viewDidLoad
{
[super viewDidLoad]; // 创建中心设备
CBCentralManager *cbMgr = [[CBCentralManager alloc] init];
self.cbMgr = cbMgr; // 代理
cbMgr.delegate = self; // 扫描外设
[cbMgr scanForPeripheralsWithServices:nil options:nil];
} #pragma mark - CBCentralManager代理方法
- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI
{
// 扫描到得外设
if (![self.Peripherals containsObject:peripheral]){ // 外设代理
peripheral.delegate = self; [self.Peripherals addObject:peripheral];
}
} // 连接外设
- (IBAction)peripheralConnectSuccess
{
for (CBPeripheral *peripheral in self.Peripherals) { // 连接外设
[self.cbMgr connectPeripheral:peripheral options:nil];
}
} // 外设连接成功会调用
- (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral
{
// 扫描服务
[peripheral discoverServices:nil];
} // 外设连接失败 - (void)centralManager:(CBCentralManager *)central didDisconnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error
{ } #pragma mark - Peripheral代理
// 扫描到服务就会调用
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error
{
NSArray *services = peripheral.services;
for (CBService *service in services) { // 某服务存在
if ([service.UUID.UUIDString isEqualToString:@"xxx"]){ [peripheral discoverCharacteristics:nil forService:service];
}
}
} // 扫描到特征就会调用
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error
{
NSArray *characters = service.characteristics; for (CBCharacteristic *character in characters) { if ([character.UUID.UUIDString isEqualToString:@"xxx"]){ // 进行设置
}
}
}
iOS 蓝牙(GameKit CoreBluetooth)的更多相关文章
- iOS蓝牙开发CoreBluetooth快速入门
在iOS开发中,实现蓝牙通信有两种方式,一种是使用传统的GameKit.framework,另一种就是使用在iOS 5中加入的CoreBluetooth.framework. 利用CoreBlueto ...
- iOS蓝牙4.0开发
文/starfox寒流(简书作者)原文链接:http://www.jianshu.com/p/974d165f78b5著作权归作者所有,转载请联系作者获得授权,并标注“简书作者”. iOS 蓝牙4.0 ...
- iOS蓝牙开发(二)蓝牙相关基础知识
原文链接: http://liuyanwei.jumppo.com/2015/07/17/ios-BLE-1.html iOS蓝牙开发(一)蓝牙相关基础知识: 蓝牙常见名称和缩写 MFI ====== ...
- https://github.com/coolnameismy/BabyBluetooth github上的一个ios 蓝牙4.0的库并带文档和教程
The easiest way to use Bluetooth (BLE )in ios,even bady can use. 简单易用的蓝牙库,基于CoreBluetooth的封装,并兼容ios和 ...
- iOS蓝牙BLE4.0通信功能
概述 iOS蓝牙BLE4.0通信功能,最近刚学的苹果,为了实现蓝牙门锁的项目,找了一天学习了下蓝牙的原理,亲手测试了一次蓝牙的通信功能,结果成功了,那么就把我学习的东西分享一下. 详细 代码下载:ht ...
- ios 蓝牙相关
ios蓝牙开发项目实战 -(附小米手环实例) 前言 最近一直在开发关于蓝牙的功能,本来是不想写这一篇文章,因为网上关于ios蓝牙开发的文章实在太多了,成吨成吨的文章出现,但是很遗憾都只是一些皮 ...
- iOS蓝牙BLE开发
蓝牙是一个标准的无线通讯协议,具有设备成本低.传输距离近和功耗低等特点,被广泛的应用在多种场合.蓝牙一般分为传统蓝牙和BLE两种模式:传统蓝牙可以传输音频等较大数据量,距离近.功耗相对大:而BLE则用 ...
- iOS 蓝牙开发资料记录
一.蓝牙基础认识: 1.iOS蓝牙开发: iOS蓝牙开发:蓝牙连接和数据读写 iOS蓝牙后台运行 iOS关于app连接已配对设备的问题(ancs协议的锅) iOS蓝牙空中 ...
- iOS蓝牙APP常驻后台
iOS蓝牙类APP常驻后台的实现方法,经过在苹果开发者论坛询问,以及查看苹果开发者文档,最后得出正确的方法为: 1.设置plist,蓝牙权限 2.到target-capabilities-backgr ...
随机推荐
- pat05-图3. 六度空间 (30)
05-图3. 六度空间 (30) 时间限制 1500 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard “六度空间”理论又称作“六度分隔(Six Degrees ...
- 搭建MHA
安装MySQL 5.7 yum源的配置文件如下 [mysql57-community] name=MySQL 5.7 Community Server baseurl=http://repo.mysq ...
- GitKraken使用教程-基础部分(6)
4) 放弃本次文件的改动 有些情况下,由于更改代码造成了编译无法通过等错误时,想要放弃这次对文件的修改,将文件还原成上一次提交后的状态,一种简单的恢复文件的方法就是,在Unstaged Files 列 ...
- 【转】android ViewPager,ViewFlipper,ViewFlow实现左右滑动
转自:http://blog.csdn.net/zhouyuanjing/article/details/8290454 开篇 首页只是作为ViewPager,ViewFlipper,ViewFlow ...
- .NET面试题5
常见面试题目: 1. const和readonly有什么区别? 2. 哪些类型可以定义为常量?常量const有什么风险? 3. 字段与属性有什么异同? 4. 静态成员和非静态成员的区别? 5. 自动属 ...
- yield关键字的使用
yield的中文是什么意思呢? 在金山词霸上面的翻译是: vt.屈服,投降: 生产: 获利: 不再反对 vi.放弃,屈服: 生利: 退让,退位 n.产量,产额: 投资的收益: 屈服,击穿: 产品 个人 ...
- flexpager权限控制文件crossdomain.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE cross-domain-policy SY ...
- 前端防御XSS
下面是前端过滤XSS的代码,取自于百度FEX前端团队的Ueditor在线编辑器: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 function xssCheck(str,r ...
- CSS如何居中元素
How to center in CSS 一步步拆解你的需求,是水平居中还是垂直居中?还是水平垂直居中?父容器是inline还是block,高度知不知,宽度造不造?一个子元素还是多个子元素?一行还是多 ...
- ArcMap如何修改地图坐标系统
有时候,地图投影坐标需要作出修改,使得符合要求,不然空间参考不一样无法进行进一步的操作,分析等!下面介绍arcgis地图投影坐标的修改! 1.首先,将地图数据导入,这里我导入的是广西的边界图bound ...