利用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)的更多相关文章

  1. iOS蓝牙开发CoreBluetooth快速入门

    在iOS开发中,实现蓝牙通信有两种方式,一种是使用传统的GameKit.framework,另一种就是使用在iOS 5中加入的CoreBluetooth.framework. 利用CoreBlueto ...

  2. iOS蓝牙4.0开发

    文/starfox寒流(简书作者)原文链接:http://www.jianshu.com/p/974d165f78b5著作权归作者所有,转载请联系作者获得授权,并标注“简书作者”. iOS 蓝牙4.0 ...

  3. iOS蓝牙开发(二)蓝牙相关基础知识

    原文链接: http://liuyanwei.jumppo.com/2015/07/17/ios-BLE-1.html iOS蓝牙开发(一)蓝牙相关基础知识: 蓝牙常见名称和缩写 MFI ====== ...

  4. https://github.com/coolnameismy/BabyBluetooth github上的一个ios 蓝牙4.0的库并带文档和教程

    The easiest way to use Bluetooth (BLE )in ios,even bady can use. 简单易用的蓝牙库,基于CoreBluetooth的封装,并兼容ios和 ...

  5. iOS蓝牙BLE4.0通信功能

    概述 iOS蓝牙BLE4.0通信功能,最近刚学的苹果,为了实现蓝牙门锁的项目,找了一天学习了下蓝牙的原理,亲手测试了一次蓝牙的通信功能,结果成功了,那么就把我学习的东西分享一下. 详细 代码下载:ht ...

  6. ios 蓝牙相关

      ios蓝牙开发项目实战 -(附小米手环实例)   前言 最近一直在开发关于蓝牙的功能,本来是不想写这一篇文章,因为网上关于ios蓝牙开发的文章实在太多了,成吨成吨的文章出现,但是很遗憾都只是一些皮 ...

  7. iOS蓝牙BLE开发

    蓝牙是一个标准的无线通讯协议,具有设备成本低.传输距离近和功耗低等特点,被广泛的应用在多种场合.蓝牙一般分为传统蓝牙和BLE两种模式:传统蓝牙可以传输音频等较大数据量,距离近.功耗相对大:而BLE则用 ...

  8. iOS 蓝牙开发资料记录

    一.蓝牙基础认识:   1.iOS蓝牙开发:  iOS蓝牙开发:蓝牙连接和数据读写   iOS蓝牙后台运行  iOS关于app连接已配对设备的问题(ancs协议的锅)          iOS蓝牙空中 ...

  9. iOS蓝牙APP常驻后台

    iOS蓝牙类APP常驻后台的实现方法,经过在苹果开发者论坛询问,以及查看苹果开发者文档,最后得出正确的方法为: 1.设置plist,蓝牙权限 2.到target-capabilities-backgr ...

随机推荐

  1. 性能测试工具LoadRunner32-LR之windows性能监控Perfmon

    Perfmon是啥? Perfmon提供了图表化的系统性能实时监视器.性能日志和警报管理,可以用于监视CPU使用率.内存使用率.硬盘读写速度.网络速度等 性能分析方法 内存分析方法 内存分析用于判断系 ...

  2. [转]AngularJS+UI Router(1) 多步表单

    本文转自:https://www.zybuluo.com/dreamapplehappy/note/54448 多步表单的实现   在线demo演示地址https://rawgit.com/dream ...

  3. entity framework discriminator error

    前几天使用code first碰到错误:列名 'Discriminator' 无效.这是一个很少见的错误,搜索了很久才发现是code first的poco实体对象的继承问题. 比如,我定义了一个实体类 ...

  4. Kudu 常见的几个应用场景

    不多说,直接上干货! Kudu 常见的几个应用场景 实时更新的应用.刚刚到达的数据就马上要被终端用户使用访问到. 时间序列相关的应用,需要同时支持: 根据海量历史数据查询. 必须非常快地返回关于单个实 ...

  5. idea编译golang插件总结

    由于使用习惯了Idea 和vim插件.于是下载了idea的go插件并安装,可惜不支持go1.4 ,官方的go插件版本太低 133.326 — 133.9999 .只能手动编译 按照这个教程就可以 ht ...

  6. 深入理解C语言函数指针(转)

    本文转自:http://www.cnblogs.com/windlaughing/archive/2013/04/10/3012012.html 示例1: void myFun(int x); //声 ...

  7. JEECMS站群管理系统-- 首页的加载过程

    在浏览器中输入http://localhost:8080/jeecms,回车 首先进入配置文件web.xml, <context-param> <param-name>cont ...

  8. Quartz使用(1) - 初识quartz

    1. 背景 由于最新的工作项目中,需要使用quartz框架,以完成相关的任务的定时执行.经过两周的调研与使用,本系列博客会参考官网及网上相关博客,结合工作项目中的使用,详细介绍quartz的各个方面. ...

  9. 导出CSV

    public FileResult ExportExcel() { var sbHtml = new StringBuilder(); sbHtml.Append("<table bo ...

  10. VUE中给template组件加背景

    <template> <div class="index_background" > </div> </template> < ...