扫描外设广播详解

扫描外设广播详解
一、接口说明
/*!
 *  @method scanForPeripheralsWithServices:options:
 *
 *  @param serviceUUIDs A list of <code>CBUUID</code> objects representing the service(s) to scan for.
 *  @param options      An optional dictionary specifying options for the scan.
 *
 *  @discussion         Starts scanning for peripherals that are advertising any of the services listed in <i>serviceUUIDs</i>. Although strongly discouraged,
 *                      if <i>serviceUUIDs</i> is <i>nil</i> all discovered peripherals will be returned. If the central is already scanning with different
 *                      <i>serviceUUIDs</i> or <i>options</i>, the provided parameters will replace them.
 *                      Applications that have specified the <code>bluetooth-central</code> background mode are allowed to scan while backgrounded, with two
 *                      caveats: the scan must specify one or more service types in <i>serviceUUIDs</i>, and the <code>CBCentralManagerScanOptionAllowDuplicatesKey</code>
 *                      scan option will be ignored.
 *
 *  @see                centralManager:didDiscoverPeripheral:advertisementData:RSSI:
 *  @seealso            CBCentralManagerScanOptionAllowDuplicatesKey
 *    @seealso            CBCentralManagerScanOptionSolicitedServiceUUIDsKey
 *
 */
- (void)scanForPeripheralsWithServices:(nullable NSArray<CBUUID *> *)serviceUUIDs options:(nullable NSDictionary<NSString *, id> *)options;

这个方法是支持后台扫描的,前提是工程开启了后台蓝牙模式。然后两个细节:

  1. 如果是后台扫描serviceUUIDs需要至少包含一个

  2. CBCentralManagerScanOptionAllowDuplicatesKey选项将被忽略

二、Options说明
1、CBCentralManagerScanOptionAllowDuplicatesKey
/*!

 *  **@const** CBCentralManagerScanOptionAllowDuplicatesKey

 *

 *  **@discussion** An NSNumber (Boolean) indicating that the scan should run without duplicate filtering. By default, multiple discoveries of the

 *              same peripheral are coalesced into a single discovery event. Specifying this option will cause a discovery event to be generated

 * every time the peripheral is seen, which may be many times per second. This can be useful in specific situations, such as making

 * a connection based on a peripheral's RSSI, but may have an adverse affect on battery-life and application performance.

 *

 *  **@see**        scanForPeripheralsWithServices:options:

 *

 */

这个选项是一个bool值。缺省是false,当发现多个相同的外设时候会合并为一个事件回调。当设置为true的时候,它会将多个相同的的外设单独回调,对设备电池寿命和应用的性能会有影响。但是某些需要通过RSSI来做业务需求的,又需要持续扫描。

2、CBCentralManagerScanOptionSolicitedServiceUUIDsKey
/*!

 *  **@const** CBCentralManagerScanOptionSolicitedServiceUUIDsKey

 *

 *  **@discussion** An NSArray of <code>CBUUID</code> objects respresenting service UUIDs. Causes the scan to also look for peripherals soliciting

 * any of the services contained in the list.

 *

 *  **@see**        scanForPeripheralsWithServices:options:

 *

 */

这个选项是一个bool值。外设建立连接时应用程序挂起,系统会给定外设的连接警报。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值