本文主要是对蓝牙功能api的介绍及学习 原文地址:http://blog.csdn.net/dwarven/article/details/37873663

一:CBPeripheralManager

@interface CBPeripheralManager : NSObject

//CBPeripheralManager的几种状态

typedef NS_ENUM(NSInteger, CBCentralManagerState) {

// 初始的时候是未知的(刚刚创建的时候)

CBCentralManagerStateUnknown = 0,

//正在重置状态

CBCentralManagerStateResetting,

//设备不支持的状态

CBCentralManagerStateUnsupported,

// 设备未授权状态

CBCentralManagerStateUnauthorized,

//设备关闭状态

CBCentralManagerStatePoweredOff,

// 设备开启状态 -- 可用状态

CBCentralManagerStatePoweredOn,

}; 

 

// 设置代理 一般就是当前类

@property(weak, nonatomic) id<CBPeripheralManagerDelegate> delegate;

 

// CBPeripheral类实例当前状态

@property(readonly) CBPeripheralManagerState state;

 

//当前是否正在广播数据

@property(readonly) BOOL isAdvertising;

 

//蓝牙设备授权状态

// 授权状态不确定 未知

CBPeripheralManagerAuthorizationStatusNotDetermined = 0,

// 授权状态是受限制的 

CBPeripheralManagerAuthorizationStatusRestricted,

// 授权状态是拒绝的 (未授权)

CBPeripheralManagerAuthorizationStatusDenied,

// 授权状态是已授权

CBPeripheralManagerAuthorizationStatusAuthorized,

+ (CBPeripheralManagerAuthorizationStatus)authorizationStatus

 

//创建  如果queue为nil那么就是在主线程中使用

- (id)initWithDelegate:(id<CBPeripheralManagerDelegate>)delegate queue:(dispatch_queue_t)queue;

 

//相较于第一个创建方法多了一个可选项options

//其中options里面有两个key值

//CBPeripheralManagerOptionRestoreIdentifierKey

----对应的值是一个字典(数组)创建一个CBPeripheralManager的一个实例时从options中取出值去恢复Peripheral的状态

//CBPeripheralManagerOptionShowPowerAlertKey

----对应的值是一个NSNumber类型BOOL值,它标识了在系统peripheral创建在蓝牙关闭的情况下是否应该显示一个警告对话框

- (id)initWithDelegate:(id<CBPeripheralManagerDelegate>)delegate queue:(dispatch_queue_t)queue options:(NSDictionary *)options

 

//advertisementData包含了你想要广播的数据,当广播开启的时候 peripheral会调用他的代理方法-(void)peripheralManagerDidStartAdvertising: error:

 

- (void)startAdvertising:(NSDictionary *)advertisementData;

 

// 停止广播

- (void)stopAdvertising

 

// 设置一个延时for central 

//CBPeripheralManagerConnectionLatency是一个枚举:

CBPeripheralManagerConnectionLatencyLow 低连接延时,

CBPeripheralManagerConnectionLatencyMedium 中等连接延时,

CBPeripheralManagerConnectionLatencyHigh 高连接延时

- (void)setDesiredConnectionLatency:(CBPeripheralManagerConnectionLatency)latency forCentral:(CBCentral *)central

 

//添加一个service和与这个service相关联的characteristic到local database,如果他们已经存在他们必须首先被发布

- (void)addService:(CBMutableService *)service;

 

//冲local database移除一个已经发布的服务,如果这个服务包含了其他服务,那么必须先移除前者

- (void)removeService:(CBMutableService *)service;

 

//移除所有已经发布的服务service

- (void)removeAllServices;

 

//响应一个从central传过来读或者写请求

//响应已连接的central的读写请求,当peripheral接收到central的读或者写的 characteristic 的 value时候peripheral会回调peripheralManager:didReceiveReadRequest:或者peripheralManager:didReceiveWriteRequest:

- (void)respondToRequest:(CBATTRequest *)request withResult:(CBATTError)result;

 

//为订阅了peripheral的central更新characteristic里面的值

- (BOOL)updateValue:(NSData *)value forCharacteristic:(CBMutableCharacteristic *)characteristic onSubscribedCentrals:(NSArray *)centrals

 

******************************代理方法***********************************

@protocol CBPeripheralManagerDelegate <NSObject>

 

@required

 

//更新状态 ,只有状态可用的时候才能够进行创建服务,发布等等操作

//状态和CBCentralManager一样

- (void)peripheralManagerDidUpdateState:(CBPeripheralManager*)peripheral

 

@optional

 

//peripheral提供信息,dict包含了应用程序关闭是系统保存的peripheral的信息,用dic去恢复peripheral

//app状态的保存或者恢复,这是第一个被调用的方法当APP进入后台去完成一些蓝牙有关的工作设置,使用这个方法同步app状态通过蓝牙系统

//dic里面有两对key值分别对应服务(数组)和数据(数组)

- (void)peripheralManager:(CBPeripheralManager *)peripheral willRestoreState:(NSDictionary *)dict;

 

// 开始向外广播数据  当startAdvertising被执行的时候调用这个代理方法

- (void)peripheralManagerDidStartAdvertising:(CBPeripheralManager *)peripheral error:(NSError *)error

 

// 当你执行addService方法后执行如下回调,当你发布一个服务和任何一个相关特征的描述到GATI数据库的时候执行

- (void)peripheralManager:(CBPeripheralManager *)peripheral didAddService:(CBService *)service error:(NSError *)error

 

 

//central订阅了characteristic的值,当更新值的时候peripheral会调用【updateValue: forCharacteristic: onSubscribedCentrals:(NSArray*)centrals】去为数组里面的centrals更新对应characteristic的值,在更新过后peripheral为每一个central走一遍改代理方法

- (void)peripheralManager:(CBPeripheralManager *)peripheral central:(CBCentral *)central didSubscribeToCharacteristic:(CBCharacteristic *)characteristic

 

 

//当central取消订阅characteristic这个特征的值后调用方法。使用这个方法提示停止为这个central发送更新

- (void)peripheralManager:(CBPeripheralManager *)peripheral central:(CBCentral *)central didUnsubscribeFromCharacteristic:(CBCharacteristic *)characteristic

 

 

//当peripheral接受到一个读ATT读请求,数据在CBATTRequest

- (void)peripheralManager:(CBPeripheralManager *)peripheral didReceiveReadRequest:(CBATTRequest *)request

 

//当peripheral接受到一个写请求的时候调用,参数有一个数组的CBATTRequest对象request

- (void)peripheralManager:(CBPeripheralManager *)peripheral didReceiveWriteRequests:(NSArray *)requests

 

 

//peripheral再次准备好发送Characteristic值的更新时候调用

//当updateValue: forCharacteristic:onSubscribedCentrals:方法调用因为底层用于传输Characteristic值更新的队列满了而更新失败的时候,实现这个委托再次发送改值

- (void)peripheralManagerIsReadyToUpdateSubscribers:(CBPeripheralManager *)peripheral

 

二:CBATTRequest

/*!

 *  @class CBATTRequest

 *  @discussion :代表了一个从中央发出的读或者写请求

 */

 

@interface CBATTRequest : NSObject

 

// 发送请求的中央

@property(readonly, retain, nonatomic) CBCentral *central;

 

// 将被读或者写的对应的characteristic

@property(readonly, retain, nonatomic) CBCharacteristic*characteristic

 

// 读或者写的首字节索引,指读写的偏移量

@property(readonly, nonatomic) NSUInteger offset;

 

// 正在被读写的数据,这个属性的值取决于不用的请求类型,对于读请求它的值在 respondToRequest:withResult:方法被调用之前被设置。

@property(readwrite, copy) NSData *value;

 

三:CBCharacteristic-CBMutableChaeacteristic

CBCharacteristic学习笔记

/*!

 *  @class CBCharacteristic

 *  @discussion

 *      代表一个服务的特征

 */

@interface CBCharacteristic : NSObject

 

//表明这个属于哪个service

@property(weak, readonly, nonatomic) CBService *service;

 

//characteristic的UUID标识

@property(readonly, nonatomic) CBUUID *UUID;

 

// characteristic的属性,是一个结构体

 

typedef NS_OPTIONS(NSInteger, CBCharacteristicProperties) {

// 标识这个characteristic的属性是广播

CBCharacteristicPropertyBroadcast= 0x01,

// 标识这个characteristic的属性是读

CBCharacteristicPropertyRead= 0x02,

// 标识这个characteristic的属性是写-没有响应

CBCharacteristicPropertyWriteWithoutResponse= 0x04,

// 标识这个characteristic的属性是写

CBCharacteristicPropertyWrite= 0x08,

// 标识这个characteristic的属性是通知

CBCharacteristicPropertyNotify= 0x10,

// 标识这个characteristic的属性是声明

CBCharacteristicPropertyIndicate= 0x20,

// 标识这个characteristic的属性是通过验证的

CBCharacteristicPropertyAuthenticatedSignedWrites= 0x40,

// 标识这个characteristic的属性是拓展

CBCharacteristicPropertyExtendedProperties= 0x80,

// 标识这个characteristic的属性是需要加密的通知

CBCharacteristicPropertyNotifyEncryptionRequiredNS_ENUM_AVAILABLE(NA, 6_0)= 0x100,

// 标识这个characteristic的属性是需要加密的申明

CBCharacteristicPropertyIndicateEncryptionRequiredNS_ENUM_AVAILABLE(NA, 6_0)= 0x200

};

 

@property(readonly, nonatomic) CBCharacteristicPropertiesproperties;

 

// characteristic的值

@property(retain, readonly) NSData *value;

 

// * *

@property(retain, readonly) NSArray *descriptors;

 

// 特征是否正在广播

@property(readonly) BOOL isBroadcasted;

 

// 特征是否正在通知

@property(readonly) BOOL isNotifying;

 

 

 

@interface CBMutableCharacteristic : CBCharacteristic

 

// 权限

typedef NS_OPTIONS(NSInteger, CBAttributePermissions) {

// 可读的

CBAttributePermissionsReadable= 0x01,

// 可写的

CBAttributePermissionsWriteable= 0x02,

// 需验证

CBAttributePermissionsReadEncryptionRequired= 0x04,

CBAttributePermissionsWriteEncryptionRequired= 0x08

}

@property(assign, readwrite, nonatomic) CBAttributePermissionspermissions;

 

//订阅这个characteristic的central

@property(retain, readonly) NSArray *subscribedCentrals

 

@property(retain, readwrite, nonatomic) CBUUID *UUID;

 

@property(assign, readwrite, nonatomic)CBCharacteristicProperties properties;

 

@property(retain, readwrite) NSData *value;

 

@property(retain, readwrite) NSArray *descriptors;

 

- (id)initWithType:(CBUUID *)UUID properties:(CBCharacteristicProperties)properties value:(NSData *)value permissions:(CBAttributePermissions)permissions;

 

四:CBCenteralManager

@interface CBCentralManager : NSObject

 

//CBCentralManager的几种状态

 

typedef NS_ENUM(NSInteger, CBCentralManagerState) {

// 初始的时候是未知的(刚刚创建的时候)

CBCentralManagerStateUnknown = 0,

//正在重置状态

CBCentralManagerStateResetting,

//设备不支持的状态

CBCentralManagerStateUnsupported,

// 设备未授权状态

CBCentralManagerStateUnauthorized,

//设备关闭状态

CBCentralManagerStatePoweredOff,

// 设备开启状态 -- 可用状态

CBCentralManagerStatePoweredOn,

};

 

// 代理

@property(weak, nonatomic) id<CBCentralManagerDelegate> delegate;

//设备的状态

@property(readonly) CBCentralManagerState state;

// 初始化方法-参数-delegate-可以放到多线程里面去创建做更多的事情-queue

- (id)initWithDelegate:(id<CBCentralManagerDelegate>)delegate queue:(dispatch_queue_t)queue;

//初始化方法-参数-options指定这个manager

- (id)initWithDelegate:(id<CBCentralManagerDelegate>)delegate queue:(dispatch_queue_t)queue options:(NSDictionary *)options

//检索外围设备通过传入一个UUID数组

- (void)retrievePeripherals:(NSArray *)peripheralUUIDs 

//检索外围设备通过传入一个identifiers数组

- (NSArray *)retrievePeripheralsWithIdentifiers:(NSArray*)identifiers

// 检索已连接的外围设备

- (void)retrieveConnectedPeripherals

//通过服务的UUID数组返回已经连接的服务数组

- (NSArray *)retrieveConnectedPeripheralsWithServices:(NSArray*)serviceUUIDs

// serviceUUIDs是一个CBUUID数组,如果serviceUUIDs是nil为空的话所有的被发现的peripheral将要被返回

- (void)scanForPeripheralsWithServices:(NSArray *)serviceUUIDs options:(NSDictionary *)options

// 停止扫描

- (void)stopScan

//通过options指定的选项去连接peripheral

- (void)connectPeripheral:(CBPeripheral *)peripheral options:(NSDictionary *)options

//取消一个活跃的或者等待连接的peripheral的连接

- (void)cancelPeripheralConnection:(CBPeripheral *)peripheral

 

-----------------------------------------------------代理方法------------------------------------------------------------------

@protocol CBCentralManagerDelegate <NSObject>

// 必须实现的方法

@required

//仅仅在CBCentralManagerStatePoweredOn的时候可用当central的状态是OFF的时候所有与中心连接的peripheral都将无效并且都要重新连接,central的初始状态时是Unknown

- (void)centralManagerDidUpdateState:(CBCentralManager *)central;

 

@optional

//central提供信息,dict包含了应用程序关闭是系统保存的central的信息,用dic去恢复central

//app状态的保存或者恢复,这是第一个被调用的方法当APP进入后台去完成一些蓝牙有关的工作设置,使用这个方法同步app状态通过蓝牙系统

- (void)centralManager:(CBCentralManager *)central willRestoreState:(NSDictionary *)dict

 

//*  @param central      The central manager providing this information.

 *  @param peripherals  A list of <code>CBPeripheral</code> objects.

改方法返回一个结果当{@link retrievePeripherals} 被调用,与周边,中央能够匹配UUID

- (void)centralManager:(CBCentralManager *)central didRetrievePeripherals:(NSArray *)peripherals

 

//peripherals表示当前连接central的所有peripherals

//这个方法返回一个结果,当retrieveConnectedPeripherals被调用是

- (void)centralManager:(CBCentralManager *)central didRetrieveConnectedPeripherals:(NSArray *)peripherals

 

- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI

 

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

 

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

 

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

 

 五:CBService-CBMutableService

@interface CBService : NSObject

 

//表明服务属于哪个peripheral,一个peripheral可以有多个服务

@property(weak, readonly, nonatomic) CBPeripheral *peripheral;

 

//服务的UUID标识

@property(readonly, nonatomic) CBUUID *UUID;

 

//标识CBService是主要的服务还是次要的。

//解释:一个周边peripheral的服务可以是主要的也可以是次要的。主要的服务描述了设备的主要功能并且可以包含另外一个服务。次要服务描述了服务唯一相关和另一个服务额应用。如心率检测器的主要服务是显示心率监测数据,而第二个服务可能是显示电池数据,如果属性是Yes那么服务就是主要类型

@property(readonly, nonatomic) BOOL isPrimary;

 

//服务可能包含服务,那么includedServices就是服务所包含的服务(层级关系)

@property(retain, readonly) NSArray *includedServices;

 

//包含服务的characteristic

@property(retain, readonly) NSArray *characteristics;

 

 

 

 

@interface CBMutableService : CBService

 

@property(retain, readwrite, nonatomic) CBUUID *UUID;

@property(readwrite, nonatomic) BOOL isPrimary;

@property(retain, readwrite) NSArray *includedServices;

@property(retain, readwrite) NSArray *characteristics;

 

- (id)initWithType:(CBUUID *)UUID primary:(BOOL)isPrimary;