您的应用可以使用用于订阅其他附近设备发布的消息的相同机制来订阅蓝牙低功耗 (BLE) 信标消息。
默认情况下,只有当您的应用在前台运行时,信标订阅才会生效。当您的应用进入后台时,这些订阅会自动停止扫描信标。如需详细了解如何启用后台扫描,请参阅后台扫描。
如需订阅信标,请在订阅参数中将 deviceTypesToDiscover
参数设置为 kGNSDeviceBLEBeacon
。以下代码段演示了如何执行此操作:
Objective-C
id<GNSSubscription> beaconSubscription = [messageManager
subscriptionWithMessageFoundHandler:myMessageFoundHandler
messageLostHandler:myMessageLostHandler
paramsBlock:^(GNSSubscriptionParams *params) {
params.deviceTypesToDiscover = kGNSDeviceBLEBeacon;
}];
Swift
let beaconSubscription = messageManager.subscriptionWithMessageFoundHandler(
myMessageFoundHandler, messageLostHandler: myMessageLostHandler,
paramsBlock: { (params: GNSSubscriptionParams!) in
params.deviceTypesToDiscover = .BLEBeacon
})
上述订阅仅会发现您项目拥有的各个信标,并接收来自这些信标的所有消息。如果您想接收使用其他命名空间注册的信标发送的消息,可以在订阅参数中传递一个命名空间。同样,如果您想要特定类型的消息,也可以传递消息类型进行过滤。以下代码段展示了如何执行此操作:
Objective-C
id<GNSSubscription> beaconSubscription = [messageManager
subscriptionWithMessageFoundHandler:myMessageFoundHandler
messageLostHandler:myMessageLostHandler
paramsBlock:^(GNSSubscriptionParams *params) {
params.deviceTypesToDiscover = kGNSDeviceBLEBeacon;
params.messageNamespace = @"com.mycompany.mybeaconservice";
params.type = @"mybeacontype";
}];
Swift
let beaconSubscription = messageManager.subscriptionWithMessageFoundHandler(
myMessageFoundHandler, messageLostHandler: myMessageLostHandler,
paramsBlock: { (params: GNSSubscriptionParams!) in
params.deviceTypesToDiscover = .BLEBeacon
params.messageNamespace = "com.mycompany.mybeaconservice"
params.type = "mybeacontype"
})
默认情况下,信标订阅会同时扫描 Eddystone 和 iBeacon 两种类型的信标。启用 iBeacon 扫描功能后,系统会提示用户授权应用使用其位置数据。应用的 Info.plist
必须包含 NSLocationWhenInUseUsageDescription
键,并简要说明使用位置信息的原因。如需了解详情,请参阅 Apple 文档。
如果您只想扫描 Eddystone 信标,可以在 GNSBeaconStrategy
中停用 iBeacon 扫描,这样 iOS 便不会向用户请求使用位置信息的权限。以下代码段展示了如何为上述原始订阅停用 iBeacon 扫描:
Objective-C
id<GNSSubscription> beaconSubscription = [messageManager
subscriptionWithMessageFoundHandler:myMessageFoundHandler
messageLostHandler:myMessageLostHandler
paramsBlock:^(GNSSubscriptionParams *params) {
params.deviceTypesToDiscover = kGNSDeviceBLEBeacon;
params.beaconStrategy =
[GNSBeaconStrategy strategyWithParamsBlock:^(GNSBeaconStrategyParams *params) {
params.includeIBeacons = NO;
};
}];
Swift
let beaconSubscription = messageManager.subscriptionWithMessageFoundHandler(
myMessageFoundHandler, messageLostHandler: myMessageLostHandler,
paramsBlock: { (params: GNSSubscriptionParams!) in
params.deviceTypesToDiscover = .BLEBeacon
params.beaconStrategy =
GNSBeaconStrategy(paramsBlock: { (params: GNSBeaconStrategyParams!) in
params.includeIBeacons = false
})
})
默认情况下,系统会启用低功耗扫描,这有时会导致在查找 Eddystone 信标时出现较长的延迟时间。停用低功耗模式后,iBeacon 扫描会用于帮助查找 Eddystone 信标,从而缩短这些延迟时间。不过,这会增加电池用量,并且 iOS 会向用户请求使用位置信息的权限。
以下代码段展示了如何在扫描 Eddystone 信标时停用低功耗模式。
Objective-C
id<GNSSubscription> beaconSubscription = [messageManager
subscriptionWithMessageFoundHandler:myMessageFoundHandler
messageLostHandler:myMessageLostHandler
paramsBlock:^(GNSSubscriptionParams *params) {
params.deviceTypesToDiscover = kGNSDeviceBLEBeacon;
params.beaconStrategy =
[GNSBeaconStrategy strategyWithParamsBlock:^(GNSBeaconStrategyParams *params) {
params.includeIBeacons = NO;
params.lowPowerPreferred = NO;
};
}];
Swift
let beaconSubscription = messageManager.subscriptionWithMessageFoundHandler(
myMessageFoundHandler, messageLostHandler: myMessageLostHandler,
paramsBlock: { (params: GNSSubscriptionParams!) in
params.deviceTypesToDiscover = .BLEBeacon
params.beaconStrategy =
GNSBeaconStrategy(paramsBlock: { (params: GNSBeaconStrategyParams!) in
params.includeIBeacons = false
params.lowPowerPreferred = false
})
})
扫描 iBeacon 时,iOS 位置信息权限对话框会先于“附近分享”权限对话框显示。如果您想替换此对话框(例如,提供一个“预处理”对话框来说明为何需要位置信息权限),请在订阅参数中将 permissionRequestHandler
设置为自定义块。以下代码段展示了如何执行此操作:
Objective-C
id<GNSSubscription> beaconSubscription = [messageManager
subscriptionWithMessageFoundHandler:myMessageFoundHandler
messageLostHandler:myMessageLostHandler
paramsBlock:^(GNSSubscriptionParams *params) {
params.deviceTypesToDiscover = kGNSDeviceBLEBeacon;
params.permissionRequestHandler = ^(GNSPermissionHandler permissionHandler) {
// Show your custom dialog here, and don't forget to call permissionHandler after it is dismissed
permissionHandler(userGavePermission);
};
}];
Swift
let beaconSubscription = messageManager.subscriptionWithMessageFoundHandler(
myMessageFoundHandler, messageLostHandler: myMessageLostHandler,
paramsBlock: { (params: GNSSubscriptionParams!) in
params.deviceTypesToDiscover = .BLEBeacon
params.permissionRequestHandler = { (permissionHandler: GNSPermissionHandler!) in
// Show your custom dialog here, and don't forget to call permissionHandler after it is dismissed
permissionHandler(userGavePermission);
}
})
后台扫描
由于信标扫描使用 BLE,因此它可以在后台运行。在决定使用后台模式时,您应注意以下几点:
- 后台 BLE 会增加电池用量。费用很低,但您应先衡量费用,然后再决定是否使用后台模式。
- 如果启用了 iBeacon 扫描或停用了低功耗模式,iOS 会向用户请求在后台使用位置信息的权限。
如需在后台启用信标扫描,请按以下额外步骤操作:
传入正确配置的
GNSBeaconStrategy
对象,为订阅启用后台模式。以下代码段展示了如何执行此操作:Objective-C
id<GNSSubscription> beaconSubscription = [messageManager subscriptionWithMessageFoundHandler:myMessageFoundHandler messageLostHandler:myMessageLostHandler paramsBlock:^(GNSSubscriptionParams *params) { params.deviceTypesToDiscover = kGNSDeviceBLEBeacon; params.beaconStrategy = [GNSBeaconStrategy strategyWithParamsBlock:^(GNSBeaconStrategyParams *params) { params.allowInBackground = YES; }]; }];
Swift
let beaconSubscription = messageManager.subscriptionWithMessageFoundHandler( myMessageFoundHandler, messageLostHandler: myMessageLostHandler, paramsBlock: { (params: GNSSubscriptionParams!) in params.deviceTypesToDiscover = .BLEBeacon params.beaconStrategy = GNSBeaconStrategy(paramsBlock: { (params: GNSBeaconStrategyParams!) in params.allowInBackground = true }) })
将所需条目添加到应用的
Info.plist
:UIBackgroundModes
条目:bluetooth-central
(用于后台 BLE 扫描)。location
,用于在后台使用高功率模式进行 iBeacon 扫描。如果您仅针对 Eddystone 信标执行低功耗扫描,则可以忽略此步骤。
NSLocationAlwaysUsageDescription
字符串,用于说明您将在后台跟踪用户的位置信息的原因。例如,“需要获取您的位置信息,以便在后台扫描信标。”如需了解详情,请参阅 Apple 文档。如果您仅针对 Eddystone 信标执行低功耗扫描,则可以省略此部分。
用户是否可以在您的应用中启用或停用后台扫描?如果是,您应将后台模式值保存为
NSUserDefaults
,因为 iOS 可能会在应用处于后台时随时终止应用。您的应用应执行以下操作:- 每当用户更改后台模式时,将后台模式值保存到
NSUserDefaults
。 - 在启动时,从
NSUserDefaults
读取该值,并在启用后台模式时恢复信标订阅。
- 每当用户更改后台模式时,将后台模式值保存到