0
votes

I am new to programming iBeacons, and was wondering if someone could help me with a question about ranging updates using the AltBeacon library (android-beacon-library-2.5.1). I have some RadBeacon Dot beacons by Radius networks, which are advertising at 10Hz. I'm just trying the Ranging example from the AltBeacon Quick Start page which looks like this:

public class RangingActivity extends Activity implements BeaconConsumer {
protected static final String TAG = "RangingActivity";
private BeaconManager beaconManager;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_ranging);
    beaconManager = BeaconManager.getInstanceForApplication(this);
    // To detect proprietary beacons, you must add a line like below corresponding to your beacon
    // type.  Do a web search for "setBeaconLayout" to get the proper expression.
    // beaconManager.getBeaconParsers().add(new BeaconParser().
    //        setBeaconLayout("m:2-3=beac,i:4-19,i:20-21,i:22-23,p:24-24,d:25-25"));
    beaconManager.bind(this);
}
@Override 
protected void onDestroy() {
    super.onDestroy();
    beaconManager.unbind(this);
}
@Override
public void onBeaconServiceConnect() {
    beaconManager.setRangeNotifier(new RangeNotifier() {
        @Override 
        public void didRangeBeaconsInRegion(Collection<Beacon> beacons, Region region) {
            if (beacons.size() > 0) {
                Log.i(TAG, "The first beacon I see is about "+beacons.iterator().next().getDistance()+" meters away.");        
            }
        }
    });

    try {
        beaconManager.startRangingBeaconsInRegion(new Region("myRangingUniqueId", null, null, null));
    } catch (RemoteException e) {    }
}}

This is all straightforward, but ranging only returns range data at 1Hz via the didRangeBeaconsInRegion call back. I am wondering how to get range data more frequently from the library, and can't see how to do it. Since the beacons adevrtise at 10Hz, I'd ideally like to sample rssi values from them at this rate (or close to it). It is possible to set the bluetooth scan times to a corresonding value, but the call backs still only occur at 1 Hz. Does anyone know any way to do this?

Thanks in advance, Patrick

2

2 Answers

1
votes

If you want to change the Android Beacon Library's default scan interval to 100ms, you can do so with code like this:

 try {
     beaconManager.setForegroundScanPeriod(100l); // 100 mS
     beaconManager.setForegroundBetweenScanPeriod(0l); // 0ms
     beaconManager.updateScanPeriods();
  }
  catch (RemoteException e) {
     Log.e(TAG, "Cannot talk to service");
  }

Before you do this, however, make sure you understand a few concepts:

  1. If you reduce the scan interval from the default (1100 ms) to 100ms, you will miss a lot of packets, because packets are missed if being transmitted during the time scans are stopped and restarted.

  2. While you will get callbacks more often with a shorter scan interval, it will use more CPU and often you will get zero beacons in the callback due to the issue above.

  3. The main reason to use a beacon that advertises at 10 Hz vs. one that advertises at 1 Hz is not to get more frequent callbacks. (On iOS for example, callbacks are fixed at 1 Hz and not changeable.) The main reason for a 10 Hz beacon is to get more statistical samples for ranging for better distance estimates. The more signal strength measurements you have to work with, the more stable and accurate distance estimates you will get.

0
votes
    RangedBeacon.setSampleExpirationMilliseconds(5000); // default is 20000
    beaconManager.setForegroundScanPeriod(100l); // 100 mS
    beaconManager.setForegroundBetweenScanPeriod(0l); // 0ms
    try {
        beaconManager.updateScanPeriods();
    } catch (RemoteException e) {
        e.printStackTrace();
    }

this help me to update the distance fast but the distance accuracy is decreased