【问题标题】:How to search for continuously for bluetooth peripherals in iOS如何在 iOS 中连续搜索蓝牙外设
【发布时间】:2015-11-02 01:12:52
【问题描述】:

我基本上想从我的 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 设备都会广播的特定/通用服务名称?

【问题讨论】:

  • 大多数设备都会宣传“设备信息”0x180A,但这可能是从健身手环到 iPhone 的任何东西。许多音频设备将使用传统蓝牙,而不是 BLE,因此它们无论如何都不会出现
  • 传统蓝牙?但是没有办法从iOS扫描它?? EA 框架只允许经过苹果认证的设备。
  • 你让它工作了吗?请让我们知道您接受的答案。
  • 这是一种限制。您无法在后台搜索所有这些,并且并非所有这些都广播蓝牙开发者页面上提到的 UUID。所以你必须忍受它......只有当设备有电池时,180F才会被广播。比如汽车音响没有电池,180F就检测不到。

标签: ios objective-c iphone core-bluetooth cbcentralmanager


【解决方案1】:

您可以随时通过 UUID 0x180F 查看电池服务。

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

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

A) 通过将以下键添加到 info.plist 来请求后台模式:Required background modes(数组)。将App communicates using CoreBluetooth 项添加到此数组中。

B) 转到目标设置/功能。打开并展开Background Modes。检查Uses Bluetooth LE accessories。这会将 Required background modes 键添加到您的 info plist 文件中。

【讨论】:

    猜你喜欢
    • 2016-09-13
    • 1970-01-01
    • 2013-08-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多