CBCentralManager 與之對應的是CBPeripheraimanager
CBCentralManager對應的幾種狀態
typedef NS_ENUM(NSInteger, CBCentralManagerState) {
// 初始的時候是未知的(剛剛創建的時候)
CBCentralManagerStateUnknown = 0,
//正在重置狀態 CBCentralManagerStateResetting, //設備不支持的狀態 CBCentralManagerStateUnsupported, // 設備未授權狀態 CBCentralManagerStateUnauthorized, //設備關閉狀態 CBCentralManagerStatePoweredOff, // 設備開啓狀態 -- 可用狀態 CBCentralManagerStatePoweredOn, };代理@property(weak, nonatomic) id<CBCentralManagerDelegate> delegate;
// 初始化方法-參數-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
必須實現的方法
//僅僅在CBCentralManagerStatePoweredOn的時候可用當central的狀態是OFF的時候所有與中心連接的peripheral都將無效並且都要重新連接,central的初始狀態時是Unknown
- (void)centralManagerDidUpdateState:(CBCentralManager *)central;
//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