Issue in AltBeacon Monitoring and Ranging android

381 Views Asked by At

I am using this code for ranging and monitoring in Altbeacon

 class Appclass extends Application implements BootstrapNotifier {
private RegionBootstrap regionBootstrap;
private BackgroundPowerSaver backgroundPowerSaver;
private boolean haveDetectedBeaconsSinceBoot = false;
public static Region region1;
private static boolean activityVisible;
public static Appclass instances;
public static BeaconManager beaconManager;
public void onCreate() {
    super.onCreate();
    instances = this;beaconManager=org.altbeacon.beacon.BeaconManager.getInstanceForApplication(this);

       beaconManager.getBeaconParsers().add(new BeaconParser()
            .setBeaconLayout("m:2-3=0215,i:4-19,i:20-21,i:22-23,p:24-24"));
    beaconManager.setForegroundScanPeriod(1100l);
    beaconManager.setForegroundBetweenScanPeriod(0l);
    beaconManager.setAndroidLScanningDisabled(true);
    beaconManager.setBackgroundBetweenScanPeriod(01);
    beaconManager.setBackgroundScanPeriod(1100l);
    try {
        beaconManager.updateScanPeriods();

    } catch (Exception e) {
    }
    Log.d("", "setting up background monitoring for beacons and power                 saving");
    // wake up the app when a beacon is seen
    region1 = new Region("backgroundRegion",
            null, null, null);
    regionBootstrap = new RegionBootstrap(this, region1);
    backgroundPowerSaver = new BackgroundPowerSaver(this);

}

@Override
public void didEnterRegion(Region region) {
    Log.d("ABC", "Enter");
    Log.d("ABC value", region.getId2() + " " + region.getId3());
    try { Intent i = new Intent(getApplicationContext(), AbcService.class);

            startService(i);
    } catch (Exception e) {
    }
}
@Override
public void didExitRegion(Region region) {
    Log.d("ABC", "exit");
}

@Override
public void didDetermineStateForRegion(int i, Region region) {

}

and then in my AbcService class code is:

class AbcService extends Service implements BeaconConsumer, MonitorNotifier, RangeNotifier {
@Override
public IBinder onBind(Intent intent) {
    return null;
}

@Override
public void onCreate() {
    Appclass.beaconManager.bind(this);
    super.onCreate();

}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    try {
    } catch (Exception e) {

    }


    return START_STICKY;
}

@Override
public void didRangeBeaconsInRegion(Collection<Beacon> collection, Region region) {
    Log.d("ABC", "range");
}

@Override
public void onBeaconServiceConnect() {
    Appclass.beaconManager.setMonitorNotifier(this);
    Appclass.beaconManager.setRangeNotifier(this);

}

@Override
public void didEnterRegion(Region region) {
    try {
        Log.d("ABC", "didEnterRegion");
        Log.d("ABC", "" + region.getId2() + region.getId3());
        Appclass.beaconManager.startRangingBeaconsInRegion(Appclass.region1);

    } catch (RemoteException e) {
        e.printStackTrace();
    }
}

@Override
public void didExitRegion(Region region) {
    try {
        Log.d("ABC", "didExitRegion");
         Appclass.beaconManager.stopRangingBeaconsInRegion(Appclass.region1);

    } catch (Exception e) {
        e.printStackTrace();
    }
}

@Override
public void didDetermineStateForRegion(int i, Region region) {

}

}

now issue are getting that the didEnterRegion method of Application class calling every time when we enter in beacon range but the didEnterRegion method of AbcService class not calling some time and also not start ranging. what is the issue in my code?

1

There are 1 best solutions below

1
davidgyoung On BEST ANSWER

A few tips:

  • Start ranging the same time as you start monitoring. There is no reason to wait for an entered region callback or to stop ranging when you exit the region.

  • Add logging to didDetermineStateFirRegion. This method gets called when a beacon appears after app startup even if the device was already in region from when it was previously running. didEnterRegion only gets called if beacons matching the region actually disappear for 15+ seconds and then reappear.