代码之家  ›  专栏  ›  技术社区  ›  Darren

引起扫描计划任务的android altbeacon

  •  0
  • Darren  · 技术社区  · 6 年前

    我正在使用AltBeacon库中的测距通知程序对信标进行30秒的测距。范围通知程序在应用程序类中,没有实现引导通知程序或beaconconsumer。程序在前台运行时会生成下面的代码,它不应该到达后台。

    如果应用程序在前台运行,为什么会发生这种情况?

    关于手机:Android 9.0谷歌像素2 图书馆版本:AltBeacon 2.15.2

    11-28 16:43:44.052 13258-13258/com.testapp.app W/JobInfo: Requested interval +5m0s0ms for job 208352940 is too small; raising to +15m0s0ms
    Requested flex 0 for job 208352940 is too small; raising to +5m0s0ms
     11-28 16:44:24.276 13258-13258/com.testapp.app W/JobInfo: Requested interval +5m0s0ms for job 208352940 is too small; raising to +15m0s0ms
    Requested flex 0 for job 208352940 is too small; raising to +5m0s0ms
    11-28 16:44:24.332 13258-13258/com.testapp.app W/JobInfo: Requested interval +5m0s0ms for job 208352940 is too small; raising to +15m0s0ms
    11-28 16:44:24.426 13258-13258/com.testapp.app W/JobInfo: Requested interval +5m0s0ms for job 208352940 is too small; raising to +15m0s0ms
    Requested flex 0 for job 208352940 is too small; raising to +5m0s0ms
    11-28 16:44:25.010 13258-13258/com.testapp.app W/JobInfo: Requested interval +5m0s0ms for job 208352940 is too small; raising to +15m0s0ms
    Requested flex 0 for job 208352940 is too small; raising to +5m0s0ms
    

    下面是我认为与AltBeacon库相关的代码。

    代码上下文:我在一个扩展应用程序的类中实现了范围通知程序和引导通知程序。当调用引导通知程序方法或从有问题的活动调用StartRanging方法时。当用户点击另一个活动的按钮时,就会调用该活动,用户可以随时返回到旧的活动。一旦它们在这两个活动之间进行,上面的日志就会生成。我的范围只有30秒,一旦超过30秒,就会调用停止范围。

    扩展应用程序的类:

    Oncreate:

        beaconManager = BeaconManager.getInstanceForApplication(this);
    
        //Set the time the app last scanned
        scanningTime = 0;
        backgroundPowerSaver = new BackgroundPowerSaver(this);
    
        beaconManager.getBeaconParsers().add(new BeaconParser().setBeaconLayout("m:2-3=0215,i:4-19,i:20-21,i:22-23,p:24-24")); //iBeacon
    
        Region beaconRegion1 = new Region("Beacon1", Identifier.parse("HidingUUID1"), null, null);
        Region beaconRegion2 = new Region("Beacon2", Identifier.parse("HidingUUID2"), null, null);
        Region beaconRegion3 = new Region("Beacon3", Identifier.parse("HidingUUID3"), null, null);
        Region beaconRegion4 = new Region("Beacon4", Identifier.parse("HidingUUID4"), null, null);
        Region beaconRegion5 = new Region("Beacon5", Identifier.parse("HidingUUID5"), null, null);
    
        rangingRegion = new Region("RangingRegion", null, null, null);
        regionBootstrap = new RegionBootstrap(this, beaconRegion1);
        regionBootstrap.addRegion(beaconRegion2);
        regionBootstrap.addRegion(beaconRegion3);
        regionBootstrap.addRegion(beaconRegion4);
        regionBootstrap.addRegion(beaconRegion5);
    

    引导通知程序方法:

    @Override
    public void didEnterRegion(Region region) {
        beaconRegionEvent = true;
        StartRanging();
    }
    
    @Override
    public void didExitRegion(Region region) {
        beaconRegionEvent = true;
        StartRanging();
    }
    
    @Override
    public void didDetermineStateForRegion(int i, Region region) {
        if (i == 1) {
            beaconRegionEvent = true;
            StartRanging();
        }
    }
    

    开始测距:检查测距是否发生,如果没有,则开始测距。

    public boolean StartRanging() {
    
        long now = System.currentTimeMillis();
        if (now - scanningTime <= Globals.MAX_RANGING_PERIOD * 1000 && scanningTime != 0) {
            Timber.e("Minimum time since previous scan has not passed");
            return false;
        }
        try {
            beaconManager.startRangingBeaconsInRegion(rangingRegion);
            Timber.e("Starting Range");
    
            //Scanning is about to happen so set the scan time
            scanningTime = System.currentTimeMillis();
    
        } catch (RemoteException e) {
            Timber.e("Unable to initialize ranging");
            e.printStackTrace();
            return false;
        }
    
        //Add this range notifier to the beacon manager
        if (beaconManager.getRangingNotifiers().size() < 1) {
            beaconManager.addRangeNotifier(this);
        }
    
        return true;
    }
    

    StopRanging:

    public void StopRanging() {
        try {
            //Remove the regions being ranged
            beaconManager.stopRangingBeaconsInRegion(rangingRegion);
        } catch (RemoteException e) {
            e.printStackTrace();
            Timber.e("Cant stop ranging  " + e.toString());
        }
        Timber.d("Stopped Beacon Ranging");
    }
    

    区域范围指示灯

    @Override
    public void didRangeBeaconsInRegion(Collection<Beacon> beacons, Region region) {
        long now = System.currentTimeMillis();
        if (now - scanningTime > Globals.MAX_RANGING_PERIOD * 1000 && scanningTime != 0) {
            StopRanging();
        }
    
    }
    

    扫描太频繁的活动在代码中发生:每秒打开此活动,我们称之为startranging方法。

    在onCreate()中调用runnable

    Runnable ScanTimer = new Runnable() {
        @Override
        public void run() {
            Timber.d("In Runnable");
            handler.postDelayed(ScanTimer, 1000);
    
            Timber.d("Checks were good starting ranging");
    
            App.getInstance().StartRanging();
    
        }
    };
    
    1 回复  |  直到 6 年前
        1
  •  1
  •   davidgyoung    6 年前

    了解信标监视API(以RegionBootstrap启动的API)和范围API(以StartrangingBeaconsInRegion启动的API)都需要信标扫描,这在库中由计划的作业执行扫描。

    因此,即使代码停止在计时器上进行范围调整,它也不会停止监视,因为监视会无限期地继续。这就是日志显示为扫描计划的连续作业的原因。如果要停止所有扫描,必须禁用监视 regionBootstrap.disable();