iBeacons 背景与 Altbeacon 库

iBeacons' background ranging with Altbeacon Library

我需要使用 Altbeacons 库在后台对信标进行测距(在前台模式下正常工作)

在我的 android 监视器上,日志从未显示 didRangeBeaconsInRegion 方法的消息。

didEnterRegion 上,消息显示正确。

我尝试了下面的代码但没有成功,谁能指导我怎么做?

public class INBeaconApplication extends Application implements BootstrapNotifier, RangeNotifier {
private BeaconManager beaconManager;
private ArrayList<INBeacon> userBeaconArrayList;
private INBeaconUser inBeaconUser;

@Override
public void onCreate() {
    super.onCreate();
    Log.d("INBeacon", "Application created");

    //BeaconManager and custom beaconParser layout
    beaconManager = BeaconManager.getInstanceForApplication(this);
    beaconManager.getBeaconParsers().clear();
    beaconManager.getBeaconParsers().add(new BeaconParser()
            .setBeaconLayout("m:2-3=0215,i:4-19,i:20-21,i:22-23,p:24-24"));
    beaconManager.setRegionStatePeristenceEnabled(false);

    //Set the BeacomManager background between scan periods
    long period = 1000 * 30; //equal to 30 seconds
    beaconManager.setBackgroundBetweenScanPeriod(period);

    // wake up the app when a beacon is seen
    Region region = new Region(Utils.REGION_ID, Identifier
            .parse(Utils.INBEACON_UUID), null, null);
    new RegionBootstrap(this, region);

    new BackgroundPowerSaver(this);
}



boolean checkPermissionForRange(){
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        return getApplicationContext().checkSelfPermission(
                Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED;
    }
    return true;
}

@Override
public void didEnterRegion(Region region) {
    Log.d("INBeacon", "didEnterRegion");
    Utils.sendNotification(this, "Test");
    if (checkPermissionForRange()) return;
    Log.d("INBeacon", "permissions ok");
    try {
        beaconManager.startRangingBeaconsInRegion(region);
        Log.d("INBeacon","startRanging");
    } catch (RemoteException e) {
        Log.e("INBeacon",e.getMessage());
    }
}

@Override
public void didExitRegion(Region region) {
    Log.d("INBeacon", "didExitRegion");
    try {
        beaconManager.stopRangingBeaconsInRegion(region);
    } catch (RemoteException e) {
        e.printStackTrace();
    }
}

@Override
public void didRangeBeaconsInRegion(Collection<Beacon> collection, Region region) {
    Log.d("INBeacon", "didRangeBeaconsInRegion");
    ArrayList<Beacon> newBeacons = new ArrayList<>(collection);
      .............code to do what i need with the beacons.....
}

看起来代码只需要调用:

 beaconManager.setRangeNotifier(this);