一、宏定义
1、外设状态
typedef NS_ENUM(NSInteger, CBPeripheralState) {
CBPeripheralStateDisconnected = 0,//断开
CBPeripheralStateConnecting,//正在连接
CBPeripheralStateConnected,//已连接
CBPeripheralStateDisconnecting NS_AVAILABLE(10_13, 9_0),//正在断开连接
} NS_AVAILABLE(10_9, 7_0);
2、设备写服务类型
typedef NS_ENUM(NSInteger, CBCharacteristicWriteType) {
CBCharacteristicWriteWithResponse = 0,//有响应
CBCharacteristicWriteWithoutResponse,//无响应
};
二、属性
1、设备的UUID
//CBPeripheral继承自CBPeer,独一的标示该设备的id
@property(readonly, nonatomic) NSUUID *identifier NS_AVAILABLE(10_13, 7_0);
2、外设代理
/*!
* @property delegate
*
* @discussion 用来接收设备事件的代理
*/
@property(weak, nonatomic, nullable) id<CBPeripheralDelegate> delegate;
3、设备名称
/*!
* @property name
*
* @discussion 设备的名称
*/
@property(retain, readonly, nullable) NSString *name;
4、设备信号
/*!
* @property RSSI
*
* @discussion 最后一次读取到的设备强度
*
* @废弃 被 peripheral:didReadRSSI:error:替代
*/
@property(retain, readonly, nullable) NSNumber *RSSI NS_DEPRECATED(10_7, 10_13, 5_0, 8_0);
5、设备状态
/*!
* @property state
*
* @discussion 外设的连接状态
*/
@property(readonly) CBPeripheralState state;
6、设备服务
/*!
* @property services
*
* @discussion 数组,内含扫描到的设备的服务
*/
@property(retain, readonly, nullable) NSArray<CBService *> *services;
7、是否支持无响应写服务
/*!
* @property canSendWriteWithoutResponse
*
* @discussion
* 1、如果值为YES,远程设备有空间发送一个没有响应的写服务.
* 2、如果值为 NO,如果值被设置为YES时,当前写服务被冲刷,方法peripheralIsReadyToSendWriteWithoutResponse:将会被调用
*/
@property(readonly) BOOL canSendWriteWithoutResponse;
三、方法
1、读信号强度
/*!
* @method readRSSI
*
* @discussion 当连接成功,检索当前连接的信号强度。回调方法为 peripheral:didReadRSSI:error:
*
* @see peripheral:didReadRSSI:error:
*/
- (void)readRSSI;
2、扫描服务特征值
/*!
* @method discoverServices:
*
* @param serviceUUIDs 需要扫描的设备的服务的id,如果为nil,则扫描所有的服务
*
* @discussion 扫描发现设备所有可用的服务
*
* @see :扫描回调方法peripheral:didDiscoverServices:
*/
- (void)discoverServices:(nullable NSArray<CBUUID *> *)serviceUUIDs;
3、发现内联服务
/*!
* @method discoverIncludedServices:forService:
*
* @param includedServiceUUIDs 需要发现的服务service中的服务id列表,如果为nil,则扫描服务内所有的服务,这样的话会比较慢,不推荐
* @param service 服务
*
* @discussion 发现指定服务service内的服务
*
* @see 回调方法: peripheral:didDiscoverIncludedServicesForService:error:
*/
- (void)discoverIncludedServices:(nullable NSArray<CBUUID *> *)includedServiceUUIDs forService:(CBService *)service;
4、发现服务特征值
/*!
* @method discoverCharacteristics:forService:
*
* @param characteristicUUIDs 数组,内含需要被发现的所有特征值类型,如果为nil,则为所有特征值。
* @param service 服务
*
* @discussion 发现指令服务的服务特征值
*
* @see 回调方法为: peripheral:didDiscoverCharacteristicsForService:error:
*/
- (void)discoverCharacteristics:(nullable NSArray<CBUUID *> *)characteristicUUIDs forService:(CBService *)service;
5、读服务特征值
/*!
* @method readValueForCharacteristic:
*
* @param characteristic 需要读取的服务特征值
*
* @discussion 读取服务特征值的值,调用该方法则读取该方法前最新的蓝牙系统缓存的从外设读取的数据
*
* @see 回调方法: peripheral:didUpdateValueForCharacteristic:error:
*/
- (void)readValueForCharacteristic:(CBCharacteristic *)characteristic;
6、获取写服务支持最大字节数
/*!
* @method maximumWriteValueLengthForType:
*
* @discussion 获取向一个写服务可发送的最大字节数
*
* @see 该写服务可通过调用writeValue:forCharacteristic:type:写数据
*/
- (NSUInteger)maximumWriteValueLengthForType:(CBCharacteristicWriteType)type NS_AVAILABLE(10_12, 9_0);
7、向设备写数据
/*!
* @method writeValue:forCharacteristic:type:
*
* @param data 待写数据
* @param characteristic 写服务特征
* @param type 写服务的类型(有/无响应)
*
* @discussion 向指定服务写数据,
* 1、如果指定CBCharacteristicWriteWithResponse类型,写入结果将会回调 peripheral:didWriteValueForCharacteristic:error:方法
* 2、如果指定为CBCharacteristicWriteWithoutResponse类型,同时canSendWriteWithoutResponse为NO时,则数据将尽最大努力,但不会被保证成功。
*
*/
- (void)writeValue:(NSData *)data forCharacteristic:(CBCharacteristic *)characteristic type:(CBCharacteristicWriteType)type;
代理方法
/*!
* @method peripheralDidUpdateName:
*
* @param peripheral 需要更新名称的设备
*
* @discussion 该方法被触发当设备的名称改变
*/
- (void)peripheralDidUpdateName:(CBPeripheral *)peripheral NS_AVAILABLE(10_9, 6_0);
/*!
* @method peripheral:didModifyServices:
*
* @param peripheral 需要更新的设备
* @param invalidatedServices The services that have been invalidated
*
* @discussion 该方法触发当设备的服务改变时候
* 服务可以被重新发现通过discoverServices: 方法
*/
- (void)peripheral:(CBPeripheral *)peripheral didModifyServices:(NSArray<CBService *> *)invalidatedServices NS_AVAILABLE(10_9, 7_0);
/*!
* @method peripheralDidUpdateRSSI:error:
*
* @param peripheral 需要更新的设备.
* @param error 返回错误原因.
*
* @discussion 该方法是readRSSI: 的回调
*
* @deprecated 使 {@link peripheral:didReadRSSI:error:}代替了
*/
- (void)peripheralDidUpdateRSSI:(CBPeripheral *)peripheral error:(nullable NSError *)error NS_DEPRECATED(10_7, 10_13, 5_0, 8_0);
/*!
* @method peripheral:didReadRSSI:error:
*
* @param peripheral 需要更新的设备
* @param RSSI 设备的RSSI.
*
* @discussion 该方法是readRSSI: 的回调
*/
- (void)peripheral:(CBPeripheral *)peripheral didReadRSSI:(NSNumber *)RSSI error:(nullable NSError *)error NS_AVAILABLE(10_13, 8_0);
/*!
* @method peripheral:didDiscoverServices:
*
* @param peripheral 当前设备.
* @param error 错误原因
*
* @discussion 该发放为 discoverServices:回调
*
*/
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(nullable NSError *)error;
/*!
* @method peripheral:didDiscoverIncludedServicesForService:error:
*
* @param peripheral 当前设备
* @param service 设备服务
* @param error 错误原因.
*
* @discussion 该方法为 discoverIncludedServices:forService: 的回调
*/
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverIncludedServicesForService:(CBService *)service error:(nullable NSError *)error;
/*!
* @method peripheral:didDiscoverCharacteristicsForService:error:
*
* @param peripheral The peripheral providing this information.
* @param service The <code>CBService</code> object containing the characteristic(s).
* @param error If an error occurred, the cause of the failure.
*
* @discussion This method returns the result of a @link discoverCharacteristics:forService: @/link call. If the characteristic(s) were read successfully,
* they can be retrieved via <i>service</i>'s <code>characteristics</code> property.
*/
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(nullable NSError *)error;
/*!
* @method peripheral:didUpdateValueForCharacteristic:error:
*
* @param peripheral The peripheral providing this information.
* @param characteristic A <code>CBCharacteristic</code> object.
* @param error If an error occurred, the cause of the failure.
*
* @discussion This method is invoked after a @link readValueForCharacteristic: @/link call, or upon receipt of a notification/indication.
*/
- (void)peripheral:(CBPeripheral *)peripheral didUpdateValueForCharacteristic:(CBCharacteristic *)characteristic error:(nullable NSError *)error;
/*!
* @method peripheral:didWriteValueForCharacteristic:error:
*
* @param peripheral The peripheral providing this information.
* @param characteristic A <code>CBCharacteristic</code> object.
* @param error If an error occurred, the cause of the failure.
*
* @discussion This method returns the result of a {@link writeValue:forCharacteristic:type:} call, when the <code>CBCharacteristicWriteWithResponse</code> type is used.
*/
- (void)peripheral:(CBPeripheral *)peripheral didWriteValueForCharacteristic:(CBCharacteristic *)characteristic error:(nullable NSError *)error;
/*!
* @method peripheral:didUpdateNotificationStateForCharacteristic:error:
*
* @param peripheral The peripheral providing this information.
* @param characteristic A <code>CBCharacteristic</code> object.
* @param error If an error occurred, the cause of the failure.
*
* @discussion This method returns the result of a @link setNotifyValue:forCharacteristic: @/link call.
*/
- (void)peripheral:(CBPeripheral *)peripheral didUpdateNotificationStateForCharacteristic:(CBCharacteristic *)characteristic error:(nullable NSError *)error;
/*!
* @method peripheral:didDiscoverDescriptorsForCharacteristic:error:
*
* @param peripheral The peripheral providing this information.
* @param characteristic A <code>CBCharacteristic</code> object.
* @param error If an error occurred, the cause of the failure.
*
* @discussion This method returns the result of a @link discoverDescriptorsForCharacteristic: @/link call. If the descriptors were read successfully,
* they can be retrieved via <i>characteristic</i>'s <code>descriptors</code> property.
*/
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverDescriptorsForCharacteristic:(CBCharacteristic *)characteristic error:(nullable NSError *)error;
/*!
* @method peripheral:didUpdateValueForDescriptor:error:
*
* @param peripheral The peripheral providing this information.
* @param descriptor A <code>CBDescriptor</code> object.
* @param error If an error occurred, the cause of the failure.
*
* @discussion This method returns the result of a @link readValueForDescriptor: @/link call.
*/
- (void)peripheral:(CBPeripheral *)peripheral didUpdateValueForDescriptor:(CBDescriptor *)descriptor error:(nullable NSError *)error;
/*!
* @method peripheral:didWriteValueForDescriptor:error:
*
* @param peripheral The peripheral providing this information.
* @param descriptor A <code>CBDescriptor</code> object.
* @param error If an error occurred, the cause of the failure.
*
* @discussion This method returns the result of a @link writeValue:forDescriptor: @/link call.
*/
- (void)peripheral:(CBPeripheral *)peripheral didWriteValueForDescriptor:(CBDescriptor *)descriptor error:(nullable NSError *)error;
/*!
* @method peripheralIsReadyToSendWriteWithoutResponse:
*
* @param peripheral 当前设备
*
* @discussion 该方法被调用,当 writeValue:forCharacteristic:type失败,设备再次可以发送服务特征值时调用
*
*/
- (void)peripheralIsReadyToSendWriteWithoutResponse:(CBPeripheral *)peripheral;
/*!
* @method peripheral:didOpenL2CAPChannel:error:
*
* @param peripheral 当前设备
* @param channel CBL2CAPChanne
* @param error 错误信息
*
* @discussion 该方法为openL2CAPChannel: 回调
*/
- (void)peripheral:(CBPeripheral *)peripheral didOpenL2CAPChannel:(nullable CBL2CAPChannel *)channel error:(nullable NSError *)error;
网友评论