In the reference application, RegionBootstrap is initialised in a custom application class on it's onCreate method and of course, the application class is called before any activity is called.
Is there a way to initialise RegionBootstrap inside an activity? I already tried making a static variable of RegionBootstrap so i can call it in a different activity, but unfortunately, it doesn't work.
BeaconApplication.regionBootstrap = new RegionBootstrap((BootstrapNotifier) this.getApplication(), downloadedBeacons);
The Regions I needed to be initialised will come from a server, so initialisation of RegionBootstrap must not come from the application class.
* EDIT *
public class LoginActivity extends ActionBarActivity {
…
/*** short version ***/
@Override
protected void onCreate(Bundle savedInstanceState) {
/*** after successful login ***/
BeaconApplication.beacons = downloadBeaconsFromServer();
}
}
public class BeaconActivity extends ActionBarActivity {
…
@Override
protected void onCreate(Bundle savedInstanceState) {
…
startService(new Intent(this, BeaconService.class));
}
}
This is where I implemented BeaconConsumer
public class BeaconService extends Service implements BeaconConsumer {
private BeaconManager beaconManager;
private BeaconNotifier beaconNotifier;
private RegionBootstrap regionBootstrap;
@Override
public void onCreate() {
super.onCreate();
Log.i(TAG, "onCreate");
beaconManager = 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.setBackgroundBetweenScanPeriod(1001);
beaconManager.setBackgroundScanPeriod(101);
beaconManager.setForegroundScanPeriod(101);
beaconManager.setForegroundBetweenScanPeriod(1001);
beaconNotifier = new BeaconNotifier(this);
beaconManager.bind(this);
}
@Override
public void onBeaconServiceConnect() {
beaconManager.setMonitorNotifier(beaconNotifier);
monitorBeacons();
regionBootstrap = new RegionBootstrap(beaconNotifier, BeaconApplication.beacons);
}
private void monitorBeacons() {
for (Region beacon : BeaconApplication.beacons) {
try {
Log.i(TAG, "Monitoring beacon " + beacon.getUniqueId());
beaconManager.startMonitoringBeaconsInRegion(beacon);
} catch (RemoteException e) {
Log.e(TAG, "Monitoring beacon failed");
e.printStackTrace();
}
}
}
}
Implementation of BeaconNotifier
public class BeaconNotifier implements BootstrapNotifier {
private Context context;
public BeaconNotifier(Context context) {
this.context = context;
}
@Override
didEnter.. etc
@Override
public Context getApplicationContext() {
return context;
}
}