(转)CBCentralManager Class 的相关分析
本文转自:http://blog.csdn.net/feixiang_song/article/details/17395811
Overview
CBCentralManager
objects are used to manage discovered or connected remote peripheral devices (represented by CBPeripheral
objects), including scanning for, discovering, and connecting to advertising peripherals.
CBCentralManager
对象负责管理外设的发现或连接,包括扫描、发现、连接正在广播的外设。
Before you call CBCentralManager
methods, the state of the central manager object must be powered on, as indicated by theCBCentralManagerStatePoweredOn
constant. This state indicates that the central device (your iPhone or iPad, for instance) supports Bluetooth low energy and that Bluetooth is on and available to use.
CBCentralManager
方法前,我们必须查看中央管理器的状态是否为打开状态(CBCentralManagerStatePoweredOn)。如果是该状态,表明该中心设备(你的iphone或者ipad)是支持蓝牙低功耗4.0的。Tasks 任务
Initializing a Central Manager 初始化中央管理器
– initWithDelegate:queue:
– initWithDelegate:queue:options:
- - (id)initWithDelegate:(id<CBCentralManagerDelegate>)delegate queue:(dispatch_queue_t)queue options:(NSDictionary *)options
- 方法说明:delegate代理:接受中心事件 queue:指明在哪个队列处理事件,为nil时表示在主线程处理
- options:有一下两个选择
-
NSString *const CBCentralManagerOptionShowPowerAlertKey;用该参量出事中央管理器时,当蓝牙开关未打开时会弹出警告框。 NSString *const CBCentralManagerOptionRestoreIdentifierKey;该参量包含一个指定中央管理器的uid
Establishing or Canceling Connections with Peripherals 连接外设 或者 取消外设连接
– connectPeripheral:options:
- - (void)connectPeripheral:(CBPeripheral *)peripheral options:(NSDictionary *)options
- 参数说明:
- peripheral:中央管理器想要连接的外设对象
- options: 还没搞明白
NSString *const CBConnectPeripheralOptionNotifyOnConnectionKey; NSString *const CBConnectPeripheralOptionNotifyOnDisconnectionKey; NSString *const CBConnectPeripheralOptionNotifyOnNotificationKey;
讨论:当连接外设成功时要调用在委托对象中实现的方法:centralManager:didConnectPeripheral:
- 当连接外设失败时要调用在委托对象中实现的方法:
centralManager:didFailToConnectPeripheral:error:
– cancelPeripheralConnection:
- - (void)cancelPeripheralConnection:(CBPeripheral *)peripheral
- 取消外设连接
- 该方法调用后会触发委托中的方法:centralManager:didDisconnectPeripheral:error:
Retrieving Lists of Peripherals 整理出外设列表
– retrieveConnectedPeripheralsWithServices:
- - (NSArray *)retrieveConnectedPeripheralsWithServices:(NSArray *)serviceUUIDs
- 参数说明:
- serviceUUIDs 表示服务uuids的列表
- 返回值:从连接的设备(可能有多个app连接不同的蓝牙设备)中,根据服务列表,返回这些服务的数组(也就是找到我们指定的服务设备,整理到一个数组中返回)
– retrievePeripheralsWithIdentifiers:
- - (NSArray *)retrievePeripheralsWithIdentifiers:(NSArray *)identifiers
- 和上一个相识,只不过这里是指定的标示符数组
– retrieveConnectedPeripherals
Deprecated in iOS 7.0- 该方法整理出所有连接到中央设备(比如iphone)的外设,返回一个数组(该数组包含了所有app连接的外设列表)
- 该方法会触发调用委托中的方法
centralManager:didRetrieveConnectedPeripherals:
– retrievePeripherals:
Deprecated in iOS 7.0
Scanning or Stopping Scans of Peripherals 扫描或者停止扫描外设
– scanForPeripheralsWithServices:options:
- - (void)scanForPeripheralsWithServices:(NSArray *)serviceUUIDs options:(NSDictionary *)options
- 参数说明:
- serviceUUIDs:代表该app所感兴趣的服务uuids数组(也就是该app想要连接的外设)
- 每当扫描到一个外设,就会调用委托对象中的方法:
centralManager:didDiscoverPeripheral:advertisementData:RSSI:
– stopScan 停止扫描
Monitoring Properties
-
state
property - 返回一个central manager的当前状态,
-
Discussion 讨论
When a central manager object is initially created, the default value of this property is
CBCentralManagerStateUnknown
.当一个中央管理器对象初始化时,他的默认值是CBCentralManagerStateUnknown
As the central manager’s state changes, the central manager updates the value of this property and calls the
centralManagerDidUpdateState:
当中央管理器状态改变时,会调用centralManagerDidUpdateState:委托方法
delegate
propertyCentral Manager State中央管理器状态
Values representing the current state of a central manager object.
typedef enum { CBCentralManagerStateUnknown = 0, 未知 CBCentralManagerStateResetting, 复位 CBCentralManagerStateUnsupported, 表明设备不支持蓝牙低功耗 CBCentralManagerStateUnauthorized,该应用程序是无权使用蓝牙低功耗。 CBCentralManagerStatePoweredOff, 关闭状态 CBCentralManagerStatePoweredOn, 打开状态 } CBCentralManagerState
Central Manager State
Values representing the current state of a central manager object.
typedef enum { CBCentralManagerStateUnknown = 0, CBCentralManagerStateResetting, CBCentralManagerStateUnsupported, CBCentralManagerStateUnauthorized, CBCentralManagerStatePowe