CBCentralManager學習筆記

// CBCentralManager學習筆記

@interface CBCentralManager : NSObject


//CBCentralManager的幾種狀態


typedef NS_ENUM(NSInteger, CBCentralManagerState) {

// 初始的時候是未知的(剛剛創建的時候)

CBCentralManagerStateUnknown = 0,

//正在重置狀態

CBCentralManagerStateResetting,

//設備不支持的狀態

CBCentralManagerStateUnsupported,

// 設備未授權狀態

CBCentralManagerStateUnauthorized,

//設備關閉狀態

CBCentralManagerStatePoweredOff,

// 設備開啓狀態 -- 可用狀態

CBCentralManagerStatePoweredOn,

};


// 代理

@property(weaknonatomicid<CBCentralManagerDelegate> delegate;

//設備的狀態

@property(readonlyCBCentralManagerState 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

0
發佈了46 篇原創文章 · 獲贊 9 · 訪問量 14萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章