四、中心设备与外设对象CBCentral与CBPeripheral
上面介绍了中心设备管理类与外设管理类,这些类用于将设备连接建立起来,器具的数据交换的服务和一些信息则是在对应的设备对象中。
1、中心设备 CBCentral属性与方法
//设备UUID
@property(readonly, nonatomic) NSUUID *identifier;
//中心设备最大接收的数据长度
@property(readonly, nonatomic) NSUInteger maximumUpdateValueLength;
2、外设CAPeripheral属性与方法
外设对象要比中心对象复杂的多,当centralManager连接到外设后,需要通过外设对象的代理方法进行数据交互,其中主要方法属性如下:
//设置代理
@property(assign, nonatomic, nullable) id<CBPeripheralDelegate> delegate;
//外设name
@property(retain, readonly, nullable) NSString *name;
//信号强度
@property(retain, readonly, nullable) NSNumber *RSSI NS_DEPRECATED(NA, NA, 5_0, 8_0);
//外设状态
/*
typedef NS_ENUM(NSInteger, CBPeripheralState) {
CBPeripheralStateDisconnected = 0,//未连接
CBPeripheralStateConnecting,//正在链接
CBPeripheralStateConnected,//已经连接
CBPeripheralStateDisconnecting NS_AVAILABLE(NA, 9_0),//正在断开连接
} NS_AVAILABLE(NA, 7_0);
*/
@property(readonly) CBPeripheralState state;
//所有的服务数组
@property(retain, readonly, nullable) NSArray<CBService *> *services;
//获取当前信号强度
- (void)readRSSI;
//根据服务UUID寻找服务对象
- (void)discoverServices:(nullable NSArray<CBUUID *> *)serviceUUIDs;
//在服务对象UUID数组中寻找特定服务
- (void)discoverIncludedServices:(nullable NSArray<CBUUID *> *)includedServiceUUIDs forService:(CBService *)service;
//在一个服务中寻找特征值
- (void)discoverCharacteristics:(nullable NSArray<CBUUID *> *)characteristicUUIDs forService:(CBService *)service;
//从一个特征中读取数据
- (void)readValueForCharacteristic:(CBCharacteristic *)characteristic;
//写数据的最大长度
//type枚举如下
/*
typedef NS_ENUM(NSInteger, CBCharacteristicWriteType) {
CBCharacteristicWriteWithResponse = 0,//写数据并且接收成功与否回执
CBCharacteristicWriteWithoutResponse,//写数据不接收回执
};
*/
- (NSUInteger)maximumWriteValueLengthForType:(CBCharacteristicWriteType)type NS_AVAILABLE(NA, 9_0);
//向某个特征中写数据
- (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;
外设的代理方法如下:
//外设名称更改时回调的方法
- (void)peripheralDidUpdateName:(CBPeripheral *)peripheral NS_AVAILABLE(NA, 6_0);
//外设服务变化时回调的方法
- (void)peripheral:(CBPeripheral *)peripheral didModifyServices:(NSArray<CBService *> *)invalidatedServices NS_AVAILABLE(NA, 7_0);
//信号强度改变时调用的方法
- (void)peripheralDidUpdateRSSI:(CBPeripheral *)peripheral error:(nullable NSError *)error NS_DEPRECATED(NA, NA, 5_0, 8_0);
//读取信号强度回调的方法
- (void)peripheral:(CBPeripheral *)peripheral didReadRSSI:(NSNumber *)RSSI error:(nullable NSError *)error NS_AVAILABLE(NA, 8_0);
//发现服务时调用的方法
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(nullable NSError *)error;
//在服务中发现子服务回调的方法
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverIncludedServicesForService:(CBService *)service error:(nullable NSError *)error;
//发现服务的特征值后回调的方法
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(nullable NSError *)error;
//特征值更新时回调的方法
- (void)peripheral:(CBPeripheral *)peripheral didUpdateValueForCharacteristic:(CBCharacteristic *)characteristic error:(nullable NSError *)error;
//向特征值写数据时回调的方法
- (void)peripheral:(CBPeripheral *)peripheral didWriteValueForCharacteristic:(CBCharacteristic *)characteristic error:(nullable NSError *)error;
//特征值的通知设置改变时触发的方法
- (void)peripheral:(CBPeripheral *)peripheral didUpdateNotificationStateForCharacteristic:(CBCharacteristic *)characteristic error:(nullable NSError *)error;
//发现特征值的描述信息触发的方法
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverDescriptorsForCharacteristic:(CBCharacteristic *)characteristic error:(nullable NSError *)error;
//特征的描述值更新时触发的方法
- (void)peripheral:(CBPeripheral *)peripheral didUpdateValueForDescriptor:(CBDescriptor *)descriptor error:(nullable NSError *)error;
//写描述信息时触发的方法
- (void)peripheral:(CBPeripheral *)peripheral didWriteValueForDescriptor:(CBDescriptor *)descriptor error:(nullable NSError *)error;
五、服务对象CBService
服务对象是用来管理外设提供的一些数据服务的,其中属性如下:
//对应的外设
@property(assign, readonly, nonatomic) CBPeripheral *peripheral;
//是否是初等服务
@property(readonly, nonatomic) BOOL isPrimary;
//包含的自服务
@property(retain, readonly, nullable) NSArray<CBService *> *includedServices;
//服务中的特征值
@property(retain, readonly, nullable) NSArray<CBCharacteristic *> *characteristics;
六、服务的特征值CBCharacteristic
通过绑定服务中的特征值来进行数据的读写操作,其中属性如下:
//对应的服务对象
@property(assign, readonly, nonatomic) CBService *service;
//特征值的属性 枚举如下
/*
typedef NS_OPTIONS(NSUInteger, CBCharacteristicProperties) {
CBCharacteristicPropertyBroadcast,//允许广播特征
CBCharacteristicPropertyRead,//可读属性
CBCharacteristicPropertyWriteWithoutResponse,//可写并且接收回执
CBCharacteristicPropertyWrite,//可写属性
CBCharacteristicPropertyNotify,//可通知属性
CBCharacteristicPropertyIndicate,//可展现的特征值
CBCharacteristicPropertyAuthenticatedSignedWrites,//允许签名的特征值写入
CBCharacteristicPropertyExtendedProperties,
CBCharacteristicPropertyNotifyEncryptionRequired,
CBCharacteristicPropertyIndicateEncryptionRequired
};
*/
@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;
七、读写请求对象CBATTRequest
服务对象是外设向中心设备提供的相关数据服务,获取到相应服务后,中心设备可以进行读写请求,读写对象属性如下:
//对应的中心设备
@property(readonly, nonatomic) CBCentral *central;
//对应的特征值
@property(readonly, nonatomic) CBCharacteristic *characteristic;
//读写数据值
@property(readwrite, copy, nullable) NSData *value;