• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

ios - 当用户从后台手动退出应用程序时如何检索 BLE 外围设备?

[复制链接]
菜鸟教程小白 发表于 2022-12-11 17:47:13 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

解释:

我正在使用核心蓝牙框架连接到 BTLE 外围设备,将外围设备标识符保存在数据库中。
同时,我将外围设备保存在数组中以处理与外围设备的重新连接。
但是,当用户手动退出应用程序时,我的数组变为空。
因此我将无法再次重新连接这些外围设备。

现在,我的数据库中有外围设备标识符。
我看了下,发现有一个委托(delegate)方法

- (NSArray<CBPeripheral *> *)retrievePeripheralsWithIdentifiersNSArray<NSUUID *> *)identifiers NS_AVAILABLE(NA, 7_0)

获取外设但它没有返回任何内容。

我怎样才能再次获取外设?

https://developer.apple.com/library/content/documentation//NetworkingInternetWeb/Conceptual/CoreBluetooth_concepts/BestPracticesForInteractingWithARemotePeripheralDevice/BestPracticesForInteractingWithA

我也检查了上面的文件。因为他们提到了重新连接到外围设备的方法

Retrieving a List of Known Peripherals

The first time you discover a peripheral, the system generates an identifier (a UUID, represented by an NSUUID object) to identify the peripheral. You can then store this identifier (using, for instance, the resources of the NSUserDefaults class), and later use it to try to reconnect to the peripheral using the retrievePeripheralsWithIdentifiers: method of the CBCentralManager class. The following describes one way to use this method to reconnect to a peripheral you’ve previously connected to.

When your app launches, call the retrievePeripheralsWithIdentifiers: method, passing in an array containing the identifiers of the peripherals you’ve previously discovered and connected to (and whose identifiers you have saved), like this:

    knownPeripherals =
        [myCentralManager retrievePeripheralsWithIdentifiers:savedIdentifiers];
The central manager tries to match the identifiers you provided to the identifiers of previously discovered peripherals and returns the results as an array of CBPeripheral objects. If no matches are found, the array is empty and you should try one of the other two reconnection options. If the array is not empty, let the user select (in the UI) which peripheral to try to reconnect to.

When the user selects a peripheral, try to connect to it by calling the connectPeripheralptions: method of the CBCentralManager class. If the peripheral device is still available to be connected to, the central manager calls the centralManager:didConnectPeripheral: method of its delegate object and the peripheral device is successfully reconnected.

Note: A peripheral device may not be available to be connected to for a few reasons. For instance, the device may not be in the vicinity of the central. In addition, some Bluetooth low energy devices use a random device address that changes periodically. Therefore, even if the device is nearby, the address of the device may have changed since the last time it was discovered by the system, in which case the CBPeripheral object you are trying to connect to doesn’t correspond to the actual peripheral device. If you cannot reconnect to the peripheral because its address has changed, you must rediscover it using the scanForPeripheralsWithServicesptions: method.



Best Answer-推荐答案


在应用程序启动时,我会发现所有外围设备,然后将发现的外围设备与存储在数据库中的外围设备标识符进行比较。

发现的外设和外设比较后,就可以连接所需的外设了。

关于ios - 当用户从后台手动退出应用程序时如何检索 BLE 外围设备?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39930889/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap