iOS蓝牙开发(一):蓝牙相关基础知识

作者 john 日期 2017-07-10 阅读量
iOS蓝牙开发(一):蓝牙相关基础知识

蓝牙常见词汇

MFI :苹果认证的设备
BLE :bluetooth low energy 说明蓝牙4.0设备明显的特点是耗电低
Central :中心,发起连接的设备
Peripheral :外设,被连接的设备
Service :服务,一个外设有多个服务,类似于服务端的API
Characteristic :特征,一个服务有多个特征,特征的权限有read、write、notify几种
Description :描述Characteristic的信息或属性,每个Characteristic可以对应多个Description
APP中常见的是中心模式,即手机作为Central
对于iOS BLE开发,用CoreBluetooth框架、开发MFI,用ExternalAccessory框架

中心模式流程

1、建立中心管理centralManager
2、扫描外设discoverPeripheral
3、连接外设connectPeripheral
4、扫描外设中的Service
5、扫描Service中的Characteristic
6、获取Characteristic的值value以及Description
7、与外设做数据交互,write、read/notify订阅
       read:主动获取数据;notify:外设主动发送通知
*– notify方式:

*-- 
*-- 订阅成功后回调didUpdateNotificationStateForCharacteristic
*-- 订阅后characteristic的值发生变化会发送通知到didUpdateValueForCharacteristic
*-- 取消订阅:设置setNotifyValue为NO
[Peripheral setNotifyValue:YES forCharacteristic:characteristic];
read方式:
[Peripheral readValueForCharacteristic:characteristic];

8、断开连接disConnectPeripheral

蓝牙设备状态

1、待机状态 standby:设备没有接受和发送数据,没有连接到任何central
2、广播状态 advertiser:周期性广播状态
3、扫描状态 scanner:主动寻找正在广播的设备
4、发起连接 initiator:主动向扫描设备发起连接
5、主设备 master:作为主设备连接到其他设备
6、从设备 slave:作为从设备连接到其他设备

蓝牙设备的工作状态

准备 standby
广播 advertising
监听扫描 scanning
发起连接 initiating
已连接 connected

iOS蓝牙中心模式实现

见下一篇文章