Search code examples
androidrunnableibeacon-androidestimote

Execute BEACON method every 100 milliseconds in Android


I try do indoor navigation android application using Estimote beacons. Here is the code which I used to get distance between android device and beacons. This code sections runs approximately in every 1 second.

I need to execute this in every 100 milliseconds.

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("###################### ", " #################");
      }
    });
  }
});

How do I do that?


Solution

  • I got the answer for this problem. Here is the answer and explanation for the Estimote Beacon users and lovers.

    There is a method called setForegroundScanPeriod in the Estimote Android SDK. This method implemented in BeaconManager class. This can be used to increase or decrease scanning time period.

    This is the method definition

    setForegroundScanPeriod(long scanPeriodMillis,long waitTimeMillis)
    

    First parameter for the change the scan period and second parameter for the waiting time between two scanning period. Values of all parameters are taken as milliseconds. Example as follows.

    setForegroundScanPeriod(200,5)
    

    If you called like this, Then code section is executing in every 200 milliseconds with 5 milliseconds time interval.

    Default values of the method is 1s for the scan time period and 0s for the waiting period. 200ms is minimum scan time period and 0ms is minimum waiting period. (if you reduce the scanning period scanning period, Broadcasting interval of your Estimote Beacon should be reduced. Broadcasting interval should less than scan time period and also minimum Broadcasting interval is 100ms)

    Reducing Broadcast interval is negatively effect for Battery life of Beacon and Reducing scan time period is negatively effect for Battery life of Android device.

    Here is the full example which I used to scan 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);
          }
        });
      }
    });