您的应用可以订阅蓝牙低功耗 (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 信标,可以停用 iBeacon
在 GNSBeaconStrategy
中进行扫描,并且 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 会在 用户授予使用位置信息的权限。
以下代码段展示了如何在扫描 埃迪斯通灯塔
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 会增加电池成本。费用不高,但是 先对此进行测量,然后再决定使用后台模式。
- 在下列情况下,iOS 会请求用户授予在后台使用位置信息的权限: iBeacon 扫描已启用或低功耗模式已停用。
如需在后台启用信标扫描,请执行以下额外步骤:
通过传入正确的 已配置的
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
读取数据并恢复信标 订阅。
- 无论何时用户,都将后台模式值保存到