提示:
1,本篇文章是本人憑4年經(jīng)驗創(chuàng)作,如果有什么不正確的地方,請您提出,我改正。
2,文章中所涉及到的思想,在我的開源庫中都有體現(xiàn)。喜歡的話給個start,如果您在使用過程中遇到任何問題,我們可以一對一進行溝通。
從上篇文章中,我們應(yīng)該對藍牙開發(fā)有了個大概的了解,接下來我會對藍牙框架中的每一個類,從蘋果官方文檔出發(fā)來詳細講解。
目錄:
//對象類
CBPeer : NSObject
CBCentral: CBPeer
CBPeripheral : CBPeer
//管理類
CBManager : NSObject
CBPeripheralManager : CBManager
CBCentralManager: CBManager
//屬性類
CBAttribute: NSObject
CBCharacteristic : CBAttribute
CBDescriptor : CBAttribute
CBService : CBAttribute
//其他
CBATTRequest: NSObject
CBUUID: NSObject
對象類
CBPeer: NSObject
介紹:
它是主設(shè)備,從設(shè)備的抽象基類,用于定義表示設(shè)備的對象的常見行為。
它是CBCentral ,CBPeripheral的基類,所以這兩個類都具有identifier屬性。也就是設(shè)備的唯一標(biāo)示。
屬性:
//設(shè)備的唯一ID。(相當(dāng)于UUID)
@property(readonly, nonatomic) NSUUID *identifier
方法:
無
CBCentral: CBPeer
介紹:
表示已連接到在本地設(shè)備上實現(xiàn)外設(shè)角色的應(yīng)用程序的遠程中央設(shè)備。
也就是說,當(dāng)您使用CBPeripheralManager類實現(xiàn)外設(shè)角色時,連接到本地外設(shè)的中心將表示為CBCentral對象。
屬性:
//所能接受數(shù)據(jù)的最大長度。
@property(readonly, nonatomic) NSUInteger maximumUpdateValueLength;
方法:
無
CBPeripheral : CBPeer
介紹:
代表一個發(fā)現(xiàn)廣播或當(dāng)前連接到的遠程外圍設(shè)備。
在這個類上發(fā)現(xiàn)服務(wù),然后再在服務(wù)的特征上進行數(shù)據(jù)交互。
(通常不用自己創(chuàng)建,系統(tǒng)發(fā)現(xiàn)設(shè)備就會主動創(chuàng)建它,并返回)
屬性:
//設(shè)備的代理,接受的外部事件都將在代理方法中返回
@property(weak, nonatomic, nullable) id<CBPeripheralDelegate> delegate;
//設(shè)備的名稱
@property(retain, readonly, nullable) NSString *name;
//設(shè)備信號的強度。
@property(retain, readonly, nullable) NSNumber *RSSI ;
//設(shè)備當(dāng)前狀態(tài) (已斷開連接,正在連接,已連接,正在斷開連接)
@property(readonly) CBPeripheralState state;
//已發(fā)現(xiàn)的外設(shè)上的服務(wù)列表。
//需發(fā)現(xiàn)服務(wù)(- discoverServices: 和 - discoverIncludedServices:forService:)后才會有值
@property(retain, readonly, nullable) NSArray<CBService *> *services;
方法:
//當(dāng)設(shè)備連接之后,讀取設(shè)備的信號強度。
- (void)readRSSI;
//當(dāng)rssi發(fā)生變化后會調(diào)用如下方法
- (void)peripheralDidUpdateRSSI:(CBPeripheral *)peripheral error:(nullable NSError *)error ;//已廢棄
- (void)peripheral:(CBPeripheral *)peripheral didReadRSSI:(NSNumber *)RSSI error:(nullable NSError *)error ;
//發(fā)現(xiàn)外設(shè)上所有的可用服務(wù)。
* serviceUUIDs : 表示要發(fā)現(xiàn)的服務(wù)的對象的列表。
- (void)discoverServices:(nullable NSArray<CBUUID *> *)serviceUUIDs;
//當(dāng)設(shè)備上發(fā)現(xiàn)需要的服務(wù)的時候,會觸發(fā)如下方法。
發(fā)現(xiàn)的服務(wù)將會在設(shè)備的services屬性中。
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(nullable NSError *)error;
//發(fā)現(xiàn)指定的服務(wù)包含的服務(wù)。
*includedServiceUUIDs: 表示要發(fā)現(xiàn)的服務(wù)的對象的列表。
*service: 所在的服務(wù)
- (void)discoverIncludedServices:(nullable NSArray<CBUUID *> *)includedServiceUUIDs forService:(CBService *)service;
//當(dāng)調(diào)用上面的方法,會觸以下方法。
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverIncludedServicesForService:(CBService *)service error:(nullable NSError *)error;
//表示需要發(fā)現(xiàn)服務(wù)上面的特征屬性
- (void)discoverCharacteristics:(nullable NSArray<CBUUID *> *)characteristicUUIDs forService:(CBService *)service;
//如果發(fā)現(xiàn)特征,會觸發(fā)如下方法
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(nullable NSError *)error;
//讀特征上的數(shù)據(jù) 并非所有特性都保證具有可讀值。 可以通過訪問CBCharacteristicProperties枚舉的相關(guān)屬性來確定特征值是否可讀,
- (void)readValueForCharacteristic:(CBCharacteristic *)characteristic;
//當(dāng)特征上的值發(fā)生改變時,會觸發(fā)這個方法。(1,監(jiān)聽特征,2,讀特征)。改變的值在characteristic中的value值。
- (void)peripheral:(CBPeripheral *)peripheral didUpdateValueForCharacteristic:(CBCharacteristic *)characteristic error:(nullable NSError *)error;
//可以以單個寫入類型發(fā)送到特性的最大數(shù)據(jù)量(以字節(jié)為單位)。
//type:寫數(shù)據(jù)的方式
- (NSUInteger)maximumWriteValueLengthForType:(CBCharacteristicWriteType)type;
//data:需要寫入的數(shù)據(jù)
//characteristic:所在的特征
//type:是否需要回執(zhí) 如果指定為CBCharacteristicWriteWithoutResponse,那么數(shù)據(jù)的傳送是盡力而為的,不會保證必須到達。
- (void)writeValue:(NSData *)data forCharacteristic:(CBCharacteristic *)characteristic type:(CBCharacteristicWriteType)type;
//數(shù)據(jù)寫入后的結(jié)果回調(diào),會觸發(fā)這個方法。需要檢查error中的值。
- (void)peripheral:(CBPeripheral *)peripheral didWriteValueForCharacteristic:(CBCharacteristic *)characteristic error:(nullable NSError *)error;
//指示您是否希望在特征的值更改時接收通知或指示。
- (void)setNotifyValue:(BOOL)enabled forCharacteristic:(CBCharacteristic *)characteristic;
//當(dāng)調(diào)用上面的方法,會觸發(fā)下面的方法來告訴設(shè)置通知開始是否成功。如果值改變后的通知不在這個方法中返回。
- (void)peripheral:(CBPeripheral *)peripheral didUpdateNotificationStateForCharacteristic:(CBCharacteristic *)characteristic error:(nullable NSError *)error;
//發(fā)現(xiàn)特征上的描述。如果特征的描述符被成功發(fā)現(xiàn),您可以通過特征描述符屬性來訪問它們。
- (void)discoverDescriptorsForCharacteristic:(CBCharacteristic *)characteristic;
//發(fā)現(xiàn)描述后會觸發(fā)這個方法。所有的描述數(shù)組值會在characteristic屬性descriptors中給出。
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverDescriptorsForCharacteristic:(CBCharacteristic *)characteristic error:(nullable NSError *)error;
//讀取特征描述符的值
- (void)readValueForDescriptor:(CBDescriptor *)descriptor;
//調(diào)用上面方法讀取描述值,如果成功檢索到特征描述符的值,則可以通過特征描述符的value屬性來訪問它。如果失敗,會在error中提現(xiàn)。
- (void)peripheral:(CBPeripheral *)peripheral didUpdateValueForDescriptor:(CBDescriptor *)descriptor error:(nullable NSError *)error;
//往描述上寫數(shù)據(jù)
- (void)writeValue:(NSData *)data forDescriptor:(CBDescriptor *)descriptor;
//寫描述值后會觸發(fā)方法
- (void)peripheral:(CBPeripheral *)peripheral didWriteValueForDescriptor:(CBDescriptor *)descriptor error:(nullable NSError *)error;
//當(dāng)設(shè)備的名稱改變的時候會調(diào)用這個方法
- (void)peripheralDidUpdateName:(CBPeripheral *)peripheral ;
//當(dāng)設(shè)備的服務(wù)被改變的時候,回調(diào)這個方法。
//此時設(shè)備指定的服務(wù),已無效。需要重新調(diào)用發(fā)現(xiàn)服務(wù)方法尋找服務(wù)。
- (void)peripheral:(CBPeripheral *)peripheral didModifyServices:(NSArray<CBService *> *)invalidatedServices ;
管理類
CBManager : NSObject
介紹:
核心藍牙管理器對象(主設(shè)備和從設(shè)備)的抽象基類。
他是CBPeripheralManager, CBCentralManager的基類,所以這兩個類都具有state這個屬性。
屬性:
//當(dāng)前manager的狀態(tài),初始值為CBManagerStateUnknown ,更新由必需的委托方法 managerDidUpdateState:提供。
@property(nonatomic, assign, readonly) CBManagerState state;
方法:
無
CBPeripheralManager: CBManager
介紹:
類是Peripheral的管理者對象。其主要功能是允許您管理GATT數(shù)據(jù)庫中的已發(fā)布服務(wù),并向其他設(shè)備發(fā)布這些服務(wù)。
屬性:
//將接收外設(shè)事件的委托對象。
@property(nonatomic, weak, nullable) id<CBPeripheralManagerDelegate> delegate;
//外設(shè)當(dāng)前是否正在廣告數(shù)據(jù)。
@property(nonatomic, assign, readonly) BOOL isAdvertising;
方法:
//此方法不會提示用戶進行訪問。 您可以使用它來檢測受限訪問,只需隱藏UI,而不是提示訪問。
當(dāng)前用于共享數(shù)據(jù)的授權(quán)狀態(tài)。
+ (CBPeripheralManagerAuthorizationStatus)authorizationStatus ;
//queue:所運行的隊列,他派遣在其上發(fā)送事件的隊列。
//options:指定管理員選項的可選字典。(見底部)
- (instancetype)init;
- (instancetype)initWithDelegate:(nullable id<CBPeripheralManagerDelegate>)delegate
queue:(nullable dispatch_queue_t)queue ;
- (instancetype)initWithDelegate:(nullable id<CBPeripheralManagerDelegate>)delegate
queue:(nullable dispatch_queue_t)queue
options:(nullable NSDictionary<NSString *, id> *)options ;
//開始廣播數(shù)據(jù)
- (void)startAdvertising:(nullable NSDictionary<NSString *, id> *)advertisementData;
//開始廣播后會觸發(fā)如下方法。
如果無法啟動廣播,則會在error參數(shù)中詳細說明原因。
- (void)peripheralManagerDidStartAdvertising:(CBPeripheralManager *)peripheral error:(nullable NSError *)error;
//停止廣播
- (void)stopAdvertising;
// 將現(xiàn)有連接的期望連接延遲設(shè)置為中央設(shè)備。 連接延遲更改不能保證,所以延遲可能會有所不同。 如果未設(shè)置期望的延遲,則將使用在連接建立時由中心設(shè)備選擇的延遲。 通常,不需要更改延遲。
// latency 所需的連接延遲。
- (void)setDesiredConnectionLatency:(CBPeripheralManagerConnectionLatency)latency forCentral:(CBCentral *)central;
//向設(shè)備中添加一個服務(wù)及其關(guān)聯(lián)特征。 如果服務(wù)包含包含的服務(wù),則必須首先發(fā)布。
- (void)addService:(CBMutableService *)service;
//添加服務(wù)成功,會觸發(fā)下面方法。
- (void)peripheralManager:(CBPeripheralManager *)peripheral didAddService:(CBService *)service error:(nullable NSError *)error;
//從服務(wù)列表紅移除一個已經(jīng)發(fā)布的服務(wù),如果這個服務(wù)包含了其他服務(wù),那么必須先移除前者
- (void)removeService:(CBMutableService *)service;
//移除所有已經(jīng)發(fā)布的所有服務(wù)service
- (void)removeAllServices;
//響應(yīng)一個從central傳過來讀或者寫請求
- (void)respondToRequest:(CBATTRequest *)request withResult:(CBATTError)result;
//當(dāng)外設(shè)接收到具有動態(tài)值的特性的ATT請求時,將調(diào)用此方法。
- (void)peripheralManager:(CBPeripheralManager *)peripheral didReceiveReadRequest:(CBATTRequest *)request;
//當(dāng)外設(shè)接收到具有動態(tài)值的一個或多個特性的ATT請求或命令時,將調(diào)用此方法。
- (void)peripheralManager:(CBPeripheralManager *)peripheral didReceiveWriteRequests:(NSArray<CBATTRequest *> *)requests;
//為訂閱了peripheral的central更新characteristic里面的值。
- (BOOL)updateValue:(NSData *)value forCharacteristic:(CBMutableCharacteristic *)characteristic onSubscribedCentrals:(nullable NSArray<CBCentral *> *)centrals;
//調(diào)用updateValue之后調(diào)用此方法:forCharacteristic:onSubscribedCentrals:,當(dāng)外設(shè)再次準(zhǔn)備好發(fā)送特征值更新。
- (void)peripheralManagerIsReadyToUpdateSubscribers:(CBPeripheralManager *)peripheral;
//當(dāng)central配置特征通知或指示時,將調(diào)用此方法。 當(dāng)特征值發(fā)生變化時,應(yīng)將其用作開始發(fā)送更新的提示。
- (void)peripheralManager:(CBPeripheralManager *)peripheral central:(CBCentral *)central didSubscribeToCharacteristic:(CBCharacteristic *)characteristic;
//當(dāng)中心從特征中刪除通知/指示時,將調(diào)用此方法。
- (void)peripheralManager:(CBPeripheralManager *)peripheral central:(CBCentral *)central didUnsubscribeFromCharacteristic:(CBCharacteristic *)characteristic;
//外圍經(jīng)理的狀態(tài)已經(jīng)改變。
- (void)peripheralManagerDidUpdateState:(CBPeripheralManager *)peripheral;
//對于選擇加入狀態(tài)保存和恢復(fù)的應(yīng)用程序,當(dāng)您的應(yīng)用程序重新啟動到后臺完成某些藍牙相關(guān)任務(wù)時,這是第一個調(diào)用的方法。 使用此方法將應(yīng)用程序的狀態(tài)與藍牙系統(tǒng)的狀態(tài)同步。
//dict 包含系統(tǒng)在應(yīng)用程序終止時保留的有關(guān)外設(shè)的信息的字典。
- (void)peripheralManager:(CBPeripheralManager *)peripheral willRestoreState:(NSDictionary<NSString *, id> *)dict;
CBCentralManager: CBManager
介紹:
相當(dāng)于主設(shè)備,用來管理發(fā)現(xiàn)或連接的遠程外圍設(shè)備。包括掃描,發(fā)現(xiàn)和連接到廣告外設(shè)。
在使用其中的方法的時候,藍牙必須是打開狀態(tài)。
屬性:
//接收中心事件的委托對象。
@property(nonatomic, weak, nullable) id<CBCentralManagerDelegate> delegate;
//中央是否正在掃描。
@property(nonatomic, assign, readonly) BOOL isScanning ;
方法:
//初始化方法
//queue 操作所運行的線程,如果不傳就會在主線程上運行。
//options 指定CentralManager選項字典。
CBCentralManagerOptionShowPowerAlertKey (對應(yīng)的BOOL值,當(dāng)設(shè)為YES時,表示CentralManager初始化時,如果藍牙沒有打開,將彈出Alert提示框)
CBCentralManagerOptionRestoreIdentifierKey(唯一標(biāo)識的字符串,用于藍牙進程被殺掉恢復(fù)連接時用的。)
- (instancetype)init;
- (instancetype)initWithDelegate:(nullable id<CBCentralManagerDelegate>)delegate
queue:(nullable dispatch_queue_t)queue;
- (instancetype)initWithDelegate:(nullable id<CBCentralManagerDelegate>)delegate
queue:(nullable dispatch_queue_t)queue
options:(nullable NSDictionary<NSString *, id> *)options ;
//如果主設(shè)備的藍牙狀態(tài)發(fā)生改變,會觸發(fā)這個方法。
此方法必須實現(xiàn),比如用戶關(guān)閉系統(tǒng)藍牙,就應(yīng)該處理這個事件。
- (void)centralManagerDidUpdateState:(CBCentralManager *)central;
//在藍牙于后臺被殺掉時,重連之后會首先調(diào)用此方法,可以獲取藍牙恢復(fù)時的各種狀態(tài)
- (void)centralManager:(CBCentralManager *)central willRestoreState:(NSDictionary<NSString *, id> *)dict;
//該方法整理出所有連接到中央設(shè)備(比如iphone)的外設(shè)。(根據(jù)UUID找到所有匹配的藍牙對象)
返回一個數(shù)組(該數(shù)組包含了所有app連接的外設(shè)列表)
- (NSArray<CBPeripheral *> *)retrievePeripheralsWithIdentifiers:(NSArray<NSUUID *> *)identifiers ;
//從連接的設(shè)備(可能有多個app連接不同的藍牙設(shè)備)中,根據(jù)服務(wù)列表,返回這些服務(wù)的數(shù)組(也就是找到我們指定的服務(wù)設(shè)備,整理到一個數(shù)組中返回)
此套件可能包括由其他應(yīng)用程序連接的外圍設(shè)備,但是還是需要連接后才能使用
- (NSArray<CBPeripheral *> *)retrieveConnectedPeripheralsWithServices:(NSArray<CBUUID *> *)serviceUUIDs ;
//掃描外部設(shè)備
serviceUUIDs:掃描外設(shè)必須包含的服務(wù)列表(如果為空,將全部掃描)
options:掃描時所遵循的規(guī)則
*CBCentralManagerScanOptionAllowDuplicatesKey 默認為NO,過濾功能是否啟用,每次尋找都會合并相同的peripheral。如果設(shè)備YES的話每次都能接受到來自peripherals的廣播包數(shù)據(jù)。
*CBCentralManagerScanOptionSolicitedServiceUUIDsKey 想要掃描的服務(wù)的UUID,以一個數(shù)組的形式存在。掃描的時候只會掃描到包含這些UUID的設(shè)備。
- (void)scanForPeripheralsWithServices:(nullable NSArray<CBUUID *> *)serviceUUIDs options:(nullable NSDictionary<NSString *, id> *)options;
//上面的方法調(diào)用,發(fā)現(xiàn)一個設(shè)備,會回到下面的方法。
//advertisementData:從設(shè)備所包含的廣播數(shù)據(jù)。
//RSSI:設(shè)備的信號強度
- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary<NSString *, id> *)advertisementData RSSI:(NSNumber *)RSSI;
//停止掃描外設(shè)
- (void)stopScan;
//連接一個設(shè)備
options:連接時所尊重的規(guī)則
*CBConnectPeripheralOptionNotifyOnConnectionKey 默認為NO,APP被掛起時,這時如果連接到peripheral時,是否要給APP一個提示框。
*CBConnectPeripheralOptionNotifyOnDisconnectionKey 默認為NO,APP被掛起時,恰好在這個時候斷開連接,要不要給APP一個斷開提示。
*CBConnectPeripheralOptionNotifyOnNotificationKey 默認為NO,APP被掛起時,是否接受到所有的來自peripheral的包都要彈出提示框。
- (void)connectPeripheral:(CBPeripheral *)peripheral options:(nullable NSDictionary<NSString *, id> *)options;
//上面的連接設(shè)備方法,只有兩種可能,一種是成功,一種是不成功。 所以以下兩個方法有且只有一個調(diào)用。
- (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral;
*由于連接嘗試不超時,連接失敗是非典型的,通常表示暫時的問題。
- (void)centralManager:(CBCentralManager *)central didFailToConnectPeripheral:(CBPeripheral *)peripheral error:(nullable NSError *)error;
//取消與外部設(shè)備的連接
- (void)cancelPeripheralConnection:(CBPeripheral *)peripheral;
//主設(shè)備與從設(shè)備失去連接,將會調(diào)用如下方法。這個方法必須處理,應(yīng)為很有可能是由于外部原因(設(shè)備電量低,設(shè)備故障)等失去連接。這時需要提示用戶。
*如果是調(diào)用上面方法主動斷開連接,error會為空。
- (void)centralManager:(CBCentralManager *)central didDisconnectPeripheral:(CBPeripheral *)peripheral error:(nullable NSError *)error;
屬性類
CBAttribute: NSObject
介紹:
一個抽象基類,它定義了表示由外設(shè)提供的服務(wù)方面的對象集合的共同行為。
CBService,CBCharacteristic,CBDescriptor的基類,所以這仨個類都具有UUID這個屬性。
屬性:
//唯一ID
@property(readonly, nonatomic) CBUUID *UUID;
方法:
無
CBService : CBAttribute
介紹:
此類及其子類CBMutableService表示外設(shè)的服務(wù),用于完成設(shè)備(或該設(shè)備的部分)的功能或功能的數(shù)據(jù)和相關(guān)行為的集合,表示遠程外圍設(shè)備的服務(wù)。
服務(wù)是主要的或次要的,可能包含許多特征或包含的服務(wù)(對其他服務(wù)的引用)。
屬性:
//服務(wù)所屬的設(shè)備
@property(assign, readonly, nonatomic) CBPeripheral *peripheral;
//是否是主服務(wù)
@property(readonly, nonatomic) BOOL isPrimary;
//目前在該服務(wù)中已發(fā)現(xiàn)的包含CBServices的列表。
@property(retain, readonly, nullable) NSArray<CBService *> *includedServices;
//此服務(wù)中所有被發(fā)現(xiàn)的特征列表。
@property(retain, readonly, nullable) NSArray<CBCharacteristic *> *characteristics;
方法:
無
CBCharacteristic : CBAttribute
介紹:
此類及子類CBMutableCharacteristic表示有關(guān)外設(shè)服務(wù)的更多信息,表示遠程外設(shè)服務(wù)的特性。
特征包含單個值和描述該值的任意數(shù)量的描述符。 特性的屬性決定了如何使用特征值,以及如何訪問描述符。
屬性:
//這個特征所在的服務(wù)
@property(assign, readonly, nonatomic) CBService *service;
//這個特征所擁有的屬性
@property(readonly, nonatomic) CBCharacteristicProperties properties;
//特征上的值
@property(retain, readonly, nullable) NSData *value;
//特征上所擁有的描述
@property(retain, readonly, nullable) NSArray<CBDescriptor *> *descriptors;
//該特征是否被廣播。
@property(readonly) BOOL isBroadcasted ;
//通知是否已開啟
@property(readonly) BOOL isNotifying;
方法:
無
CBDescriptor : CBAttribute
介紹:
描述及其子類CBMutableDescriptor表示外設(shè)特性的描述,提供有關(guān)特征值的進一步信息。
特征描述符還指示當(dāng)特征值變化時,是否在服務(wù)器(外圍設(shè)備)上配置特征值來指示或通知客戶端(中央)。
屬性:
//這個描述所屬的特征
@property(assign, readonly, nonatomic) CBCharacteristic *characteristic;
//描述上的值
@property(retain, readonly, nullable) id value;
方法:
無
其他
CBATTRequest: NSObject
介紹
“表示來自遠程中央設(shè)備(由CBCentral對象表示)的屬性協(xié)議(ATT)讀取和寫入請求。
遠程中心使用這些ATT請求在本地外設(shè)(由CBPeripheralManager對象表示)上讀寫特征值。
另一方面,本地外設(shè)使用CBATTRequest對象的屬性可以使用CBPeripheralManager類的respondToRequest:withResult:method來適當(dāng)?shù)仨憫?yīng)讀寫請求。
服務(wù)對象是外設(shè)向中心設(shè)備提供的相關(guān)數(shù)據(jù)服務(wù),獲取到相應(yīng)服務(wù)后,中心設(shè)備可以進行讀寫請求。
屬性:
//發(fā)起請求的中心。
@property(readonly, nonatomic) CBCentral *central;
//其值將被讀取或?qū)懭氲奶卣鳌?@property(readonly, nonatomic) CBCharacteristic *characteristic;
//讀取或?qū)懭氲牡谝粋€字節(jié)的從零開始的索引。
@property(readonly, nonatomic) NSUInteger offset;
//寫入或讀取的數(shù)據(jù)
@property(readwrite, copy, nullable) NSData *value;
方法:
無
CBUUID: NSObject
介紹:
它代表一個屬性唯一的ID,是由16、32、128位數(shù)字組成。
一般用在外設(shè)所提供的服務(wù),特征,描述上面。
屬性:
//uuid上的數(shù)據(jù)
@property(nonatomic, readonly) NSData *data;
//uuid所表示的字符串
@property(nonatomic, readonly) NSString *UUIDString
方法:
全為初始化方法
//通過字符串方式創(chuàng)建一個UUID
+ (CBUUID *)UUIDWithString:(NSString *)theString;
//通過data創(chuàng)建一個UUID
+ (CBUUID *)UUIDWithData:(NSData *)theData;
//通過CFUUIDRef對象創(chuàng)建一個UUID
+ (CBUUID *)UUIDWithCFUUID:(CFUUIDRef)theUUID;
//通過NSUUID創(chuàng)建一個UUID
+ (CBUUID *)UUIDWithNSUUID:(NSUUID *)theUUID ;
CBAdvertisementData.h
這是一個常量類,這里面定義的常量都是從設(shè)備廣播出來的數(shù)據(jù)的鍵值對。
當(dāng)主設(shè)備發(fā)現(xiàn)從設(shè)備相應(yīng)代理里面所包含的廣播數(shù)據(jù) - (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI
CBAdvertisementDataLocalNameKey 設(shè)備的廣播名
CBAdvertisementDataTxPowerLevelKey 外設(shè)的發(fā)送功率
CBAdvertisementDataServiceDataKey 特定服務(wù)的分發(fā)數(shù)據(jù)
CBAdvertisementDataServiceUUIDsKey 需要公布的服務(wù)的`UUID`數(shù)組
CBAdvertisementDataManufacturerDataKey 設(shè)備的生產(chǎn)廠家
CBAdvertisementDataOverflowServiceUUIDsKey 附加服務(wù)的UUID數(shù)組
CBAdvertisementDataIsConnectable 表示是否可被連接
CBAdvertisementDataSolicitedServiceUUIDsKey 一個代表著一個或多個服務(wù)的`UUID`
CBPeripheralManagerOptionShowPowerAlertKey 對應(yīng)的值是一個NSNumber類型BOOL值,它標(biāo)識了在系統(tǒng)peripheral創(chuàng)建在藍牙關(guān)閉的情況下是否應(yīng)該顯示一個警告對話框
CBPeripheralManagerOptionRestoreIdentifierKey 對應(yīng)的值是一個字典(數(shù)組)創(chuàng)建一個CBPeripheralManager的一個實例時從options中取出值去恢復(fù)Peripheral的狀態(tài)
CBPeripheralManagerRestoredStateServicesKey CBMutableService對象的數(shù)組,包含系統(tǒng)終止應(yīng)用程序時發(fā)布到本地外設(shè)數(shù)據(jù)庫的所有服務(wù)。
CBPeripheralManagerRestoredStateAdvertisementDataKey 包含外部管理器在系統(tǒng)終止應(yīng)用程序時廣播的數(shù)據(jù)的字典。
CBConnectPeripheralOptionNotifyOnDisconnectionKey設(shè)置當(dāng)外設(shè)斷開連接后是否彈出一個警告
CBConnectPeripheralOptionNotifyOnConnectionKey設(shè)置當(dāng)外設(shè)連接后是否彈出一個警告
CBConnectPeripheralOptionNotifyOnNotificationKey設(shè)置當(dāng)外設(shè)暫停連接后是否彈出一個警告
note:下一篇文章,我講著重介紹,我在github上的第三方庫, 如您有興趣,可以先預(yù)習(xí)下。(希望能給個start??)