CBPeripheral的繼承
CBPeripheral繼承自CBPeer 看下CBPeer里面的東西
@interface CBPeer : NSObject <NSCopying>
@property(readonly, nonatomic) CFUUIDRef UUID NS_DEPRECATED(5_0, 7_0);
@property(readonly, nonatomic) NSUUID *identifier NS_AVAILABLE(7_0);
很單純的一個類 看上去應(yīng)該就是用來判別設(shè)備的 7.0以后給出了identifier
屬性來代替UUID
屬性,關(guān)于NSUUID
也是很簡單的一個類不做贅述了熟菲。
CBPeripheral中的屬性
用來接收藍(lán)牙設(shè)備事件的代理
@property(weak, nonatomic) id<CBPeripheralDelegate> delegate;
藍(lán)牙設(shè)備的名字
@property(retain, readonly) NSString *name;
接收的信號強度指示Received Signal Strength Indication
@property(retain, readonly) NSNumber *RSSI NS_DEPRECATED(NA, NA, 5_0, 8_0);
8.0以后用下面這個方法代替
- (void)peripheral:(CBPeripheral *)peripheral didReadRSSI:(NSNumber *)RSSI error:(NSError *)error NS_AVAILABLE(NA, 8_0);
是否鏈接 7.0之后廢棄由state代替
@property(readonly) BOOL isConnected NS_DEPRECATED(NA, NA, 5_0, 7_0);
當(dāng)前peripheral的鏈接狀態(tài)
@property(readonly) CBPeripheralState state;
@discussion A list of <code>CBService</code> objects that have been discovered on the peripheral.
@property(retain, readonly) NSArray *services;
CBPeripheral的方法
讀取RSSI值
- (void)readRSSI;
查找服務(wù) 參數(shù)可以為nil
- (void)discoverServices:(NSArray *)serviceUUIDs;
- (void)discoverIncludedServices:(NSArray *)includedServiceUUIDs forService:(CBService *)service;
查找特征
- (void)discoverCharacteristics:(NSArray *)characteristicUUIDs forService:(CBService *)service;
讀取特征
- (void)readValueForCharacteristic:(CBCharacteristic *)characteristic;
寫入數(shù)據(jù)
- (void)writeValue:(NSData *)data forCharacteristic:(CBCharacteristic *)characteristic type:(CBCharacteristicWriteType)type;
監(jiān)聽特征值
- (void)setNotifyValue:(BOOL)enabled forCharacteristic:(CBCharacteristic *)characteristic;
- (void)discoverDescriptorsForCharacteristic:(CBCharacteristic *)characteristic;
- (void)readValueForDescriptor:(CBDescriptor *)descriptor;
- (void)writeValue:(NSData *)data forDescriptor:(CBDescriptor *)descriptor;
CBPeripheral的代理方法
@protocol CBPeripheralDelegate <NSObject>
@optional
peripheral.name 被修改時 會被調(diào)用
- (void)peripheralDidUpdateName:(CBPeripheral *)peripheral NS_AVAILABLE(NA, 6_0);
鏈接的設(shè)備發(fā)生改變,指定CBService
對象已經(jīng)失效時調(diào)用該方法夕土】淝常可通過discoverServices:
讀取外設(shè)的services
鏈接未舟。
- (void)peripheral:(CBPeripheral *)peripheral didModifyServices:(NSArray *)invalidatedServices NS_AVAILABLE(NA, 7_0);
當(dāng)外設(shè)更新了RSSI的時候被調(diào)用,8.0后棄用了,換下面的.
- (void)peripheralDidUpdateRSSI:(CBPeripheral *)peripheral error:(NSError *)error NS_DEPRECATED(NA, NA, 5_0, 8_0);
調(diào)用readRSSI 后在此方法中獲取RSSI值
- (void)peripheral:(CBPeripheral *)peripheral didReadRSSI:(NSNumber *)RSSI error:(NSError *)error NS_AVAILABLE(NA, 8_0);
調(diào)用查找服務(wù)discoverServices:
后 會回調(diào)此方法
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error;
調(diào)用didDiscoverIncludedServicesForService:
回調(diào)此方法沒看出用途
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverIncludedServicesForService:(CBService *)service error:(NSError *)error;
調(diào)用查找特征discoverCharacteristics
后 會回調(diào)此方法
一般會在此方法中遍歷服務(wù)的特征 并調(diào)用readValueForCharacteristic
讀取.
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error;
調(diào)用讀取特征readValueForCharacteristic
后 會回調(diào)此方法,收的一切數(shù)據(jù),基本都從這里得到.
- (void)peripheral:(CBPeripheral *)peripheral didUpdateValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error;
當(dāng)發(fā)數(shù)據(jù)到外設(shè)的某一個特征值上面,并且響應(yīng)的類型是CBCharacteristicWriteWithResponse
,會走此方法響應(yīng)發(fā)送是否成功。
- (void)peripheral:(CBPeripheral *)peripheral didWriteValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error;
特征注冊通知后 會回調(diào)此方法
[peripheral setNotifyValue:YES forCharacteristic: characteristic];
- (void)peripheral:(CBPeripheral *)peripheral didUpdateNotificationStateForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error;
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverDescriptorsForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error;
- (void)peripheral:(CBPeripheral *)peripheral didUpdateValueForDescriptor:(CBDescriptor *)descriptor error:(NSError *)error;
寫入出錯后 會調(diào)用此方法 成功的話不調(diào)用
- (void)peripheral:(CBPeripheral *)peripheral didWriteValueForDescriptor:(CBDescriptor *)descriptor error:(NSError *)error;