iOS 蓝牙的开发专题(总)

[2016-08-02]

最近做一个iOS蓝牙设备的项目,需要用iOS连接外部蓝牙设备,就对iOS蓝牙相关技术做了一些研究,主要分为下面几个专题

一、蓝牙相关基础知识

-蓝牙常见名称和缩写
-蓝牙基础知识
-蓝牙和版本的使用限制

1-蓝牙常见名称和缩写

MFI ======= make for ipad ,iphone, itouch 专们为苹果设备制作的设备
BLE ==== buletouch low energy,蓝牙4.0设备因为低耗电,所以也叫做BLE
peripheral,central == 外设和中心,发起连接的是central,被连接的设备为perilheral
service and characteristic === 服务和特征 每个设备会提供服务和特征,类似于服务端的api,但是机构不同。每个外设会有很多服务,每个服务中包含很多字段,这些字段的权限一般分为 读read,写write,通知notiy几种,就是我们连接设备后具体需要操作的内容。
Description 每个characteristic可以对应一个或多个Description用户描述characteristic的信息或属性
MFI === 开发使用ExternalAccessory 框架
4.0 BLE === 开发使用CoreBluetooth 框架

2-蓝牙基础知识

CoreBluetooth框架的核心其实是两个东西,peripheral和central, 可以理解成外设和中心。对应他们分别有一组相关的API和类

CoreBluetoothFramework

//objcetive c特征的定义枚举
  typedef NS_OPTIONS(NSUInteger, CBCharacteristicProperties) {
      CBCharacteristicPropertyBroadcast											= 0x01,
      CBCharacteristicPropertyRead												= 0x02,
      CBCharacteristicPropertyWriteWithoutResponse		   					   	= 0x04,
      CBCharacteristicPropertyWrite												= 0x08,
      CBCharacteristicPropertyNotify											= 0x10,
      CBCharacteristicPropertyIndicate											= 0x20,
      CBCharacteristicPropertyAuthenticatedSignedWrites							  = 0x40,
      CBCharacteristicPropertyExtendedProperties								 = 0x80,
      CBCharacteristicPropertyNotifyEncryptionRequired NS_ENUM_AVAILABLE(NA, 6_0)   = 0x100,
      CBCharacteristicPropertyIndicateEncryptionRequired NS_ENUM_AVAILABLE(NA, 6_0)	= 0x200
  };

外设、服务、特征间的关系

TreeOfServicesAndCharacteristics_Remote_2x

蓝牙中心模式流程

- 建立中心角色
- 扫描外设(discover)
- 扫描外设中的服务和特征(discover)
		-获取外设的services
		-获取外设的Characteristics,获取Characteris的值,获取Characteristics的Descriptor和Descritor的值
- 与外设做数据交互(explore and interact)
- 订阅Characteristic的通知
- 断开连接(disconnect)

蓝牙外设模式流程

- 启动一个Peripheral管理对象
- 本地Peripheral设置服务,特性,描述,权限等等
- Peripheral发送广告
- 设置处理订阅、取消订阅、读characteristic、写characteristic的委托方法

蓝牙设备状态

1. 待机状态(standby):设备没有传输和发送数据,并且没有连接到任何设备
2. 广播状态(Advertiser):周期性广播状态
3. 扫描状态(Scanner):主动寻找正在广播的设备
4. 发起链接状态(Initiator):主动向扫描设备发起连接。
5. 主设备(Master):作为主设备连接到其他设备。
6. 从设备(Slave):作为从设备连接到其他设备。

蓝牙设备的五种工作状态

1. 准备(standby)
2. 广播(advertising)
3. 监听扫描(Scanning
4. 发起连接(Initiating)
5. 已连接(Connected)

名词解释

3-蓝牙和版本的使用限制

蓝牙2.0 === 越狱设备
蓝牙4.0 === IOS6 以上
MFI认证设备(Make For ipod/ipad/iphone) === 无限制

二、iOS连接外设的代码实现

-建立中心角色
-扫描外设(discover)
-扫描外设中的服务和特征(discover)
		-获取外设的services
		-获取外设的Characteristics,获取Characteris的值,获取Characteristics的Descriptor和Descritor的值
-与外设做数据交互(explore and interact)
-订阅Characteristic的通知
-断开连接(disconnect)

三、app作为外设被连接的实现

-打开peripheralManager,设置peripheralManager的委托
-创建characteristics,characteristics的descrition创建service,把characteristics添加到service中,再把service添加到peripheralManager中
-开启广播advertising
-对central的操作进行响应
		-读characteristics请求
		-写characteristics请求
		-订阅和取消订阅characteristics

四、BabyBluetooth蓝牙库

-CoreBluetooth有哪些问题
-BabyBluetooth优点和快速使用

From where

BackHome