文章目录
扫描外设广播详解
一、接口说明
/*!
* @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;
这个方法是支持后台扫描的,前提是工程开启了后台蓝牙模式。然后两个细节:
-
如果是后台扫描serviceUUIDs需要至少包含一个
-
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值。外设建立连接时应用程序挂起,系统会给定外设的连接警报。