1

I use the Android Beacon Library to scan Eddystone beacons with my phone. The application is properly running in the foreground.

Now, I want to detect Eddytone beacons in the background mode, but the example in the website of the library doesn't run on my application.

In my case, I have an activity for the UI and a separate Service for the foreground beacons detection.

I have exactly the same code of the example and the launchMode of my activity is "singleInstance".

Here is my code:

public class Background extends Application implements BootstrapNotifier{

    private static final String TAG = "Background";
    private RegionBootstrap regionBootstrap;

    @Override
    public void onCreate() {
        super.onCreate();
        Log.e(TAG, "App started up");
        BeaconManager beaconManager = BeaconManager.getInstanceForApplication(this);
        beaconManager.getBeaconParsers().add(new BeaconParser().setBeaconLayout(BeaconParser.EDDYSTONE_UID_LAYOUT));
        beaconManager.setBackgroundScanPeriod(1100L);
        beaconManager.setBackgroundBetweenScanPeriod(5000);
        Region region = new Region("all", null, null, null);
        regionBootstrap = new RegionBootstrap(this, region);
    }

    @Override
    public void didDetermineStateForRegion(int arg0, Region arg1) {
        // Don't care
    }

    @Override
    public void didEnterRegion(Region arg0) {
        Log.e(TAG, "Got a didEnterRegion call");
        regionBootstrap.disable();
        Intent intent = new Intent(this, MainActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        this.startActivity(intent);
    }

    @Override
    public void didExitRegion(Region arg0) {
        // Don't care
    }
}

Am I missing anything? Or due to my separate service, this is not the proper method?

Thank you for your help.

Rodia
  • 1,407
  • 8
  • 22
  • 29
Damien LD
  • 53
  • 1
  • 13
  • What do you see that is not expected? Can you show more context around those two lines of code so we can see when they are executed? – davidgyoung Mar 15 '17 at 11:55
  • In my case, I expect that the app start when a beacon is detected (during the background mode), but nothing happen, even if I wait 5 minutes or more. – Damien LD Mar 15 '17 at 12:41
  • @DamienLD do you see log `Got a didEnterRegion call` in logcat? – Divers Mar 15 '17 at 12:42
  • @Divers : Yes when the app is in foreground mode. But, when the app is killed, the head of the logcat show the app as [DEAD], is it normal ? Nothing happen after that; the app stay [DEAD] – Damien LD Mar 15 '17 at 12:47
  • Yes, it is normal after killing the app for it to show as DEAD. However, the Android Beacon Library is designed to restart the scanning service within 5 minutes in this case. It accomplishes this using an `AlarmManager`. – davidgyoung Mar 15 '17 at 16:14

2 Answers2

0

If your app is "killed" of course you will not scan, because your App object doesn't exist at all.

What you need is use foreground service which runs all the time in background. Also you need to consider restart that service when device was restarted.

Community
  • 1
  • 1
Divers
  • 9,531
  • 7
  • 45
  • 88
  • Ok, but I want that the service is transparent for the user, it's not the case with a foreground service, or I am wrong ? – Damien LD Mar 15 '17 at 13:47
  • Then use not foreground service but standard one and restart it when needed. – Divers Mar 15 '17 at 14:43
  • @divers, The Android Beacon Library is designed to automatically restart the scanning service, which re-creates the Application object because the scanning service is part of the same app. – davidgyoung Mar 15 '17 at 16:12
0

It is important to understand two things about how the Android Beacon Library works in this case:

  1. When you swipe an app of the screen, it kills the process. The Android Beacon Library will attempt to restart its lightweight scanning service (and your application in the background) within 5 minutes of this happening. When it does, it will be a new process, so you will still see the same one as DEAD in Android Studio, but a new one should appear in the pull down menu.

  2. The library persistently tracks region state for cases like this. So if you were already inside the region when you killed the app, you won't get a second didEnterRegion call when it starts back up. An easier way to test what is going on is to put a log line inside didDetermineStateForRegion, which gets called when the app starts back up regardless of the state.

There are a small percentage of Android devices that have non-standard task switchers that kill apps in a way that do not allow them to restart. In the unlikely event that you have one of these devices, the above won't work. To find out for sure, I would do this:

  1. Add a new log line in didDetermineStateForRegion

  2. On the command line, run adb logcat | grep Background to look for all log lines from your application, even after it restarts with a different process id.

  3. Run your app and verify you see log output.

  4. Kill your app.

  5. Wait five minutes while watching the log output. Do you see App started up or the log line you added for didDetermineStateForRegion?

EDIT: Based on the log output provided in a link in the comments, I see that the app is running as process id 6863 detecting beacons for awhile:

03-28 14:31:17.540  6863  6863 E Background: App started up
...
03-28 14:31:18.196  6863  6935 E Background: Got a didEnterRegion call
...
03-28 14:31:23.550  6863  6863 E MainActivity: Beacons received

Then these entries appear:

03-28 14:31:24.932   928  1913 I MediaProcessHandler: processOp opType: 1, uid: 10138, pid: 6863
03-28 14:31:24.932   928  1913 W MediaProcessHandler: remove target not exist, maybe the UI process: uid: 10138, pid: 6863

It appears that a class called MediaProcessHandler in this non-standard Android OS flavor is killing the process at the time above. The Android Beacon Library uses an AlarmManager to restart scanning 5 minutes after the last scan in such a case, which should have gone off at 14:36:23 to resume scanning. Unfortunately, the log ends at 03-28 14:33:28.553, so I can't tell if this happened as designed.

davidgyoung
  • 63,876
  • 14
  • 121
  • 204
  • Thank you for your explanation. But I don't see `App started up` or le the line added in `didDetermineStateForRegion`. When the app run, the logcat is shown correctly, but I have only `Background partial concurrent mark sweep GC freed 22088(1378KB) AllocSpace objects, 17(532KB) LOS objects, 4% free, 44MB/46MB, paused 2.411ms total 159.648ms at HeapTaskDaemon thread CareAboutPauseTimes 1` – Damien LD Mar 16 '17 at 13:35
  • Do you see "app started up" and the line added in "didDetermineStateForRegion" if you launch the app manually? If so, can you then try killing the app, disconnecting USB/reconnecting USB, and then see if you can see those lines in LogCat? – davidgyoung Mar 16 '17 at 14:14
  • Yes I see the "app started up" in the line "didDetermineStateForRegion". It's also the case when I kill the app and I restart manually. But even if I wait 5 minutes, nothing happens in the logcat. I use two smartphones Huawei (P9 lite and Honor 5C) – Damien LD Mar 27 '17 at 13:50
  • Hmmm... Can you please add `beaconManager.setDebug(true)` and then capture a LogCat excerpt for the 5 minutes after killing the app to see if you see any more debug lines coming from the library? – davidgyoung Mar 27 '17 at 17:40
  • `03-28 10:16:59.830 928 938 I art : Background partial concurrent mark sweep GC freed 4685(269KB) AllocSpace objects, 86(5MB) LOS objects, 4% free, 40MB/42MB, paused 2.238ms total 145.363ms at HeapTaskDaemon thread CareAboutPauseTimes 1 03-28 10:17:00.150 928 938 I art : Background partial concurrent mark sweep GC freed 1083(65KB) AllocSpace objects, 79(4MB) LOS objects, 4% free, 40MB/42MB, paused 1.780ms total 130.064ms at HeapTaskDaemon thread CareAboutPauseTimes 1` Nothing happens except few lines like this. – Damien LD Mar 28 '17 at 08:33
  • Ok, but you say you do see "app started up". There should be a number of lines after this where it starts scanning as a result of RegionBootstrap being initialized. Then at some point these lines perhaps stop. This is what I need to see. The log will be long, so you will probably have to link to a file put up somewhere else. – davidgyoung Mar 28 '17 at 11:51
  • I put the log here : www.transfernow.net/201aa2y2k4by I hope that it's what do you expect. – Damien LD Mar 28 '17 at 12:47
  • Sorry, no luck downloading that file using transfernow.net. I can see the download link, but when I click on it I always get "Forbidden (403)" – davidgyoung Mar 28 '17 at 14:31
  • See my edited answer. I can tell that something kills the library's BeaconService along with your app, but I cannot tell if it restarts scanning 5 minutes later as designed because the log is not long enough. – davidgyoung Mar 29 '17 at 10:42