0
votes

Disclaimer: newbie to android programming.

Hello everybody, I'm trying to design an app to track the position of a smartphone using altbeacon library. The beacons I'm using are set to advertise every 100ms (non-connectable advertising), so since by default the library scans for 1.1s, one would expect to receive several advertising packets. Instead, I'm only receiving one packet per scan in the log, so the refreshing of the RSSI is quite slow (one per second). With default android library (setting SCAN_MODE_LOW_LATENCY) I am able to reach the ble callback continuously, updating the RSSI several times in a second.

Is it possible to do the same with Altbeacon library? My sample application looks like this:

public class MainActivity extends AppCompatActivity implements BeaconConsumer{


protected static final String TAG = "info";
private BeaconManager beaconManager;
private HashSet<Beacon> beaconsSeen = new HashSet<Beacon>();

Region region1 = new Region("region1", "xx:xx:xx:xx:xx:xx");
Region region2 = new Region("region2", "xx:xx:xx:xx:xx:xx");
Region region3 = new Region("region3", "xx:xx:xx:xx:xx:xx");
Region region4 = new Region("region4", "xx:xx:xx:xx:xx:xx");
Region region5 = new Region("region5", "xx:xx:xx:xx:xx:xx");
Region region6 = new Region("region6", "xx:xx:xx:xx:xx:xx");

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    RangedBeacon.setSampleExpirationMilliseconds(5000); //collects signal measurements over 5 seconds, throws out the top 10% and bottom 10% of
    // measurements, and averages the rest
    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=0215,i:4-19,i:20-21,i:22-23,p:24-24"));
    beaconManager.bind(this);
}
@Override
protected void onDestroy() {
    super.onDestroy();
    beaconManager.unbind(this);
}
@Override
public void onBeaconServiceConnect() {
    beaconManager.addRangeNotifier(new RangeNotifier() {
        @Override
        public void didRangeBeaconsInRegion(Collection<Beacon> beacons, Region region) {
            for (Beacon beacon : beacons) {
                Log.i(TAG, "I see a beacon "+beacon.getBluetoothAddress()+" with rssi: "+beacon.getRssi()+" dBm about "+beacons.iterator().next().getDistance()+" meters away.");
            }
        }
    });

    try {
        beaconManager.startRangingBeaconsInRegion(region1);
        beaconManager.startRangingBeaconsInRegion(region2);
        beaconManager.startRangingBeaconsInRegion(region3);
        beaconManager.startRangingBeaconsInRegion(region4);
        beaconManager.startRangingBeaconsInRegion(region5);
        beaconManager.startRangingBeaconsInRegion(region6);
    } catch (RemoteException e) {    }
}

My phone is a Xiaomi Redmi Note 2 running Android 5.0.2.

Thanks.

1

1 Answers

0
votes

The library is designed to send only one callback to didRangeBeaconsInRegion for each ranging cycle (based on a 1.1 second scan by default). This is true regardless of how many packets are received, and the individual RSSI readings are tracked internally and

You can increase the frequency of callbacks by reducing the length of the scan cycle. The following code will change the scan cycle to 100ms, so you'll get ranging updates at that interval.

beaconManager.setForegroundScanPeriod(100l);
beaconManager.updateScanPeriods();