您的位置:首页 > 其它

CBPeripheralManager学习笔记

2015-05-31 16:08 204 查看
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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: