0

I want to experiment with iBeacon on Android. It looks like that AltBeacon is the best tool to use. I read that if I subclass the application of my app to this, I can make my app works even though the app is killed the afterward.

What if I want to my app to monitor only if the user logged in? The Application will run each the app is launched, won't it? How to run the BootstrapNotifier in Application after login and not run it if the user isn't logged in?

        @Override
        protected void onPostExecute(final Boolean success) {
            if (success) {
                //algorithm to make altbeacon run in the background, even after the app killed
            } else {
                //if failed
            }
        }

So, here is the proposed solution:

public class BeaconReferenceApplication extends Application implements BootstrapNotifier {

    // OTHER CODE

    public void onCreate() {
        super.onCreate();
        if (loggedin) {
            BeaconManager beaconManager = BeaconManager.getInstanceForApplication(this);
            Region region = new Region("backgroundRegion", null, null, null);
            regionBootstrap = new RegionBootstrap(this, region);
            BeaconManager.getBeaconSimulator()).createTimedSimulatedBeacons();
        }
    }

    // OTHER CODE

}


public class LoginACtivity {

    // OTHER CODE

    public void onClick {

        if (username == TRUE && password == TRUE) {
            // SINCE THE USER LOGGED IN, HOW DO I MAKE MY APP TO START ALWAYS SCAN EVEN AFTER REBOOTING AS LONG THE USER ISN'T LOGGING OUT?
        }

    }

    // OTHER CODE

}

public class MainActivity {

    // OTHER CODE

    private void logout {

        // SINCE THE USER CLICK LOG OUT BUTTON, HOW DO I MAKE MY APP TO STOP SCANNING EVEN AFTER REBOOTING UNTIL THE USER LOGGING IN AGAIN?

    }

    // OTHER CODE

}

Is that code will guarantee my app to always scan for beacon only if the user is loggedin, even after rebooting and the app isn't running?

stackex
  • 825
  • 2
  • 10
  • 13
  • Use `SharedPreferences` or a `boolean` to hold a value when the user has logged in. If logged in, start monitoring and update `SharedPreferences` value or that boolean – hrskrs Mar 20 '15 at 07:16
  • So do you want to scan for beacons automatically after boot if the user has *previously* logged in? Or do you want to only scan for beacons after boot if the user logs in again after boot? – davidgyoung Mar 20 '15 at 16:10
  • @davidgyoung i want my app to scan for beacons automatically after boot if the user has previously logged in. If the user has not logged in previously, I want my app not to scan for beacons. So, basically I want my app to scan ONLY if the user in a logged in condition. I already use SharedPreferences to track if the user already logged in or not so that if the user has already logged in previously, the app will skip the login section, even after rebooting. – stackex Mar 23 '15 at 02:01

1 Answers1

1

You simply need to wrap the code that constructs the RegionBootstrap object to check if the user has logged in before. If not, don't construct it.

You can then disable the RegionBootstrap at a later time, and reconstruct it when necessary. Like this:

public class BeaconReferenceApplication extends Application implements BootstrapNotifier {
  ...
  private RegionBootstrap regionBootstrap;

  public RegionBootstrap startBeaconMonitoring() {
    if (regionBootstrap == null) {
        Region region = new Region("backgroundRegion", null, null, null);
        regionBootstrap = new RegionBootstrap(this, region);          
    }
  }

  public RegionBootstrap stopBeaconMonitoring() {
    if (regionBootstrap != null) {
        regionBootstrap.disable();          
    }
  }

  public void onCreate() {
    super.onCreate();
    if (loggedin) {
       startBeaconMonitoring();
       ...
    }
}


public class MainActivity {
  ...
  private void logout() {
    ((BeaconReferenceApplication)this.getApplication()).stopBeaconMonitoring();
  }
  private void login() {
    ((BeaconReferenceApplication)this.getApplication()).startBeaconMonitoring();
  }

}
davidgyoung
  • 63,876
  • 14
  • 121
  • 204
  • In BeaconReferenceApplication Class, I've checked the login condition from SharedPreferences. But, BeaconReferenceApplication only run once when the user launch the app, doesn't it? What if the afterward the user login, how do I call bootstrap to always run in the background to scan for beacon, even after the app is killed? Above I update a bit of my code. – stackex Mar 24 '15 at 01:49
  • I have expanded my answer above to show how you can switch back and forth – davidgyoung Mar 24 '15 at 02:07