CBPeripheralManager

CBPeripheralManager學習筆記


@interface CBPeripheralManager : NSObject

//CBPeripheralManager的幾種狀態

typedef NS_ENUM(NSInteger, CBCentralManagerState) {

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

CBCentralManagerStateUnknown = 0,

//正在重置狀態

CBCentralManagerStateResetting,

//設備不支持的狀態

CBCentralManagerStateUnsupported,

// 設備未授權狀態

CBCentralManagerStateUnauthorized,

//設備關閉狀態

CBCentralManagerStatePoweredOff,

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

CBCentralManagerStatePoweredOn,

}; 


// 設置代理 一般就是當前類

@property(weaknonatomicid<CBPeripheralManagerDelegate> delegate;


// CBPeripheral類實例當前狀態

@property(readonlyCBPeripheralManagerState state;


//當前是否正在廣播數據

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

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