首页 文章

Android Beacon Library进入不存在的空区域

提问于
浏览
0

尝试使用Android Beacon Library检测区域时,我有一种奇怪的行为 .

我定义了2个区域来监控具有特定ID的信标 .

每次我在任何区域并输入一个,didEnterRegion回调被触发两次,一次是预期的区域,另一次是'null'区域 . 如果我进入第二个区域,它只会触发一次预期区域 .

这种行为是否正常?如何让它不触发这个'null'区域?

这是我打印的日志,以更好地显示正在发生的事情:

10-10 18:07:15.683:有一个didEnterRegion调用,id1:11111111-1111-1111-1111-111111111111 10-10 18:07:15.693:有一个didEnterRegion调用,id1:null id2:null id3:null 10 -10 18:07:22.946:有一个didEnterRegion电话,id1:00000000-0000-0000-0000-000000000000 10-10 18:07:41.880:有一个didExitRegion 11111111-1111-1111-1111-111111111111 10-10 18: 07:57.913:得到了一个didExitRegion 00000000-0000-0000-0000-000000000000 10-10 18:07:57.914:得到了一个didExitRegion null

这是我的代码的一部分:

public class BeaconReferenceApplication extends Application implements BootstrapNotifier {
    private RegionBootstrap regionBootstrap;

    public void onCreate() {
        super.onCreate();

        // UPDATE: Trying to clear old regions.
        BeaconManager beaconManager = BeaconManager.getInstanceForApplication(this);
        for (Region region: beaconManager.getMonitoredRegions()) {
            Log.i(TAG, "Clearing old monitored region" + region);
            try {
                beaconManager.stopMonitoringBeaconsInRegion(region);
            } catch (RemoteException e) {
                e.printStackTrace();
            }
        }

        ArrayList<Identifier> ids1 = new ArrayList<>(1);
        ids1.add(Identifier.parse("11111111-1111-1111-1111-111111111111"));
        Region region1 = new Region("region1", ids1);

        ArrayList<Identifier> ids0 = new ArrayList<>(1);
        ids0.add(Identifier.parse("00000000-0000-0000-0000-000000000000"));
        Region region0 = new Region("region0", ids0);


        ArrayList<Region> regions = new ArrayList<>(2);
        regions.add(region0);
        regions.add(region1);
        regionBootstrap = new RegionBootstrap(this, regions);
    }
    @Override
    public void didEnterRegion(Region region) {
        Log.i(TAG, "Got a didEnterRegion call, " + region);
    }
    @Override
    public void didExitRegion(Region region) {
        Log.i(TAG, "got a didExitRegion" + region.getId1());
    }
}

1 回答

  • 1

    我怀疑发生的事情是 the "null" region was persisted by an older version of your code. 当旧的代码版本开始监控该区域时会发生这种情况 .

    了解无论何时开始监控某个区域,Android Beacon Library都会将这些区域保留在Android设备上的非易失性存储中,因此当它重新启动时,它会记住最后一个区域状态,并知道是否触发新的区域条目事件 .

    这样做的结果是,如果您想要清除那些旧区域,则必须以编程方式执行此操作 . You can fix this with the following code:

    // Stop monitoring all currently monitored regions
        for (Region region: mBeaconManager.getMonitoredRegions()) {
            mBeaconManager.stopMonitoringBeaconsInRegion(region);
        }        '
    

相关问题