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;

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 原文:http://www.myexception.cn/operating-system/2052286.htm...
    KYM1988阅读 6,052评论 2 2
  • 首先进一则广告: 蓝牙技术联盟(Bluetooth SIG)2010年7月7日宣布,正式采纳蓝牙4.0核心规范(B...
    L泽阅读 5,305评论 3 4
  • 本文主要以蓝牙4.0做介绍,因为现在iOS能用的蓝牙也就是只仅仅4.0的设备 用的库就是core bluetoot...
    暮雨飞烟阅读 4,302评论 0 2
  • “你有女朋友没?” “没有” “我喜欢你,你看我行不?” …… …… “你,你,要是拒绝,也不管用,因为我要追你,...
    1cc3a6bd86d4阅读 1,248评论 0 0
  • 稀疏矩阵 稀疏矩阵(sparse matrix)是由于矩阵中存在大量0,从而可以采用特别的存储技巧来压缩内存。由于...
    JxKing阅读 11,305评论 3 4