AltBeacon 监控和测距问题 android

Issue in AltBeacon Monitoring and Ranging android

我正在使用此代码在 Altbeacon 中进行测距和监控

 class Appclass extends Application implements BootstrapNotifier {
private RegionBootstrap regionBootstrap;
private BackgroundPowerSaver backgroundPowerSaver;
private boolean haveDetectedBeaconsSinceBoot = false;
public static Region region1;
private static boolean activityVisible;
public static Appclass instances;
public static BeaconManager beaconManager;
public void onCreate() {
    super.onCreate();
    instances = this;beaconManager=org.altbeacon.beacon.BeaconManager.getInstanceForApplication(this);

       beaconManager.getBeaconParsers().add(new BeaconParser()
            .setBeaconLayout("m:2-3=0215,i:4-19,i:20-21,i:22-23,p:24-24"));
    beaconManager.setForegroundScanPeriod(1100l);
    beaconManager.setForegroundBetweenScanPeriod(0l);
    beaconManager.setAndroidLScanningDisabled(true);
    beaconManager.setBackgroundBetweenScanPeriod(01);
    beaconManager.setBackgroundScanPeriod(1100l);
    try {
        beaconManager.updateScanPeriods();

    } catch (Exception e) {
    }
    Log.d("", "setting up background monitoring for beacons and power                 saving");
    // wake up the app when a beacon is seen
    region1 = new Region("backgroundRegion",
            null, null, null);
    regionBootstrap = new RegionBootstrap(this, region1);
    backgroundPowerSaver = new BackgroundPowerSaver(this);

}

@Override
public void didEnterRegion(Region region) {
    Log.d("ABC", "Enter");
    Log.d("ABC value", region.getId2() + " " + region.getId3());
    try { Intent i = new Intent(getApplicationContext(), AbcService.class);

            startService(i);
    } catch (Exception e) {
    }
}
@Override
public void didExitRegion(Region region) {
    Log.d("ABC", "exit");
}

@Override
public void didDetermineStateForRegion(int i, Region region) {

}

然后在我的 AbcService class 代码中是:

class AbcService extends Service implements BeaconConsumer, MonitorNotifier, RangeNotifier {
@Override
public IBinder onBind(Intent intent) {
    return null;
}

@Override
public void onCreate() {
    Appclass.beaconManager.bind(this);
    super.onCreate();

}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    try {
    } catch (Exception e) {

    }


    return START_STICKY;
}

@Override
public void didRangeBeaconsInRegion(Collection<Beacon> collection, Region region) {
    Log.d("ABC", "range");
}

@Override
public void onBeaconServiceConnect() {
    Appclass.beaconManager.setMonitorNotifier(this);
    Appclass.beaconManager.setRangeNotifier(this);

}

@Override
public void didEnterRegion(Region region) {
    try {
        Log.d("ABC", "didEnterRegion");
        Log.d("ABC", "" + region.getId2() + region.getId3());
        Appclass.beaconManager.startRangingBeaconsInRegion(Appclass.region1);

    } catch (RemoteException e) {
        e.printStackTrace();
    }
}

@Override
public void didExitRegion(Region region) {
    try {
        Log.d("ABC", "didExitRegion");
         Appclass.beaconManager.stopRangingBeaconsInRegion(Appclass.region1);

    } catch (Exception e) {
        e.printStackTrace();
    }
}

@Override
public void didDetermineStateForRegion(int i, Region region) {

}

}

现在的问题是,当我们进入信标范围时,Application class 的 didEnterRegion 方法每次都会调用,但 AbcService class 的 didEnterRegion 方法不会调用有一段时间又开始测距不了。我的代码有什么问题?

几点提示:

  • 在开始监控的同时开始测距。没有理由等待进入区域回调或退出区域时停止测距。

  • 将日志记录添加到 didDetermineStateFirRegion。当应用程序启动后出现信标时会调用此方法,即使设备已经在之前 运行 的区域中。 didEnterRegion 仅当与该区域匹配的信标实际消失 15 秒以上然后重新出现时才会被调用。