在Android中每100毫秒执行一次BEACON方法

时间:2016-10-05 15:00:17

标签: android runnable ibeacon-android estimote

我尝试使用Estimote信标做室内导航android应用程序。这是我用来获取Android设备和信标之间距离的代码。此代码段大约每1秒运行一次。

我需要每100毫秒执行一次。

beaconManager.setRangingListener(new BeaconManager.RangingListener() {
  @Override public void onBeaconsDiscovered(Region region, final List<Beacon> beacons) {
    runOnUiThread(new Runnable() {
      @Override public void run() {

        long time= System.currentTimeMillis();
        Log.i("###################### ", " #################");
        Log.i("Time Class ", " Time value in millisecinds "+time);

        toolbar.setSubtitle("Found beacons: " + beacons.size());
        ArrayList<Beacon> newBeacons = new ArrayList<>();

        for (int x=0; x<beacons.size();x++) {
          int major= beacons.get(x).getMajor();
          int minor = beacons.get(x).getMinor();

          if (major==3&&minor==3) {
            newBeacons.add(beacons.get(x));
            Dsi[0] = Utils.computeAccuracy(beacons.get(x));
          }
          else if (major==4&&minor==4) {
            newBeacons.add(beacons.get(x));
            Dsi[1] = Utils.computeAccuracy(beacons.get(x));
          }
          else if (major==2&&minor==2) {
            newBeacons.add(beacons.get(x));
            Dsi[2] = Utils.computeAccuracy(beacons.get(x));
          }
        }

        double[][] positions = new double[][] { { -3.4, 0.8}, { 0, 7.5 }, { 6.7, 6.7 } };
        double[] distances = new double[] { Dsi[0], Dsi[1], Dsi[2] };

        TrilaterationFunction trilaterationFunction = new TrilaterationFunction(positions, distances);
        LinearLeastSquaresSolver lSolver = new LinearLeastSquaresSolver(trilaterationFunction);
        NonLinearLeastSquaresSolver nlSolver = new NonLinearLeastSquaresSolver(trilaterationFunction, new LevenbergMarquardtOptimizer());

        double[] expectedPosition = new double[] { 3.3, 15.0 };
        RealVector x = lSolver.solve();
        Optimum optimum = nlSolver.solve();
        testResults(expectedPosition, 1, optimum, x);
        adapter.replaceWith(newBeacons);

        time= System.currentTimeMillis();
        Log.i("Time Class ", " Time value in millisecinds "+time);
        Log.i("###################### ", " #################");
      }
    });
  }
});

我该怎么做?

1 个答案:

答案 0 :(得分:0)

我得到了这个问题的答案。以下是Estimote Beacon用户和爱好者的答案和解释。

Estimote Android SDK中有一种名为 setForegroundScanPeriod 的方法。此方法在 BeaconManager 类中实现。这可用于增加或减少扫描时间段。

这是方法定义

setForegroundScanPeriod(long scanPeriodMillis,long waitTimeMillis)

第一个参数用于更改扫描周期,第二个参数用于两个扫描周期之间的等待时间。所有参数的值都以毫秒为单位。示例如下。

setForegroundScanPeriod(200,5)

如果你这样调用,那么代码部分每200毫秒执行一次,时间间隔为5毫秒。

扫描时间段的方法默认值为1秒,等待时间为0秒。 200ms 是最短扫描时间段, 0ms 是最短等待时间。 (如果减少扫描周期扫描周期,应减少Estimote Beacon的广播间隔。广播间隔应小于扫描时间段,最小广播间隔为100ms)

减少广播间隔对信标的电池寿命产生负面影响,缩短扫描时间对Android设备的电池寿命有负面影响。

以下是我用来扫描Beacons的完整示例。

BeaconManager beaconManager = new BeaconManager(this);

beaconManager.setForegroundScanPeriod(200,0);

beaconManager.setRangingListener(new BeaconManager.RangingListener() {
  @Override public void onBeaconsDiscovered(Region region, final List<Beacon> beacons) {
    runOnUiThread(new Runnable() {
      @Override public void run() {

        toolbar.setSubtitle("Found beacons: " + beacons.size());

        ArrayList<Beacon> newBeacons = new ArrayList<>();

        for (int x=0; x<beacons.size();x++) {
          int major= beacons.get(x).getMajor();
          int minor = beacons.get(x).getMinor();

          if (major==3&&minor==3) {
            newBeacons.add(beacons.get(x));
            Dsi[0] = Utils.computeAccuracy(beacons.get(x));
          }
          else if (major==4&&minor==4) {
            newBeacons.add(beacons.get(x));
            Dsi[1] = Utils.computeAccuracy(beacons.get(x));
          }
          else if (major==2&&minor==2) {
            newBeacons.add(beacons.get(x));
            Dsi[2] = Utils.computeAccuracy(beacons.get(x));
          }
        }

        adapter.replaceWith(newBeacons);
      }
    });
  }
});