扫描作业在 15 分钟后继续正常调用,并在区域匹配时停止



根据我在 AltBeacon 库文档中的理解,ScanJob 应该在与应用程序最后一次交互 15 分钟后停止调用。

我已经开始监视和范围某些区域,并且已禁用所有信标, 因此没有检测到信标.启动应用程序后,我已经杀死了它,并且在日志中我看到即使在 15 分钟后也会连续调用 ScanJob。

03-22 14:17:46.047 10755-10755/ I/ScanJob: Using immediateScanJobId from manifest: 208352939
03-22 14:17:46.061 10755-10755/ I/ScanJob: Using periodicScanJobId from manifest: 208352940
03-22 14:17:46.061 10755-10755 W/JobInfo: Specified interval for 208352940 is +5m10s0ms. Clamped to +15m0s0ms
03-22 14:17:46.061 10755-10755/ W/JobInfo: Specified flex for 208352940 is 0. Clamped to +5m0s0ms
03-22 14:17:46.061 10755-10755/ W/JobInfo: Specified interval for 208352940 is +5m10s0ms. Clamped to +15m0s0ms
03-22 14:17:46.062 10755-10755/ W/JobInfo: Specified flex for 208352940 is 0. Clamped to +5m0s0ms
03-22 14:17:46.178 10755-10755/ I/CycledLeScanner: Using Android O scanner
03-22 14:17:46.180 10755-10755/ I/ScanJob: Using immediateScanJobId from manifest: 208352939
03-22 14:17:46.180 10755-10755/ I/ScanJob: Running immediate scan job: instance is org.altbeacon.beacon.service.ScanJob@6c0d823
03-22 14:17:46.181 10755-10755/ I/ScanJob: scanJob version 2.13.1 is starting up on the main process
03-22 14:17:46.184 10755-10755/ W/ModelSpecificDistanceCalculator: Cannot find match for this device.  Using default
03-22 14:17:46.184 10755-10755/ W/ModelSpecificDistanceCalculator: Cannot find match for this device.  Using default
03-22 14:17:46.184 10755-10755/ I/BluetoothAdapter: getBluetoothLeScanner
03-22 14:17:46.234 10755-10755/ D/BluetoothAdapter: isLeEnabled(): ON
03-22 14:17:46.244 10755-10755/ I/BluetoothAdapter: getBluetoothLeScanner
03-22 14:17:46.246 10755-12617/ D/BluetoothAdapter: isLeEnabled(): ON
03-22 14:17:46.250 10755-10755/ I/ScanJob: Scan job running for 10000 millis
03-22 14:17:46.250 10755-11219/ D/BluetoothLeScanner: onScannerRegistered() - status=0 scannerId=6 mScannerId=0
03-22 14:17:46.725 10755-12622/ I/ScanHelper: Non-distinct packets detected in a single scan.  Restarting scans unecessary.
03-22 14:17:56.251 10755-10755/ I/ScanJob: Scan job runtime expired: org.altbeacon.beacon.service.ScanJob@6c0d823
03-22 14:17:56.262 10755-12617/ D/BluetoothAdapter: isLeEnabled(): ON
03-22 14:17:56.299 10755-10755/ I/BluetoothAdapter: getBluetoothLeScanner
03-22 14:17:56.302 10755-10755/ D/BluetoothAdapter: isLeEnabled(): ON
03-22 14:17:56.533 10755-10755/ I/ScanJob: Using immediateScanJobId from manifest: 208352939
03-22 14:17:56.542 10755-10755/ I/ScanJob: Using periodicScanJobId from manifest: 208352940
03-22 14:17:56.542 10755-10755/ W/JobInfo: Specified interval for 208352940 is +5m10s0ms. Clamped to +15m0s0ms
03-22 14:17:56.542 10755-10755/ W/JobInfo: Specified flex for 208352940 is 0. Clamped to +5m0s0ms
03-22 14:17:56.542 10755-10755/ W/JobInfo: Specified interval for 208352940 is +5m10s0ms. Clamped to +15m0s0ms
03-22 14:17:56.542 10755-10755/ W/JobInfo: Specified flex for 208352940 is 0. Clamped to +5m0s0ms
03-22 14:17:56.663 10755-10755/ I/CycledLeScanner: Using Android O scanner
03-22 14:17:56.666 10755-10755/ I/ScanJob: Using immediateScanJobId from manifest: 208352939
03-22 14:17:56.667 10755-10755/ I/ScanJob: Running immediate scan job: instance is org.altbeacon.beacon.service.ScanJob@ab5d8a
03-22 14:17:56.668 10755-10755/ I/ScanJob: scanJob version 2.13.1 is starting up on the main process
03-22 14:17:56.669 10755-10755/ W/ModelSpecificDistanceCalculator: Cannot find match for this device.  Using default
03-22 14:17:56.670 10755-10755/ W/ModelSpecificDistanceCalculator: Cannot find match for this device.  Using default
03-22 14:17:56.671 10755-10755/ I/BluetoothAdapter: getBluetoothLeScanner
03-22 14:17:56.718 10755-10755/ D/BluetoothAdapter: isLeEnabled(): ON
03-22 14:17:56.730 10755-10755/ I/BluetoothAdapter: getBluetoothLeScanner
03-22 14:17:56.733 10755-12627/ D/BluetoothAdapter: isLeEnabled(): ON
03-22 14:17:56.739 10755-10755/ I/ScanJob: Scan job running for 10000 millis
03-22 14:17:56.739 10755-11219/ D/BluetoothLeScanner: onScannerRegistered() - status=0 scannerId=6 mScannerId=0

如果我没有与手机交互并且没有任何与受监控区域匹配的信标,它不应该在 15 分钟后停止吗?

另外, 当我连接信标时, 它被检测到, 但随后扫描作业停止调用.

03-22 14:19:55.150 10755-10755/ D/.MyApplication: I have just switched from seeing/not seeing beacons: 1
03-22 14:19:55.152 10755-10755/ D/.MyApplication: did enter Bootstrap region.id1: 0040c159-12f6-4fc3-9189-87c069ffe5cf id2: 5 id3: null
03-22 14:19:55.152 10755-10755/ D/.MyApplication: isAnyConsumerBound? -->false
03-22 14:19:55.186 10755-10755/ I/ScanJob: Using immediateScanJobId from manifest: 208352939
03-22 14:19:55.188 10755-10755/ I/ScanJob: Using periodicScanJobId from manifest: 208352940
03-22 14:19:55.189 10755-10755/ W/JobInfo: Specified interval for 208352940 is +5m0s0ms. Clamped to +15m0s0ms
03-22 14:19:55.189 10755-10755/ W/JobInfo: Specified flex for 208352940 is 0. Clamped to +5m0s0ms
03-22 14:19:55.189 10755-10755/ W/JobInfo: Specified interval for 208352940 is +5m0s0ms. Clamped to +15m0s0ms
03-22 14:19:55.189 10755-10755/ W/JobInfo: Specified flex for 208352940 is 0. Clamped to +5m0s0ms
03-22 14:19:55.232 10755-10755/ I/ScanJob: Using immediateScanJobId from manifest: 208352939
03-22 14:19:55.233 10755-10755/ I/ScanJob: Using periodicScanJobId from manifest: 208352940
03-22 14:19:55.234 10755-10755/ W/JobInfo: Specified interval for 208352940 is +5m0s0ms. Clamped to +15m0s0ms
03-22 14:19:55.234 10755-10755/ W/JobInfo: Specified flex for 208352940 is 0. Clamped to +5m0s0ms
03-22 14:19:55.234 10755-10755/ W/JobInfo: Specified interval for 208352940 is +5m0s0ms. Clamped to +15m0s0ms
03-22 14:19:55.234 10755-10755/ W/JobInfo: Specified flex for 208352940 is 0. Clamped to +5m0s0ms
03-22 14:19:55.276 10755-10755/ I/ScanJob: Using immediateScanJobId from manifest: 208352939
03-22 14:19:55.278 10755-10755/ I/ScanJob: Using periodicScanJobId from manifest: 208352940
03-22 14:19:55.278 10755-10755/ W/JobInfo: Specified interval for 208352940 is +5m0s0ms. Clamped to +15m0s0ms
03-22 14:19:55.278 10755-10755/ W/JobInfo: Specified flex for 208352940 is 0. Clamped to +5m0s0ms
03-22 14:19:55.278 10755-10755/ W/JobInfo: Specified interval for 208352940 is +5m0s0ms. Clamped to +15m0s0ms
03-22 14:19:55.278 10755-10755/ W/JobInfo: Specified flex for 208352940 is 0. Clamped to +5m0s0ms
03-22 14:19:55.321 10755-10755/ I/ScanJob: Using immediateScanJobId from manifest: 208352939
03-22 14:19:55.322 10755-10755/ I/ScanJob: Using periodicScanJobId from manifest: 208352940
03-22 14:19:55.323 10755-10755/ W/JobInfo: Specified interval for 208352940 is +5m0s0ms. Clamped to +15m0s0ms
03-22 14:19:55.323 10755-10755/ W/JobInfo: Specified flex for 208352940 is 0. Clamped to +5m0s0ms
03-22 14:19:55.323 10755-10755/ W/JobInfo: Specified interval for 208352940 is +5m0s0ms. Clamped to +15m0s0ms
03-22 14:19:55.323 10755-10755/ W/JobInfo: Specified flex for 208352940 is 0. Clamped to +5m0s0ms
03-22 14:19:55.368 10755-10755/ I/ScanJob: Using immediateScanJobId from manifest: 208352939
03-22 14:19:55.369 10755-10755/ I/ScanJob: Using periodicScanJobId from manifest: 208352940
03-22 14:19:55.369 10755-10755/ W/JobInfo: Specified interval for 208352940 is +5m0s0ms. Clamped to +15m0s0ms
03-22 14:19:55.369 10755-10755/ W/JobInfo: Specified flex for 208352940 is 0. Clamped to +5m0s0ms
03-22 14:19:55.369 10755-10755/ W/JobInfo: Specified interval for 208352940 is +5m0s0ms. Clamped to +15m0s0ms
03-22 14:19:55.369 10755-10755/ W/JobInfo: Specified flex for 208352940 is 0. Clamped to +5m0s0ms
03-22 14:19:55.412 10755-10755/ I/ScanJob: Using immediateScanJobId from manifest: 208352939
03-22 14:19:55.413 10755-10755/ I/ScanJob: Using periodicScanJobId from manifest: 208352940
03-22 14:19:55.414 10755-10755/ W/JobInfo: Specified interval for 208352940 is +5m0s0ms. Clamped to +15m0s0ms
03-22 14:19:55.414 10755-10755/ W/JobInfo: Specified flex for 208352940 is 0. Clamped to +5m0s0ms
03-22 14:19:55.414 10755-10755/ W/JobInfo: Specified interval for 208352940 is +5m0s0ms. Clamped to +15m0s0ms
03-22 14:19:55.414 10755-10755/ W/JobInfo: Specified flex for 208352940 is 0. Clamped to +5m0s0ms
03-22 14:19:55.468 10755-10755/ I/ScanJob: Using immediateScanJobId from manifest: 208352939
03-22 14:19:55.469 10755-10755/ I/ScanJob: Using periodicScanJobId from manifest: 208352940
03-22 14:19:55.470 10755-10755/ W/JobInfo: Specified interval for 208352940 is +5m0s0ms. Clamped to +15m0s0ms
03-22 14:19:55.470 10755-10755/ W/JobInfo: Specified flex for 208352940 is 0. Clamped to +5m0s0ms
03-22 14:19:55.470 10755-10755/ W/JobInfo: Specified interval for 208352940 is +5m0s0ms. Clamped to +15m0s0ms
03-22 14:19:55.470 10755-10755/ W/JobInfo: Specified flex for 208352940 is 0. Clamped to +5m0s0ms
03-22 14:19:55.527 10755-10755/ I/ScanJob: Using immediateScanJobId from manifest: 208352939
03-22 14:19:55.529 10755-10755/ I/ScanJob: Using periodicScanJobId from manifest: 208352940
03-22 14:19:55.529 10755-10755/ W/JobInfo: Specified interval for 208352940 is +5m0s0ms. Clamped to +15m0s0ms
03-22 14:19:55.529 10755-10755/ W/JobInfo: Specified flex for 208352940 is 0. Clamped to +5m0s0ms
03-22 14:19:55.529 10755-10755/ W/JobInfo: Specified interval for 208352940 is +5m0s0ms. Clamped to +15m0s0ms
03-22 14:19:55.529 10755-10755/ W/JobInfo: Specified flex for 208352940 is 0. Clamped to +5m0s0ms
03-22 14:19:55.584 10755-10755/ I/ScanJob: Using immediateScanJobId from manifest: 208352939
03-22 14:19:55.585 10755-10755/ I/ScanJob: Using periodicScanJobId from manifest: 208352940
03-22 14:19:55.586 10755-10755/ W/JobInfo: Specified interval for 208352940 is +5m0s0ms. Clamped to +15m0s0ms
03-22 14:19:55.586 10755-10755/ W/JobInfo: Specified flex for 208352940 is 0. Clamped to +5m0s0ms
03-22 14:19:55.586 10755-10755/ W/JobInfo: Specified interval for 208352940 is +5m0s0ms. Clamped to +15m0s0ms
03-22 14:19:55.586 10755-10755/ W/JobInfo: Specified flex for 208352940 is 0. Clamped to +5m0s0ms
03-22 14:19:55.638 10755-10755/ I/ScanJob: Using immediateScanJobId from manifest: 208352939
03-22 14:19:55.639 10755-10755/ I/ScanJob: Using periodicScanJobId from manifest: 208352940
03-22 14:19:55.639 10755-10755/ W/JobInfo: Specified interval for 208352940 is +5m0s0ms. Clamped to +15m0s0ms
03-22 14:19:55.639 10755-10755/ W/JobInfo: Specified flex for 208352940 is 0. Clamped to +5m0s0ms
03-22 14:19:55.640 10755-10755/ W/JobInfo: Specified interval for 208352940 is +5m0s0ms. Clamped to +15m0s0ms
03-22 14:19:55.640 10755-10755/ W/JobInfo: Specified flex for 208352940 is 0. Clamped to +5m0s0ms
03-22 14:19:55.659 10755-10755/ I/ScanJob: Using periodicScanJobId from manifest: 208352940
03-22 14:19:55.659 10755-10755/ I/ScanJob: onStopJob called for immediate scan org.altbeacon.beacon.service.ScanJob@e0ea913
03-22 14:19:55.663 10755-12713/ D/BluetoothAdapter: isLeEnabled(): ON
03-22 14:19:55.664 10755-10755/ I/ScanJob: We are inside a beacon region.  We will not scan between cycles.

这是预期的行为吗?我可能配置错误了某些东西吗?

编辑以添加一些代码和信息

我正在尝试的是使连续后台扫描不绑定到活动。因此,我将开始测距和监视所需的所有代码放在MyApplicationApplication扩展的类中。

@Override
public void onCreate() {
if (Utils.checkPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION)) {
beaconManager = BeaconManager.getInstanceForApplication(this);
initBeaconParsers(); //In this method I add some custo beacon parsers
//wake up the app when any beacon is seen
List<Region> regions = setupBeaconRegions();
regionBootstrap = new RegionBootstrap(this, regions);
backgroundPowerSaver = new BackgroundPowerSaver(this);
beaconManager.setBackgroundScanPeriod(3000l);
beaconManager.setBackgroundBetweenScanPeriod(15000l);
beaconManager.setRegionStatePersistenceEnabled(false);
beaconManager.bind(this);
}
}
private List<Region> setupBeaconRegions(){
List<Region> regions = new ArrayList<>();
region = new Region(Constants.REGION, Identifier.parse(BuildConfig.UUID_1), null, null);
region2= new Region(Constants.REGION_2, Identifier.parse(BuildConfig.UUID_2), null, null);
region3= new Region(Constants.REGION_3, Identifier.parse(BuildConfig.UUID_3), null, null);
region4 = new Region(Constants.REGION_4, Identifier.parse(BuildConfig.UUID_4), null, null);
region5= new Region(Constants.REGION_5, Identifier.parse(BuildConfig.UUID_5), null, null);
pushRegion = new Region(Constants.PUSH_REGION, Identifier.parse(BuildConfig.PUSH_UUID_1), null, null);
pushRegion2= new Region(Constants.PUSH_REGION_2, Identifier.parse(BuildConfig.PUSH_UUID_2), null, null);
pushRegion3= new Region(Constants.PUSH_REGION_3, Identifier.parse(BuildConfig.PUSH_UUID_3), null, null);
pushRegion4= new Region(Constants.PUSH_REGION_4, Identifier.parse(BuildConfig.PUSH_UUID_4), null, null);
pushRegion5= new Region(Constants.PUSH_REGION_5, Identifier.parse(BuildConfig.PUSH_UUID_5), null, null);
regions.add(region);
regions.add(region2);
regions.add(region3);
regions.add(region4);
regions.add(regio5);
regions.add(pushRegion);
regions.add(pushRegion2);
regions.add(pushRegion3);
regions.add(pushRegion4);
regions.add(pushRegion5);
return regions;
}
@Override
public void didEnterRegion(Region region) {
Log.d(TAG, "did enter Bootstrap region." + region);
startRangingBeacons();
}
private void startRangingBeacons(){
if (!isBeaconScanInitialized && !beaconManager.isAnyConsumerBound()) {
try {
Log.d(TAG, "isAnyConsumerBound? -->" + beaconManager.isAnyConsumerBound());
beaconManager.startRangingBeaconsInRegion(region);
beaconManager.startRangingBeaconsInRegion(region2);
beaconManager.startRangingBeaconsInRegion(region3);
beaconManager.startRangingBeaconsInRegion(region4);
beaconManager.startRangingBeaconsInRegion(region5);
beaconManager.startRangingBeaconsInRegion(pushRegion);
beaconManager.startRangingBeaconsInRegion(pushRegion2);
beaconManager.startRangingBeaconsInRegion(pushRegion3);
beaconManager.startRangingBeaconsInRegion(pushRegion4);
beaconManager.startRangingBeaconsInRegion(pushRegion5);
isBeaconScanInitialized = true;
} catch (RemoteException e) {
Log.d(TAG, e.getMessage());
}
}
}
@Override
public void didExitRegion(Region region) {
Log.d(TAG, "didExitRegion-->" + region.getUniqueId() + " - " + region.getId1() + " - " + region.getId2() + " - " + region.getId3());
//Here is some custom code to deal on region exit
}
@Override
public void didDetermineStateForRegion(int state, Region region) {
Log.d(TAG,"I have just switched from seeing/not seeing beacons: " + state);
}
@Override
public void onBeaconServiceConnect() {
Log.d(TAG, "onBeaconServiceConnect");
beaconManager.addRangeNotifier(new RangeNotifier() {
@Override
public void didRangeBeaconsInRegion(Collection<Beacon> arg0, Region arg1) {
// Some code to deal ranging
}
});
startRangingBeacons();
}

监控似乎工作正常,因为我在打开信标时接到电话并首次检测到.但是在我唤醒手机之前不会调用范围。

很抱歉听到这个麻烦。 目前还不清楚是什么原因导致扫描以意外的间隔发生,但我怀疑这可能与双重绑定有关。

一些提示:

  • 如果使用区域引导程序,请不要手动绑定,因为它在内部执行此操作。 删除:

    beaconManager.bind(this);

  • 同时删除以下方法,将范围通知程序设置移动到 onCreate

    onBeaconServiceConnect

  • 在通过 RegionBootstrap 绑定之前,必须设置配置更改。 因此,请将这些行移到该行上方:

    beaconManager.setBackgroundScanPeriod(3000l);
    beaconManager.setBackgroundBetweenScanPeriod(15000l);
    beaconManager.setRegionStatePersistenceEnabled(false);
    
  • 了解,在后台的Android 8上无法遵守上述后台扫描周期,操作系统将把扫描周期限制为~15秒。 我会一次在后台扫描超过 3 秒。 请考虑保留默认值 10 秒。

  • 您应该避免在Android 8上使用isAnyConsumerBound()方法,因为最新的库版本有一个错误,使其在该平台上不可靠。 (该错误已修复,等待下一个版本。无论如何,您都不需要此逻辑,并且可以安全地立即开始范围,而无需语句。

  • 请注意,范围只会在后台扫描期结束时为您提供一个回调。 因此,如果您设置正确,您将在后台每 15 分钟收到一次范围回调。

  • 如果您确实希望在 Android 8 的后台持续扫描,请考虑创建前台服务。 只需拥有一个就可以解锁保持应用程序无限期运行的能力。

最新更新