iOS的蓝牙用到了  CoreBluetooth 框架

首先导入框架

#import <CoreBluetooth/CoreBluetooth.h>

我们需要一个管理者来管理蓝牙设备,CBCentralManager

首先创建管理者

self.manager = [[CBCentralManager alloc]initWithDelegate:self queue:nil options:nil];

这里只需要设置一个代理,队列根据需求来选择,这里用了nil 就是默认在主线程。options是筛选设备的条件。

创建完管理者后  会执行代理方法

- (void)centralManagerDidUpdateState:(CBCentralManager *)central

可以查看manager的一个属性 : state  来识别蓝牙的状态 ,该属性是一个枚举值  CBCentralManagerState

typedef NS_ENUM(NSInteger, CBCentralManagerState) {

CBCentralManagerStateUnknown = 0,   初始的时候是未知的(刚刚创建的时候)

CBCentralManagerStateResetting,   正在重置状态

CBCentralManagerStateUnsupported,   设备不支持的状态

CBCentralManagerStateUnauthorized,      设备未授权状态

CBCentralManagerStatePoweredOff,    设备关闭状态

CBCentralManagerStatePoweredOn,    设备开启状态 -- 可用状态

};

如果是处于  CBCentralManagerStatePoweredOn  状态,那么就可以开始搜索设备   [self.managerscanForPeripheralsWithServices:nil options:nil];

可以设置一个计时器 在一段时间后停止搜索,避免耗电 [self.manager stopScan];

如果搜索到了设备,那么会调用代理方法

- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary<NSString *,id> *)advertisementData RSSI:(NSNumber *)RSSI

其中 peripheral是搜索到的设备,advertisementData是关于设备的一些信息,RSSI是设备的信号强度

可以根据 advertisementData 里面的 kCBAdvDataServiceUUIDs 的值 UUID来筛选某种设备

搜索到设备后就可以进行连接了,蓝牙4.0支持一对多连接。

连接设备    [self.manager connectPeripheral:peripheral options:nil];

连接成功后 会执行代理方法

- (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral

如果需要断开某个设备,则调用方法  [self.manager cancelPeripheralConnection:peripheral];

设备不管是自己断开还是手动断开,都会调用代理方法

- (void)centralManager:(CBCentralManager *)central didDisconnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error

在这里可以判断一下,如果不是手动断开的连接,则可以再连接断开的设备,实现断开设备自动连接的功能

CBCentralManager就是用来做以上这些事情的,要给设备发送数据和接受数据 则要用到 CBPeripheral

首先设置设备的代理

peripheral.delegate = self;

然后开始搜索设备服务    [peripheral discoverServices:nil];

搜索成功后会执行代理方法

- (void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error

在这里 设备的 services 属性里面就有设备服务的数据了,可以遍历这个数组来匹配到我们需要的服务,这个一般跟硬件厂商协商,这里我们假如需要UUID为FF00的服务

如果成功匹配到服务,那么调用方法    [peripheral discoverCharacteristics:nil forService:s];

成功后会执行代理方法

- (void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error

在这个方法里面,服务的属性 characteristics ,会是一组的 CBCharacteristic,是服务的一些特征,在这里,我们假设发送数据需要用到特征的UUID为 FF02

发送的数据为  550504010101AA,我们将其转换为 NSData 类型 调用方法   [peripheral writeValue:dataforCharacteristic:c type:CBCharacteristicWriteWithoutResponse]  将数据发送给设备

发送成功后会执行代理方法

- (void)peripheral:(CBPeripheral *)peripheral didWriteValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error

如果是读取设备的数据 那么使用方法  [peripheral readValueForCharacteristic:c] ,读取数据后会执行代理方法

- (void)peripheral:(CBPeripheral *)peripheral didUpdateValueForCharacteristic:(CBCharacteristic*)characteristic error:(NSError *)error

iOS蓝牙4.0的更多相关文章

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

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

  2. iOS蓝牙4.0开发

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

  3. iOS 蓝牙4.0开发

    背景: 1.iOS的蓝牙不能用来传输文件.2.iOS与iOS设备之间进行数据通信,使用gameKit.framework3.iOS与其他非iOS设备进行数据通信,使用coreBluetooth.fra ...

  4. iOS蓝牙4.0协议简单介绍

    iOS开发蓝牙4.0的框架是CoreBluetooth,本文主要介绍CoreBluetooth的使用,关于本文中的代码片段大多来自github上的一个demo,地址是myz1104/Bluetooth ...

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

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

  6. CoreBluetooth——IOS蓝牙4.0使用心得

    原文链接:http://m.blog.csdn.net/article/details?plg_nld=1&id=51014318&plg_auth=1&plg_uin=1&a ...

  7. iOS蓝牙4.0开发(BLE)

    智能设备 和 app 通过 BLE通讯的两种模型 模型一:设备提供数据,app 展示数据: 比如小米手环 模型二:app提供数据,设备接收: 模型与corebluetooth的对应关系: 模型一:智能 ...

  8. iOS蓝牙4.0开发例子

    1建立中心角色 1 2 3 #import <CoreBluetooth/CoreBluetooth.h>  CBCentralManager *manager;  manager = [ ...

  9. IOS BLE蓝牙4.0

    前言: 自己做的项目里面有这么一个功能,总结归纳一下. 先导入必要的框架  CoreBluetooth.framework 在要用到蓝牙的文件里面导入以下头文件 #import <CoreBlu ...

随机推荐

  1. Hadoop之Storm命令

    Hadoop之Storm命令 1.storm核心概念 stream--->一列火车 tuple--->一节车厢 数据--->乘客 spout--->始发站 bolt---> ...

  2. 大型网站SEO优化策略框架

  3. Bootstrap编码规范

    黄金定律 永远遵循同一套编码规范 -- 可以是这里列出的,也可以是你自己总结的.如果你发现本规范中有任何错误,敬请指正.通过 open an issue on GitHub为本规范添加或贡献内容. 不 ...

  4. POJ3461——Oulipo

    1.题目大意:单字符串匹配问题 2.分析:经典KMP问题 存个模板QAQ #include <cstdio> #include <cstdlib> #include <c ...

  5. Unity手游之路<三> 基于Unity+Java的聊天室源码

    http://blog.csdn.net/janeky/article/details/17233199 项目介绍 这是一个简单的Unity项目,实现最基本的聊天室群聊功能.登录聊天室后,用户可以输入 ...

  6. jquery常用

    获取元素宽度 .innerWidth() // 包含padding .outerWidth() // 包含padding, border .outerWidth(true)//包含padding, b ...

  7. 教你搭建SpringSecurity3框架( 更新中、附源码)

    源码下载地址:http://pan.baidu.com/s/1qWsgIg0 一.web.xml <?xml version="1.0" encoding="UTF ...

  8. div 自动满屏

    通常通过jq来做,类似这样: $('#navigation').css({ height: $(window).innerHeight() }); css3后,只需要用 下面这段样式即可 #navig ...

  9. Eclipse J2EE LUNA 部署tomcat

  10. 【Java MyBatis Generator】使用generator自动生成Dao,Mapping和实体文件

    具体请参照: http://blog.csdn.net/fengshizty/article/details/43086833 按照上面博客地址,下载Generator的依赖包: 如下是我的配置文件: ...