iOS CBPeripheral和CBPeripheralDelegate

CBPeripheral类表示CBCentralManager通过扫描、连接的外围设备。 外设由通用的唯一标识符(UUID)标识,由NSUUID对象表示。 外设可能包含一个或多个服务或提供有关其连接的信号强度的有用信息。

相关属性

  • identifier:Peripheral唯一标识
  • name:Peripheral名称
  • services:可被CentralManager发现的service列表
  • state:当前连接的状态
  • RSSI:信号值

发现Services

当Peripheral发现一个或多个Service时,Peripheral将调用代理方法(- (void)peripheral:(CBPeripheral )peripheral didDiscoverServices:(nullable NSError )error)

- (void)discoverServices:(NSArray<CBUUID *> *)serviceUUIDs;
  • serviceUUIDs:可以添加一组CBUUID的service,这样就可以发现指定的Services(官方推荐),如果为nil,则去发现所有有效的Services(相对较慢,不推荐)

当Peripheral发现一个或多个Service时,Peripheral将调用代理方法(- (void)peripheral:(CBPeripheral )peripheral didDiscoverIncludedServicesForService:(CBService )service error:(NSError *)error;)

- (void)discoverIncludedServices:(NSArray<CBUUID *> *)includedServiceUUIDs forService:(CBService *)service;
  • 可以添加一组CBUUID的service,这样就可以发现指定的Services(官方推荐),如果为nil,则去发现所有有效的Services(相对较慢,不推荐)

发现Characteristics 和Characteristics 的描述

发现指定Services的一个或多个Characteristics时,Peripheral将调用代理方法(- (void)peripheral:(CBPeripheral )peripheral didDiscoverCharacteristicsForService:(CBService )service error:(NSError *)error)

- (void)discoverCharacteristics:(NSArray<CBUUID *> *)characteristicUUIDs forService:(CBService *)service;
  • characteristicUUIDs : 可以添加一组CBService的characteristic,这样就可以发现指定的characteristic(官方推荐),如果为nil,则去发现所有有效的characteristic(相对较慢,不推荐)
    service : 被发现Characteristics的CBService

发现一个或多个指定characteristic的描述符时,Peripheral将调用(- (void)peripheral:(CBPeripheral )peripheral didDiscoverDescriptorsForCharacteristic:(CBCharacteristic )characteristic error:(NSError *)error)

 - (void)discoverDescriptorsForCharacteristic:(CBCharacteristic *)characteristic;

读取characteristic和characteristic描述

读取特征值时,Peripheral将调用代理方法(-(void)peripheral:(CBPeripheral )peripheral didUpdateValueForCharacteristic:(CBCharacteristic )characteristic error:(NSError *)error) 如果成功检索到特征值,可以通过特性的value属性来访问它。

- (void)readValueForCharacteristic:(CBCharacteristic *)characteristic;

读取特征描述符的值时,Peripheral将调用代理方法(-(void)peripheral:(CBPeripheral )peripheral didUpdateValueForDescriptor:(CBDescriptor )descriptor error:(NSError *)error)。 如果成功检索到特征描述符的值,则可以通过特征描述符的value属性来访问它。

- (void)readValueForDescriptor:(CBDescriptor *)descriptor;

写入characteristic和characteristic变化

写入特征值时

- (void)writeValue:(NSData *)data forCharacteristic:(CBCharacteristic *)characteristic type:(CBCharacteristicWriteType)type;
  • data : 要发送的data
  • characteristic:对应的characteristic
  • type:对应的有俩个类型
    CBCharacteristicWriteWithResponse:Peripheral将调用代理方法(- (void)peripheral:(CBPeripheral *)peripheral didWriteValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error),可以根据这个来判断是否发送成功
    CBCharacteristicWriteWithoutResponse时,如果发送不成功也不会通知你

写入特征描述符的值时,Peripheral将调用代理方法(- (void)peripheral:(CBPeripheral )peripheral didWriteValueForDescriptor:(CBDescriptor )descriptor error:(NSError *)error)

- (void)writeValue:(NSData *)data forDescriptor:(CBDescriptor *)descriptor;
  • data : 要发送的data
  • descriptor : data的描述

设置characteristic值的通知(写使能)

- (void)setNotifyValue:(BOOL)enabled forCharacteristic:(CBCharacteristic *)characteristic;
  • enabled:yes:characteristic的值更改时会接收通知或指示,Peripheral将调用代理方法(-(void)peripheral:(CBPeripheral )peripheral didUpdateNotificationStateForCharacteristic:(CBCharacteristic )characteristic error:(NSError )error),如果这个方法没有错误,那么当只要characteristic值发生变化时,Peripheral将调用代理方法(-(void)peripheral:(CBPeripheral )peripheral didUpdateValueForCharacteristic:(CBCharacteristic )characteristic error:(NSError )error);NO,则不会
  • characteristic:要处理的characteristic

关于CBPeripheralDelegate代理方法

发现Services

发现Service时回调的方法

- (void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error;

在Service中发现子Service回调的方法

- (void)peripheral:(CBPeripheral *)peripheral didDiscoverIncludedServicesForService:(CBService *)service error:(NSError *)error;

发现Characteristic 和Characteristic 描述

发现Service的Characteristic后回调的方法

- (void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error;

发现characteristic值的描述信息回调的方法

- (void)peripheral:(CBPeripheral *)peripheral didDiscoverDescriptorsForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error;

获取Characteristic和Characteristic描述

Characteristic值更新时回调的方法

- (void)peripheral:(CBPeripheral *)peripheral didUpdateValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error;

Characteristic的描述值更新时回调的方法

- (void)peripheral:(CBPeripheral *)peripheral didUpdateValueForDescriptor:(CBDescriptor *)descriptor error:(NSError *)error;

写入Characteristic和Characteristic描述

向Characteristic值写数据时回调的方法

- (void)peripheral:(CBPeripheral *)peripheral didWriteValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error;

写Characteristic描述信息时触回调的方法

- (void)peripheral:(CBPeripheral *)peripheral didWriteValueForDescriptor:(CBDescriptor *)descriptor error:(NSError *)error;

管理Characteristic的通知

Characteristic值的通知设置改变时回调的方法

- (void)peripheral:(CBPeripheral *)peripheral didUpdateNotificationStateForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error;

获取Peripheral接收信号强度

信号强度改变时回调的方法

- (void)peripheralDidUpdateRSSI:(CBPeripheral *)peripheral error:(NSError *)error;

读取信号强度回调的方法

- (void)peripheral:(CBPeripheral *)peripheral didReadRSSI:(NSNumber *)RSSI error:(NSError *)error;

监控Peripheral的名称或Services的更改

Peripheral名称更改时回调的方法

- (void)peripheralDidUpdateName:(CBPeripheral *)peripheral;

外设服务变化时回调的方法

- (void)peripheral:(CBPeripheral *)peripheral didModifyServices:(NSArray<CBService *> *)invalidatedServices;

外设服务变化时回调的方法

- (void)peripheralDidInvalidateServices:(CBPeripheral *)peripheral;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值