Multiple Regions in the Demo Application

Hi. We are working on an android app that uses estimote beacons and are having trouble creating multiple regions. We tested our code by placing three beacons near the android device with our app installed. We defined three region variables, region, region2 and region3. In our onEnteredRegion method we used three if statements with conditions that if the major and minor values of each region equal the corresponding major and minor values of a specific beacon, a notification will appear. However, everytime we run the app we only get one out of the three unique messages. Somehow the onEnteredRegion method is not working for multiple regions. Is it possible to define multiple regions in code and post a unique notification depending on the region? Or do we have to use another method or function to incorporate multiple beacons in the demo application provided on Github? Thanks!

Can you share the code where you define the regions and start monitoring? Do you use unique identifiers for each region? It’s 100% possible to range/monitor multiple regions at the same time, so it’s likely some small problem with your code, we’ll be happy to help with that.

public class NotifyDemoActivity extends BaseActivity {

private static final int NOTIFICATION_ID = 123;

private BeaconManager beaconManager;
private NotificationManager notificationManager;
private Region region;
//private Region region2;
//private Region region3;

private UUID uuid = UUID.fromString("B9407F30-F5F8-466E-AFF9-25556B57FE6D");


@Override protected int getLayoutResId() {
  return R.layout.notify_demo;
}

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);


//Beacon beacon = getIntent().getParcelableExtra(ListBeaconsActivity.EXTRAS_BEACON);
region = new Region("regionId1", uuid, 28280, 55537);
beaconManager.startMonitoring(region);
region = new Region("regionId2", uuid, 29632, 4951);
beaconManager.startMonitoring(region);
region = new Region("regionId3", uuid, 28233, 13329);
beaconManager.startMonitoring(region);

notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
beaconManager = new BeaconManager(this);

beaconManager.setBackgroundScanPeriod(TimeUnit.SECONDS.toMillis(1), TimeUnit.SECONDS.toMillis(0));

beaconManager.setRegionExitExpiration(TimeUnit.SECONDS.toMillis(20));

beaconManager.setMonitoringListener(new MonitoringListener() {

    @Override
  public void onEnteredRegion(Region region, List<Beacon> beacons) {

    if (region.getIdentifier().equals("regionId1")) {

      postNotification("Entered meeting room");
    }
    else if (region.getIdentifier().equals("regionId2")) {

      postNotification("Entered bathroom");

    }
    else if (region.getIdentifier().equals("regionId3")) {

      postNotification("Entered movie theater");

    }
  }

  @Override
  public void onExitedRegion(Region region) {
    postNotification("Exited region");
  }
});

 }

@Override
protected void onResume() {
  super.onResume();

notificationManager.cancel(NOTIFICATION_ID);
beaconManager.connect(new BeaconManager.ServiceReadyCallback() {
  @Override
  public void onServiceReady() {
    beaconManager.startMonitoring(region);
//        beaconManager.startMonitoring(region2);
//        beaconManager.startMonitoring(region3);
   }
  });
 }

@Override
protected void onDestroy() {
  notificationManager.cancel(NOTIFICATION_ID);
  beaconManager.disconnect();
  super.onDestroy();
 }

 private void postNotification(String msg) {
   Intent notifyIntent = new Intent(NotifyDemoActivity.this, NotifyDemoActivity.class);
   notifyIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
   PendingIntent pendingIntent = PendingIntent.getActivities(
       NotifyDemoActivity.this,
       0,
       new Intent[]{notifyIntent},
       PendingIntent.FLAG_UPDATE_CURRENT);
   Notification notification = new Notification.Builder(NotifyDemoActivity.this)
       .setSmallIcon(R.drawable.beacon_gray)
       .setContentTitle("Notify Demo")
       .setContentText(msg)
       .setAutoCancel(true)
       .setContentIntent(pendingIntent)
       .build();
     notification.defaults |= Notification.DEFAULT_SOUND;
     notification.defaults |= Notification.DEFAULT_LIGHTS;
     notificationManager.notify(NOTIFICATION_ID, notification);

   TextView statusTextView = (TextView) findViewById(R.id.status);
   statusTextView.setText(msg);
 }
}

We got this code from the Estimote Demos application from Github. We are trying to modify the notification aspect of this application to incorporate multiple beacons…

Hmm, does this code work for you? You have beaconManager.startMonitoring(region); before beaconManager = new BeaconManager(this);, so to my eye, it should be throwing null pointer exceptions?