美文网首页
蓝牙开发系列三:CBCentralManager详解

蓝牙开发系列三:CBCentralManager详解

作者: 猿二胖 | 来源:发表于2017-11-20 22:42 被阅读0次

一、CBCentralManagerState定义

typedef NS_ENUM(NSInteger, CBCentralManagerState) {
    CBCentralManagerStateUnknown = CBManagerStateUnknown,//蓝牙状态未知,需要更新
    CBCentralManagerStateResetting = CBManagerStateResetting,//与系统服务的连接暂时丢失,需要更新
    CBCentralManagerStateUnsupported = CBManagerStateUnsupported,//该平台不支持蓝牙低能耗
    CBCentralManagerStateUnauthorized = CBManagerStateUnauthorized,//该应用不被授权支持蓝牙低能耗
    CBCentralManagerStatePoweredOff = CBManagerStatePoweredOff,//蓝牙关闭
    CBCentralManagerStatePoweredOn = CBManagerStatePoweredOn,//蓝牙打开
} NS_DEPRECATED(10_7, 10_13, 5_0, 10_0, "Use CBManagerState instead");
注意:中心设备为中心角色,当且仅当其状态为CBCentralManagerStatePoweredOn时方可发送指令。

二、代理

//只有设置该代理方法,连接扫描回调方法方可调用
@property(nonatomic, weak, nullable) id<CBCentralManagerDelegate> delegate;

三、扫描状态

//判断设备是否处于扫描中
@property(nonatomic, assign, readonly) BOOL isScanning NS_AVAILABLE(10_13, 9_0);

四、初始化方法

1、方法一:

//初始化方法
- (instancetype)init;

2、方法二:

/*!
 *  @method 初始化方法
 *
 *  @param delegate 代理,处理中心设备重要的事件。
 *  @param queue    用于处理事件的队列
 *
 *  @discussion     初始化方法,中心设备处理事件将在提供的queue上执行
 *
 */
- (instancetype)initWithDelegate:(nullable id<CBCentralManagerDelegate>)delegate
                           queue:(nullable dispatch_queue_t)queue;

3、方法三:

/*!
 *  @method 初始化方法
 *
 *  @param delegate 代理,处理中心设备重要的事件。
 *  @param queue      用于处理事件的队列
 *  @param options  可选,一个字典用来指定manager的选项
 *
 *  初始化方法,用于中心设备处理事件将在提供的queue上执行
 *
 * 布尔值(NSNumber),用来明确当manager初始化时,蓝牙关闭,系统是否向用户提示,默认为NO
 *  @seealso        CBCentralManagerOptionShowPowerAlertKey
 *字符串,系统使用UID标明manager
 *  @seealso        CBCentralManagerOptionRestoreIdentifierKey
 *
 */
- (instancetype)initWithDelegate:(nullable id<CBCentralManagerDelegate>)delegate
                           queue:(nullable dispatch_queue_t)queue
                         options:(nullable NSDictionary<NSString *, id> *)options NS_AVAILABLE(10_9, 7_0) NS_DESIGNATED_INITIALIZER;

五、检索设备

1、方法一:

/*!
 *  @method 根据设备UUID号检索外设
 *
 *  @param identifiers          数组,内含NSUUID
 *
 *  @discussion         通过设备对应的NSUUID检索外设
 *
 *  @return             数组,内含检索到的外设.
 *
 */
- (NSArray<CBPeripheral *> *)retrievePeripheralsWithIdentifiers:(NSArray<NSUUID *> *)identifiers NS_AVAILABLE(10_9, 7_0);

2、方法二:


/*!
 *  @method 根据服务UUID检索手机已连接的设备
 *
 *  @discussion     通过服务UUIDS检索连接系统的所有外设(包括其他应用程序连接的设备)
 *  注意,该设置包括其他应用连接的设备,在这些设备使用之前需要调用connect方法连接
 *
 *  @return     数组,内含检索到的外设.
 *
 */
- (NSArray<CBPeripheral *> *)retrieveConnectedPeripheralsWithServices:(NSArray<CBUUID *> *)serviceUUIDs NS_AVAILABLE(10_9, 7_0);

六、扫描外设

/*!
 *  @method 根据外设的服务UUID扫描外设
 *
 *  @param 数组,扫描服务UUID
 *  @param options      字典,扫描可选项,包括CBCentralManagerScanOptionAllowDuplicatesKey和CBCentralManagerScanOptionSolicitedServiceUUIDsKey
 *
 *  @discussion        扫描广播服务UUID为serviceUUIDs内包含的服务的设备,如果serviceUUIDs 为nil,则返回扫描到的所有设备。 如果已经通过不同的serviceUUIDs和options设置正在扫描,则新传入的参数将会替代原来的。
 *   如果 serviceUUIDs为nil,并且所有扫描到的设备已经返回,将会被新提供的参数替代。
 *    应用允许在后台扫描,必须满足两个条件:           
 *   1、扫描必须明确一个或者更多服务类型,      
 *   2、CBCentralManagerScanOptionAllowDuplicatesKey被忽略。
 * //CBCentralManagerScanOptionAllowDuplicatesKey,NSNumber类型,默认为NO,如果为YES,则过滤将会被忽略,每次接收到设备广播的包就会触发发现设备方法。比较耗电。非必须情况下应设置为NO,如果设置为NO,则同一设备发送的将被过滤掉。
 *  @seealso   CBCentralManagerScanOptionAllowDuplicatesKey
 *  CBCentralManagerScanOptionSolicitedServiceUUIDsKey,NSArray类型,内含需要扫描的服务UUIDs,设置该扫描选项也导致中央管理器扫描外设遵循数组中包含的任何服务。
 *  @seealso    CBCentralManagerScanOptionSolicitedServiceUUIDsKey
 */
- (void)scanForPeripheralsWithServices:(nullable NSArray<CBUUID *> *)serviceUUIDs options:(nullable NSDictionary<NSString *, id> *)options;

七、停止扫描

/*!
 *  @method stopScan:
 *
 *  @discussion 停止扫描设备
 *
 */
- (void)stopScan;

八、连接设备

/*!
 *  @method 连接设备方法
 *
 *  @param peripheral   需要连接的设备
 *  @param options      字典对象,设置连接设备可选项
 *
 *  @discussion        向设备发起连接,该连接永远不会超时一直处于等待尝试连接状态,可通过调用cancelPeripheralConnection方法取消连接,根据连接结果回调centralManager:didConnectPeripheral:或者centralManager:didFailToConnectPeripheral:error:代理方法。 
 *  @seealso            CBConnectPeripheralOptionNotifyOnConnectionKey 如果应用被挂起时,连接某设备成功,系统给予连接成功提示
 *  @seealso            CBConnectPeripheralOptionNotifyOnDisconnectionKey如果应用被挂起时,设备连接断开,系统给予设备断开提示
 *  @seealso            CBConnectPeripheralOptionNotifyOnNotificationKey如果应用被挂起时,从设备接收到的所有信息通知,系统给予提示
 
 */
- (void)connectPeripheral:(CBPeripheral *)peripheral options:(nullable NSDictionary<NSString *, id> *)options;

九、取消/断开连接

/*!
 *  @method 取消连接设备
 *
 *  @param peripheral   需要取消连接的设备
 *
 *  @discussion       设备取消连接,注意处理发向该设备的指令以及等待发送的指令或许尚未完成造成的堵塞,需要对指令做回调。  
 *
 *  @see             断开连接成功回调方法:   centralManager:didDisconnectPeripheral:error:
 *
 */
- (void)cancelPeripheralConnection:(CBPeripheral *)peripheral;

十、中心设备代理方法

1 、中心设备蓝牙状态改变回调(必实现)

/*!
 *  @method centralManagerDidUpdateState:
 *
 *  @param central  中心设备
 *
 *  @discussion  
   *  1、 当中心设备的蓝牙状态更新时调用。
     *2、 指令只有在  CBCentralManagerStatePoweredOn状态时才能指令交互。
     *3、如果该状态小于CBCentralManagerStatePoweredOn则扫描停
         止,任何连接的设备断开。
     *4、如果蓝牙状态低于CBCentralManagerStatePoweredOff和中心
         设备连接的所有设备断开,必须重新检索或者重新扫描。
 *         
 */
- (void)centralManagerDidUpdateState:(CBCentralManager *)central;

3、扫描到设备回调

/*!
 *  @method centralManager:didDiscoverPeripheral:advertisementData:RSSI:
 *
 *  @param central             中心设备
 *  @param peripheral        扫描到的设备兑现
 *  @param advertisementData   字典,包含广播和扫描响应数据 
 *  @param RSSI                扫描到的设备的信号强度,单位 dBm. 当值
                                            为127时保留,表示设备无效。
 *
 *  @discussion              
 * 1、  当扫描过程中扫描到设备时调用,
 * 2、该扫描到的设备必须保存起来以供交互,
 * 3、相反,如果扫描到的设备不是需要的设备则中心设备忽略。A 
 *
 *  @seealso                    CBAdvertisementData.h
 *
 */
- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary<NSString *, id> *)advertisementData RSSI:(NSNumber *)RSSI;

4、连接设备成功回调

/*!
 *  @method centralManager:didConnectPeripheral:
 *
 *  @param central      中心设备
 *  @param peripheral   连接设备
 *
 *  @discussion         该方法在调用connectPeripheral:options时,连接设备成功后调用
 *
 */
- (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral;

5、连接设备失败回调

/*!
 *  @method centralManager:didFailToConnectPeripheral:error:
 *
 *  @param central      中心设备
 *  @param peripheral   待连接外设
 *  @param error        引起连接失败的原因
 *
 *  @discussion        该方法在调用方法connectPeripheral:options:连接外设,连接失败后回调。 当连接没有超时时,连接失败是典型的,通常表示暂时性的问题
 *
 */
- (void)centralManager:(CBCentralManager *)central didFailToConnectPeripheral:(CBPeripheral *)peripheral error:(nullable NSError *)error;

6、断开连接回调

/*!
 *  @method centralManager:didDisconnectPeripheral:error:
 *
 *  @param central      中心设备
 *  @param peripheral   断开的设备
 *  @param error        如果断开失败,返回失败原因
 *
 *  @discussion        通过调用 connectPeripheral:options:连接的设备,如果该设备断开不是通过调用cancelPeripheralConnection方法时,引起断开的具体原因将在参数error中体现. 一旦该方法被调用,设备periphera的代理方法将不会被调用
 *
 */
- (void)centralManager:(CBCentralManager *)central didDisconnectPeripheral:(CBPeripheral *)peripheral error:(nullable NSError *)error;

本文翻译自苹果文档,如果对你有帮助的话请点喜欢呦,下面一章将详细讲解CBPeripheral的一些方法。如果有翻译不当的地方还请大家多多交流

相关文章

  • 蓝牙开发系列三:CBCentralManager详解

    一、CBCentralManagerState定义 二、代理 三、扫描状态 四、初始化方法 1、方法一: 2、方法...

  • 蓝牙开发-Central开发

    必须要了解的 1、CBCentralManager 蓝牙中心管理器(作为中心开发使用)CB...

  • ios 智能电表蓝牙连接

    1、开启蓝牙: _manager=[[CBCentralManager alloc] initWithDelega...

  • iOS蓝牙开发(CoreBluetooth )

    CBCentralManager 类表示中心设备,扫描发现周边蓝牙设备,周边蓝牙设备用 CBPeripheral ...

  • iOS蓝牙开发详解

    iOS蓝牙开发详解 随着物联网技术的高速发展,蓝牙开发也越来越火热。不论是智能穿戴设备还是蓝牙家具,车联网蓝牙,都...

  • 06 蓝牙

    iOS的蓝牙开发很简单,只要包含一个库,创建CBCentralManager实例,实现代理方法,然后就可以直接和设...

  • iOS之蓝牙开发—何时触发(CBCentralManager *

    当调用手机连接外设的方法: 作为中心设备的手机会和硬件之间建立连接,建立连接需要一定时间,具体的时间未知,肯定是很...

  • 关于蓝牙重启问题

    实现蓝牙 Restore 代理 在 App 启动的时候,必须初始化 CBCentralManager

  • iOS 蓝牙连接外设思路

    第一步,创建CBCentralManager。第二步,扫描可连接的蓝牙外设(必须在蓝牙模块打开的前提下)。第三步,...

  • 蓝牙开发系列四:CBPeripheral详解

    一、宏定义 1、外设状态 2、设备写服务类型 二、属性 1、设备的UUID 2、外设代理 3、设备名称 4、设备信...

网友评论

      本文标题:蓝牙开发系列三:CBCentralManager详解

      本文链接:https://www.haomeiwen.com/subject/iiwbvxtx.html