iOS 实现蓝牙设备重连的四种方式

一、通过identifiers的方式实现重连
/*!
 *  @method retrievePeripheralsWithIdentifiers:
 *
 *  @param identifiers    A list of <code>NSUUID</code> objects.
 *
 *  @discussion            Attempts to retrieve the <code>CBPeripheral</code> object(s) with the corresponding <i>identifiers</i>.
 *
 *    @return                A list of <code>CBPeripheral</code> objects.
 *
 */
- (NSArray<CBPeripheral *> *)retrievePeripheralsWithIdentifiers:(NSArray<NSUUID *> *)identifiers NS_AVAILABLE(10_9, 7_0);

使用retrievePeripheralsWithIdentifiers接口。参数为之前连接过的CBPeripheral.identifier。我们可以在接口列表中找到目标设备,并发起连接。

二、通过UUID的方式实现重连
/*!
 *  @method retrieveConnectedPeripheralsWithServices
 *
 *  @discussion Retrieves all peripherals that are connected to the system and implement any of the services listed in <i>serviceUUIDs</i>.
 *				Note that this set can include peripherals which were connected by other applications, which will need to be connected locally
 *				via {@link connectPeripheral:options:} before they can be used.
 *
 *	@return		A list of <code>CBPeripheral</code> objects.
 *
 */
- (NSArray<CBPeripheral *> *)retrieveConnectedPeripheralsWithServices:(NSArray<CBUUID *> *)serviceUUIDs NS_AVAILABLE(10_9, 7_0);

通过retrieveConnectedPeripheralsWithServices的方式,可以获取到所有当前所有已经连接的设备,可以是其他应用连接的。然后通过CBUUID去过滤出想要的设备。

三、通过scan的方式实现重连
/*!
 *  @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;

这种方式就比较简单了,扫描广播,通过广播筛选自己的目标设备。这个方式是可以做后台重连接的。

四、通过didDisconnect后回连实现重连
/*!
 *  @method centralManager:didDisconnectPeripheral:error:
 *
 *  @param central      The central manager providing this information.
 *  @param peripheral   The <code>CBPeripheral</code> that has disconnected.
 *  @param error        If an error occurred, the cause of the failure.
 *
 *  @discussion         This method is invoked upon the disconnection of a peripheral that was connected by {@link connectPeripheral:options:}. If the disconnection
 *                      was not initiated by {@link cancelPeripheralConnection}, the cause will be detailed in the <i>error</i> parameter. Once this method has been
 *                      called, no more methods will be invoked on <i>peripheral</i>'s <code>CBPeripheralDelegate</code>.
 *
 */
- (void)centralManager:(CBCentralManager *)central didDisconnectPeripheral:(CBPeripheral *)peripheral error:(nullable NSError *)error;

系统回调设备断开连接后,然后在发起重新连接,但是这里有一个注意事项,回连可以延迟执行。为什么要做延迟,我的理解是等待蓝牙做一些初始化动作。具体的原因有知道的小伙伴可以帮忙解答下。谢谢。

相关推荐

  1. iOS 实现设备方式

    2024-06-07 07:12:03       8 阅读
  2. 实现Android设备之间自动配对

    2024-06-07 07:12:03       33 阅读
  3. Freebudsubuntu

    2024-06-07 07:12:03       19 阅读
  4. Mysql索引实现方式

    2024-06-07 07:12:03       15 阅读
  5. 分布式 ID 实现方式

    2024-06-07 07:12:03       34 阅读
  6. 【CSS】垂直居中实现方式

    2024-06-07 07:12:03       36 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-06-07 07:12:03       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-06-07 07:12:03       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-06-07 07:12:03       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-06-07 07:12:03       20 阅读

热门阅读

  1. Git常用指令

    2024-06-07 07:12:03       8 阅读
  2. 机器学习-降维算法,PCK,LDA,NMF,LLE

    2024-06-07 07:12:03       9 阅读
  3. npm发布自己的插件包

    2024-06-07 07:12:03       9 阅读
  4. Android开发之内访Sqlite数据库(六)

    2024-06-07 07:12:03       9 阅读
  5. C#字符串格式化之$语法

    2024-06-07 07:12:03       7 阅读
  6. Python_ 爬楼梯

    2024-06-07 07:12:03       7 阅读
  7. OCR行驶证识别介绍

    2024-06-07 07:12:03       9 阅读
  8. ubuntu24安装python2

    2024-06-07 07:12:03       7 阅读
  9. C语言题目:单词个数统计

    2024-06-07 07:12:03       8 阅读