OGeek|极客世界-中国程序员成长平台

标题: ios - 如何在 iOS 中连续搜索蓝牙外设 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-13 09:49
标题: ios - 如何在 iOS 中连续搜索蓝牙外设

我基本上想从我的 iOS 应用中搜索附近的 BLE 设备,即使应用在后台也是如此。

iOS 提供了这种方法:

//_cbcManager是CBCentralManager的对象

[_cbcManager scanForPeripheralsWithServices:nil options:[NSDictionary dictionaryWithObject:[NSNumber numberWithBool:NO] forKey:CBCentralManagerScanOptionAllowDuplicatesKey]]

此方法将扫描所有服务(因为参数中提供了 nil)。但这在后台不起作用。当应用在后台时,iOS 需要特定的服务名称。

我基本上想扫描音频设备,阅读此页面:https://developer.bluetooth.org/gatt/services/Pages/ServicesHome.aspx我已经提供了音频设备可以广播的大部分通用服务。所以现在,该方法看起来像这样:

[_cbcManager scanForPeripheralsWithServices[[CBUUID UUIDWithString"180A"], [CBUUID UUIDWithString"1815"], [CBUUID UUIDWithString"1800"], [CBUUID UUIDWithString"1801"], [CBUUID UUIDWithString"1807"], [CBUUID UUIDWithString"180E"], [CBUUID UUIDWithString"1813"], [CBUUID UUIDWithString"181C"]] options:[NSDictionary dictionaryWithObject:[NSNumber numberWithBool:NO] forKey:CBCentralManagerScanOptionAllowDuplicatesKey]]

是否有每个 BLE 设备都会广播的特定/通用服务名称?



Best Answer-推荐答案


您始终可以在 UUID 0x180F 处检查电池服务。

更多详情请访问: https://developer.bluetooth.org/gatt/services/Pages/ServiceViewer.aspx?u=org.bluetooth.service.battery_service.xml

如果您错过了让 BLE 连接在后台工作的情况:

A) 通过将以下键添加到 info.plist 来请求后台模式:必需的后台模式(数组)。将 App 使用 CoreBluetooth 进行通信 项添加到此数组。

B) 转到目标设置/功能。打开并展开 Background Modes。检查使用蓝牙 LE 配件。这会将 必需的背景模式 键添加到您的 info plist 文件中。

关于ios - 如何在 iOS 中连续搜索蓝牙外设,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31914189/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://sqlite.in/) Powered by Discuz! X3.4