How to keep scanner alive in background after app is killed

After following the proximity quick start guide and the snip guide from the SDK - I am still unable to keep my proximity scanner alive after the app has been killed :cry:

I have tried reading the official android doc on creating a background service but to no avail.

Is there a lament term or an official estimote background service tutorial somewhere that I can follow?:dizzy_face:

Any help is appreciated :slight_smile:

Hi @nikkysc

In case of Android version prior 8.0 (Oreo) - there is no supported way to keep scans working while application itself is not running. This is how Android security/power-management is designed and we cannot do anything about it.
However, we have to clarify what does exactly mean ā€œApplication is killedā€ :slight_smile:
Basically - killed application is a state when the process hosting application content is not running. Killed application IS NOT the state when just the activity was destroyed. In other words - it is possible to exit the application (kill the activity) but still keep the background process up-and running with active scan in place. :rocket:
To achieve such behaviour with Proximity-SDK, you need to use withScannerInForegroundService option when creating ProximityObserver instance. Here is Kotlin example:

val notification = Notification.Builder(this)
                .setSmallIcon(R.drawable<YOUR NOTIFICATION DRAWABLE>)
                .setContentTitle(R.strings.<YOUR NOTIFICATION TITLE>)
                .setContentText(R.strings.<YOUR NOTIFICATION CONTENT>)
                .setPriority(Notification.PRIORITY_HIGH)
                .build()

proximityObserver = ProximityObserverBuilder(applicationContext, EstimoteCloudCredentials(<APP_ID>, <APP_TOKEN>))
                .withTelemetryReporting()
                .withBalancedPowerMode()
                .withOnErrorAction { Log.d("PSDK", it.message) }
                .withScannerInForegroundService(notification)
                .build()

With ProximityObserver created in such way, each scan will be performed within foreground service (Check out more information about foreground services here). This will allow us to continue scanning even if main application is no longer visible for the user.

Regards
Wojtek

Hi @Wojciech_Wawerek ,

Thanks for your reply :slight_smile:
What I had meant by ā€œapplication is killedā€ was that it had been dismissed or swiped up etc from the android multitasking application tray.

So is there no work around for now to actually keep the scanner running in the background with the foreground notification??

What i really wanted to do was just:

  1. If the bluetooth is on please look for estimote bluetooth devices (regardless of having the application open in the foreground or is minimised into background (is active in the multitasking app tray)
  2. If it is in the vicinity then have the phone display the notification.

From what i understand from your response is that this is not achieve able on any android version prior to android 8?:disappointed_relieved:

Hi @nikkysc

I think i wasnā€™t crystal clear when I elaborated about ā€œapplication is killedā€ and handling this situation in different Android versions term :slight_smile:
Once again:

  • Application is killed when process hosting this application is no longer running (e.g. you killed application by going to Setting->Applications-> and choosing ā€œKill Appā€ option). In such case - there are options to continue scanning only on Android 8+. However - this is not your cace

  • Application is NOT Killed when you just swipe out its Activity, press home button etc. In such case we are able to continue scan on all supported Android version. I believe this is exactly your case :slight_smile:

To continue scanning while application is in background (not running from user perspective) you have to use withScannerInForegroundService option when creating proximity observer.
As a result, our SDK will pop up notification in Androidā€™s status bar and thus become ā€œforeground oneā€ even if user destroy applicationā€™s activity.
You probably know this behaviour from other applications abe to work in background while user interacts with different stuff. Spotify is a good example - when it starts to play, then notification appears in status bar and you can easily ā€œexitā€ the Spotifyā€™s activity and still listen the song.

Let me know if this address your concerns :slight_smile:

Regards
Wojtek

Hi @Wojciech_Wawerek,

Thanks for your reply.

Great explanation - I believe weā€™re getting on to the same page now :smile:
I also have implemented the foreground service with my scanner but when the app is slided up from the multitasking - it just kills my scanner.

What I am trying to do is that; I want to be able to scan for Estimote beacons without having to open the app and leaving in the background.

Sort of like how messaging apps when you restart the phone and it isnā€™t running itā€™ll get get messages sent kinda thing.

Is this possible? Hope Iā€™ve some what clarify my actual question :confounded:

Hi @nikkysc

Iā€™m sorry for delayed response - Iā€™ve been super busy last days.

Regarding messaging-like behaviour - Unfortunately I think youā€™ll not be able to achieve it with BLE technology. This is because messaging application (like hangouts, facebook messenger etc.) utilizes Firebase Cloud Messaging infrastructure to get launched/notified whenever new message arrives. Firebase ensures that each registered application will get notified (or even launched when itā€™s not running) whenever new message pop up for this app. Unfortunately Android does not support similar mechanisms for BLE scanning. All you can do is to fire up your scan in foreground service with notification icon in notification bar. - This is exactly what our SDK is doing when withScannerInForegroundService(...) option is used during proximityObserver construction.

Regards
Wojtek

First, I think you should know what exactly happens to running services when app is swiped away from the recent app list in Android and then make the Android service keep running even after app swipe. I have written a detailed post on this on medium and you may check it .