【问题标题】:Execute BEACON method every 100 milliseconds in Android在Android中每100毫秒执行一次BEACON方法
【发布时间】:2016-10-05 15:00:17
【问题描述】:

我尝试使用 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 秒?
  • 编辑了代码。我不知道时间限制为 1 秒的原因。我需要每 100 毫秒执行一次此代码。
  • 显然您正在使用 Estimote 的 Android SDK。显然,Estimote 的聪明头脑已经得出结论,考虑到所有不同的因素,如信标的广告频率、人们通常仅移动 1 米/秒等,1 秒的扫描间隔是最佳的。显然,他们的 SDK 仅作为AAR 库,所以逆向工程有点困难,但并非不可能。但可能许可证不允许“破解”它。我不知道。可能在BeaconManager 类中的某个地方隐藏着扫描频率的秘密。
  • 你可能会得到一个更有教育意义的答案on their own forum
  • @MarkusKauppinen 感谢您的评论。我使用 timetask 来做到这一点。但这不是解决方案。我需要将扫描间隔减少到 100 毫秒。 Estimote Beacon 的发射功率可以降低到 100 毫秒。我会遵循这个。感谢您的建议

标签: android runnable ibeacon-android estimote


【解决方案1】:

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

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

这是方法定义

setForegroundScanPeriod(long scanPeriodMillis,long waitTimeMillis)

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

setForegroundScanPeriod(200,5)

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

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

减少广播间隔对 Beacon 的电池寿命有负面影响,减少扫描时间对 Android 设备的电池寿命有负面影响。

这是我用来扫描信标的完整示例。

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);
      }
    });
  }
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-12-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-20
    • 2019-03-20
    相关资源
    最近更新 更多