蓝牙开发系列四:CBPeripheral详解

一、宏定义

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;
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,491评论 5 459
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,856评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,745评论 0 319
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,196评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,073评论 4 355
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,112评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,531评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,215评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,485评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,578评论 2 309
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,356评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,215评论 3 312
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,583评论 3 299
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,898评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,174评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,497评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,697评论 2 335

推荐阅读更多精彩内容