2

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

4

1 回答 1

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 文件中。

于 2015-08-10T10:23:28.693 回答