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

作者 : admin 本文共2918个字,预计阅读时间需要8分钟 发布时间: 2024-06-8 共3人阅读

文章目录

          • 一、通过identifiers的方式实现重连
          • 二、通过UUID的方式实现重连
          • 三、通过scan的方式实现重连
          • 四、通过didDisconnect后回连实现重连
一、通过identifiers的方式实现重连
/*!
 *  @method retrievePeripheralsWithIdentifiers:
 *
 *  @param identifiers    A list of NSUUID objects.
 *
 *  @discussion            Attempts to retrieve the CBPeripheral object(s) with the corresponding identifiers.
 *
 *    @return                A list of CBPeripheral objects.
 *
 */
- (NSArray *)retrievePeripheralsWithIdentifiers:(NSArray *)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 serviceUUIDs.
 *				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 CBPeripheral objects.
 *
 */
- (NSArray *)retrieveConnectedPeripheralsWithServices:(NSArray *)serviceUUIDs NS_AVAILABLE(10_9, 7_0);

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

三、通过scan的方式实现重连
/*!
 *  @method scanForPeripheralsWithServices:options:
 *
 *  @param serviceUUIDs A list of CBUUID 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 serviceUUIDs. Although strongly discouraged,
 *                      if serviceUUIDs is nil all discovered peripherals will be returned. If the central is already scanning with different
 *                      serviceUUIDs or options, the provided parameters will replace them.
 *                      Applications that have specified the bluetooth-central background mode are allowed to scan while backgrounded, with two
 *                      caveats: the scan must specify one or more service types in serviceUUIDs, and the CBCentralManagerScanOptionAllowDuplicatesKey
 *                      scan option will be ignored.
 *
 *  @see                centralManager:didDiscoverPeripheral:advertisementData:RSSI:
 *  @seealso            CBCentralManagerScanOptionAllowDuplicatesKey
 *	@seealso			CBCentralManagerScanOptionSolicitedServiceUUIDsKey
 *
 */
- (void)scanForPeripheralsWithServices:(nullable NSArray *)serviceUUIDs options:(nullable NSDictionary *)options;

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

四、通过didDisconnect后回连实现重连
/*!
 *  @method centralManager:didDisconnectPeripheral:error:
 *
 *  @param central      The central manager providing this information.
 *  @param peripheral   The CBPeripheral 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 error parameter. Once this method has been
 *                      called, no more methods will be invoked on peripheral's CBPeripheralDelegate.
 *
 */
- (void)centralManager:(CBCentralManager *)central didDisconnectPeripheral:(CBPeripheral *)peripheral error:(nullable NSError *)error;

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

本站无任何商业行为
个人在线分享 » iOS 实现蓝牙设备重连的四种方式
E-->