CBCentralManager的属性
需要一个代理对象来接收中心事件
@property(weak, nonatomic) id<CBCentralManagerDelegate> delegate;
蓝牙设备的当前状态 用代理方法centralManagerDidUpdateState:来更新
@property(readonly) CBCentralManagerState state;
CBCentralManager的方法
初始化
用一个符合协议的的对象和队列初始化CBCentralManager对象
- (instancetype)initWithDelegate:(id<CBCentralManagerDelegate>)delegate queue:(dispatch_queue_t)queue;
- (instancetype)initWithDelegate:(id<CBCentralManagerDelegate>)delegate queue:(dispatch_queue_t)queue options:(NSDictionary *)options NS_AVAILABLE(NA, 7_0)
检索
根据identifiers检索相对应的CBPeripheral对象并返回CBPeripheral数组
- (NSArray *)retrievePeripheralsWithIdentifiers:(NSArray *)identifiers NS_AVAILABLE(NA, 7_0);
检索所有链接到系统的外围设备,和实现了serviceUUIDs列表中的任何一个服务
- (NSArray *)retrieveConnectedPeripheralsWithServices:(NSArray *)serviceUUIDs NS_AVAILABLE(NA, 7_0);
扫描
serviceUUIDs参数是你提供的一组CBUUID对象,如果指定了参数,扫描将只会返回 那些广告服务中包含了参数中CBUUID的周边设备 (如果不完全匹配也会返回)
- (void)scanForPeripheralsWithServices:(NSArray *)serviceUUIDs options:(NSDictionary *)options;
停止扫描
- (void)stopScan;
链接
调用此方法后链接到蓝牙设备
链接不会超时,根据不同的结果,启用不同的回调方法.
- (void)connectPeripheral:(CBPeripheral *)peripheral options:(NSDictionary *)options;
调用此方法后取消链接
- (void)cancelPeripheralConnection:(CBPeripheral *)peripheral;
CBCentralManagerDelegate
@required
获取CentralManager的状态state
- (void)centralManagerDidUpdateState:(CBCentralManager *)central;
typedef NS_ENUM(NSInteger, CBCentralManagerState) { CBCentralManagerStateUnknown = 0, // 初始的时候是未知的(刚刚创建的时候) CBCentralManagerStateResetting, // 正在重置状态 CBCentralManagerStateUnsupported, // 设备不支持的状态 CBCentralManagerStateUnauthorized, // 设备未授权状态 CBCentralManagerStatePoweredOff, // 设备关闭状态 CBCentralManagerStatePoweredOn, // 设备开启状态 -- 可用状态 };
@optional
- (void)centralManager:(CBCentralManager *)central willRestoreState:(NSDictionary *)dict;
当调用scanForPeripheralsWithServices
扫描的时候 会回调这个方法,在这个方法中获取扫描到的设备的信息
- (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;
网友评论