CBPeripheral
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);
很单纯的一个类 看上去应该就是用来判别设备的 7.0以后给出了identifier
属性来代替UUID
属性,关于NSUUID
也是很简单的一个类不做赘述了。
CBPeripheral中的属性
用来接收蓝牙设备事件的代理
@property(weak, nonatomic) id<CBPeripheralDelegate> delegate;
蓝牙设备的名字
@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);
当前peripheral的链接状态
@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;
查找服务 参数可以为nil
- (void)discoverServices:(NSArray *)serviceUUIDs;
- (void)discoverIncludedServices:(NSArray *)includedServiceUUIDs forService:(CBService *)service;
查找特征
- (void)discoverCharacteristics:(NSArray *)characteristicUUIDs forService:(CBService *)service;
读取特征
- (void)readValueForCharacteristic:(CBCharacteristic *)characteristic;
写入数据
- (void)writeValue:(NSData *)data forCharacteristic:(CBCharacteristic *)characteristic type:(CBCharacteristicWriteType)type;
监听特征值
- (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 被修改时 会被调用
- (void)peripheralDidUpdateName:(CBPeripheral *)peripheral NS_AVAILABLE(NA, 6_0);
链接的设备发生改变,指定CBService
对象已经失效时调用该方法。可通过discoverServices:
读取外设的services
链接。
- (void)peripheral:(CBPeripheral *)peripheral didModifyServices:(NSArray *)invalidatedServices NS_AVAILABLE(NA, 7_0);
当外设更新了RSSI的时候被调用,8.0后弃用了,换下面的.
- (void)peripheralDidUpdateRSSI:(CBPeripheral *)peripheral error:(NSError *)error NS_DEPRECATED(NA, NA, 5_0, 8_0);
调用readRSSI 后在此方法中获取RSSI值
- (void)peripheral:(CBPeripheral *)peripheral didReadRSSI:(NSNumber *)RSSI error:(NSError *)error NS_AVAILABLE(NA, 8_0);
调用查找服务discoverServices:
后 会回调此方法
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error;
调用didDiscoverIncludedServicesForService:
回调此方法没看出用途
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverIncludedServicesForService:(CBService *)service error:(NSError *)error;
调用查找特征discoverCharacteristics
后 会回调此方法
一般会在此方法中遍历服务的特征 并调用readValueForCharacteristic
读取.
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error;
调用读取特征readValueForCharacteristic
后 会回调此方法,收的一切数据,基本都从这里得到.
- (void)peripheral:(CBPeripheral *)peripheral didUpdateValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error;
当发数据到外设的某一个特征值上面,并且响应的类型是CBCharacteristicWriteWithResponse
,会走此方法响应发送是否成功。
- (void)peripheral:(CBPeripheral *)peripheral didWriteValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error;
特征注册通知后 会回调此方法
[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;
写入出错后 会调用此方法 成功的话不调用
- (void)peripheral:(CBPeripheral *)peripheral didWriteValueForDescriptor:(CBDescriptor *)descriptor error:(NSError *)error;