Background monitoring stops working when I press home

Hi guys, just got your Estimote beacons yesterday so new to the club.

I cant get the monitoring to work in the background. When the app is open it works perfectly. It triggers the onEntered event and the onExited event as it should. When I press the home button it no longer works. Logcat still gives me the startLEscan and stopLEscan but it can no longer trigger the onEntered event. After a little while it still triggers the onExited event though.

Any idea what’s going wrong? Im using a nexus 5 with android 6.0 atm.

Thanks in advance, here’s my application code.

public class MyApplication extends Application {

private BeaconManager beaconManager;

@Override
public void onCreate() {
    super.onCreate();
    beaconManager = new BeaconManager(getApplicationContext());
    beaconManager.setMonitoringListener(new BeaconManager.MonitoringListener() {
        @Override
        public void onEnteredRegion(Region region, List<Beacon> list) {
            showNotification(
                    "Your gate closes in 47 minutes.",
                    "Current security wait time is 15 minutes, "
                            + "and it's a 5 minute walk from security to the gate. "
                            + "Looks like you've got plenty of time!");
        }
        @Override
        public void onExitedRegion(Region region) {
            showNotification(
                    "Leaving gate","Goodbye"
            );
        }
    });
    beaconManager.setBackgroundScanPeriod(1000,1000);
    beaconManager.connect(new BeaconManager.ServiceReadyCallback() {
        @Override
        public void onServiceReady() {
            beaconManager.startMonitoring(new Region("monitored region",
                UUID.fromString("B9407F30-F5F8-466E-AFF9-25556B57FE6D"), null, null));
        }
    });
}

public void showNotification(String title, String message) {
    Intent notifyIntent = new Intent(this, MainActivity.class);
    notifyIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
    PendingIntent pendingIntent = PendingIntent.getActivities(this, 0,
            new Intent[]{notifyIntent}, PendingIntent.FLAG_UPDATE_CURRENT);
    Notification notification = new Notification.Builder(this)
            .setSmallIcon(android.R.drawable.ic_dialog_info)
            .setContentTitle(title)
            .setContentText(message)
            .setAutoCancel(true)
            .setContentIntent(pendingIntent)
            .build();
    notification.defaults |= Notification.DEFAULT_SOUND;
    NotificationManager notificationManager =
            (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.notify(1, notification);
}
}

I see that you are using example from our tutorial. Let me double check on Android 6 tomorrow this example.

Yep. Decided to test it out with your examples first before I start using it in my own app. Thanks for the speedy reply, let me know what you come up with!

Also my target SDK version is set to 22 instead of 23 because im having issues with the “dangerous” permissions atm.

Just FYI, you can try to generate yourself an example app from a template:

https://cloud.estimote.com/#/apps/add

All the Android templates in there are already updated to account for having to request location permissions in SDK 23.

(Background monitoring, the “Notification” template will be most suitable.)

Ooooh thanks.
In the notification template both the background monitoring is working (still after pressing the home button) and the permission thing is working. I’ll go dig through this template and see if I can replicate it within my own app.

Cheers!

1 Like

Using the template as an example I got everything working. Im not sure where I went wrong in the first place but oh well.

Thanks!! :smiley:

1 Like