- 参考文章:
iOS中的蓝牙开发
iOS-BLE蓝牙开发demo
- 官网
- 转载 CenteralManager学习笔记
<span style="font-size:18px;">// CBCentralManager学习笔记
@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</span>
网友评论