【问题标题】:iBeacons' background ranging with Altbeacon LibraryiBeacon 的背景与 Altbeacon 库
【发布时间】:2016-11-10 12:51:19
【问题描述】:

我需要使用 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.....
}

【问题讨论】:

    标签: android ibeacon altbeacon


    【解决方案1】:

    看起来代码只需要调用:

     beaconManager.setRangeNotifier(this);
    

    【讨论】:

    • 大卫,我添加了它现在可以工作了,但是这个方法显示为已弃用,这样可以吗?
    • 另外,这使它像应用程序打开时一样进行扫描,还有另一种方法可以让它只在一段时间内查找它们,比如我设置 beaconManager.setBackgroundBetweenScanPeriod(period);跨度>
    • 抱歉,不推荐使用的版本是 beaconManager.addRangeNotifier(this); 根据您的后台扫描设置,您应该会看到一次测距停止 30 秒。
    猜你喜欢
    • 1970-01-01
    • 2014-10-25
    • 1970-01-01
    • 2023-03-30
    • 1970-01-01
    • 1970-01-01
    • 2016-04-23
    • 2022-07-21
    • 1970-01-01
    相关资源
    最近更新 更多