I am developing an Android app using AltBeacon Library from RadiusNetworks. I am using a nav-drawer (which launches activities, not fragments) to show different sections. Thus I have one main activity for the drawer which launches different activities by means of intents.
I would like to have nearby iBeacons info (ids and distance) available everywhere in the app. How can approach this?
I have tried the following (if we map my app structure with the reference one provided by RadiusNetworks):
- BeaconReferenceApp: same as reference app. Launches MainActivity.
- MainActivity: I have merged Monitoring and Ranging activities. This activity also manages the nav-drawer.
- Section1: this one extends MainActivity and displays some info depending on iBeacons collected data.
In MainActivity I do the following:
public void onBeaconServiceConnect() {
beaconManager.setRangeNotifier(new RangeNotifier() {
@Override
public void didRangeBeaconsInRegion(Collection<Beacon> beacons, Region region) {
for (Beacon beacon : beacons) {
Log.i("TAG","Beacon detected with id1: "+beacon.getId1()+" id2:"+beacon.getId2()+" id3: "+beacon.getId3()+" distance: "+beacon.getDistance());
}
}
});
try {
beaconManager.startRangingBeaconsInRegion(new Region("myRangingUniqueId", null, null, null));
} catch (RemoteException e) { }
}
I can see in the log the iBeacon info, but just once (first detection). However, I would like to get this info continuously, everytime the iBeacon is seen (as happens in the reference app), so that I can pass it to my other activities.
How can I solve this? Should I use a different app structure (because of the nav-drawer) or am I using the library in a wrong way?
Thank you.
UPDATE: Code for MainActivity and BeaconReferenceApp.
BeaconReferenceApp:
public class BeaconReferenceApp extends Application implements BootstrapNotifier {
private static final String TAG = "BeaconApp";
private RegionBootstrap regionBootstrap;
private BackgroundPowerSaver backgroundPowerSaver;
private boolean haveDetectedBeaconsSinceBoot = false;
private MainActivity mainActivity = null;
public void onCreate() {
super.onCreate();
BeaconManager 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,d:25-25"));
Log.d(TAG, "setting up background monitoring for beacons and power saving");
// wake up the app when a beacon is seen
Region region = new Region("backgroundRegion", null, null, null);
regionBootstrap = new RegionBootstrap(this, region);
backgroundPowerSaver = new BackgroundPowerSaver(this);
}
public void didEnterRegion(Region arg0) {
Log.d(TAG, "did enter region.");
// regionBootstrap.disable();
if (!haveDetectedBeaconsSinceBoot) {
Log.d(TAG, "auto launching MainActivity");
// The very first time since boot that we detect an beacon, we launch the MainActivity
Intent intent = new Intent(this, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
this.startActivity(intent);
haveDetectedBeaconsSinceBoot = true;
} else {
sendNotification();
}
}
public void didExitRegion(Region region) {}
public void didDetermineStateForRegion(int state, Region region) {}
public void setMainActivity(MainActivity activity) {this.mainActivity = activity;}
MainActivity:
public class MainActivity extends ActionBarActivity implements BeaconConsumer {
protected FrameLayout frameLayout;
protected ListView mDrawerList;
protected String[] listArray = { "Item1", "Item2", "Item3", "Item4" };
protected static int position;
private static boolean isLaunch = true;
private DrawerLayout mDrawerLayout;
private ActionBarDrawerToggle actionBarDrawerToggle;
private BeaconManager beaconManager = BeaconManager.getInstanceForApplication(this);
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
verifyBluetooth();
beaconManager.bind(this);
setContentView(R.layout.navigation_drawer_base_layout);
frameLayout = (FrameLayout)findViewById(R.id.content_frame);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerList = (ListView) findViewById(R.id.left_drawer);
// set up the drawer's list view with items and click listener
mDrawerList.setAdapter(new ArrayAdapter<String>(this, R.layout.drawer_list_item, listArray));
mDrawerList.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
openActivity(position);
}
});
// enable ActionBar app icon to behave as action to toggle nav drawer
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
// ActionBarDrawerToggle ties together the proper interactions between the sliding drawer and the action bar app icon
actionBarDrawerToggle = new ActionBarDrawerToggle(
this, /* host Activity */
mDrawerLayout, /* DrawerLayout object */
R.string.open_drawer, /* "open drawer" description for accessibility */
R.string.close_drawer) /* "close drawer" description for accessibility */
{
public void onDrawerClosed(View drawerView) {
getSupportActionBar().setTitle(listArray[position]);
invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
super.onDrawerClosed(drawerView);
}
public void onDrawerOpened(View drawerView) {
getSupportActionBar().setTitle(getString(R.string.app_name));
invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
super.onDrawerOpened(drawerView);
}
public void onDrawerSlide(View drawerView, float slideOffset) {
super.onDrawerSlide(drawerView, slideOffset);
}
public void onDrawerStateChanged(int newState) {
super.onDrawerStateChanged(newState);
}
};
actionBarDrawerToggle.syncState();
mDrawerLayout.setDrawerListener(actionBarDrawerToggle);
/**
* MainActivity is intended just to add navigation drawer in the app.
* Open some activity with layout on launch. Check checking if this
* MainActivity is called first time when we are
* opening our first activity.
* */
if(isLaunch){
/**
*Setting this flag false so that next time it will not open
* first activity.
* Use this flag because we are using MainActivity
* as parent activity to our other activities.
* In this case this base activity will always be call when any
* child activity will launch.
*/
isLaunch = false;
openActivity(0);
}
}
/**
* Launching activity when any list item is clicked.
*/
protected void openActivity(int position) {
mDrawerLayout.closeDrawer(mDrawerList);
MainActivity.position = position; //Setting currently selected position in this field so that it will be available in child activities.
switch (position) {
case 0:
startActivity(new Intent(this, Item1.class));
break;
case 1:
startActivity(new Intent(this, Item2.class));
break;
case 2:
startActivity(new Intent(this, Item3.class));
break;
case 3:
startActivity(new Intent(this, Item4.class));
break;
default:
break;
}
}
public void onDestroy() {
super.onDestroy();
beaconManager.unbind(this);
}
public void onResume() {
super.onResume();
((BeaconReferenceApp) this.getApplicationContext()).setMainActivity(this);
if (beaconManager.isBound(this)) beaconManager.setBackgroundMode(false);
}
public void onPause() {
super.onPause();
((BeaconReferenceApp) this.getApplicationContext()).setMainActivity(null);
if (beaconManager.isBound(this)) beaconManager.setBackgroundMode(true);
}
private void verifyBluetooth() {}
public void onBeaconServiceConnect() {
beaconManager.setRangeNotifier(new RangeNotifier() {
@Override
public void didRangeBeaconsInRegion(Collection<Beacon> beacons, Region region) {
for (Beacon beacon : beacons) {
Log.i("TAG", "Beacon detected with id1: " + beacon.getId1() + " id2:" + beacon.getId2() + " id3: " + beacon.getId3() + " distance: " + beacon.getDistance());
}
}
});
try {
beaconManager.startRangingBeaconsInRegion(new Region("myRangingUniqueId", null, null, null));
} catch (RemoteException e) { }
}
}