I made a simple Android app that continuously scans Nearables and submits their sensor data to the cloud (Amazon AWS IoT). It seems to work first, but after a while there are no more scan results and it goes quiet.
Sometimes exiting and starting the app again seems to help, and sometimes turning Bluetooth off and on again helps.
Are there any other ways to make continuous scanning reliable? My device is a Nexus 7 tablet. Are some other Android devices more reliable for this purpose? Or am I forgetting to do something important in my code? The tablet is plugged in and in ‘always on’ mode with the app in foreground.
You can do this in a service, and make it run in the background. Use a singleton for the BeaconManager and use that singleton in the service to do monitoring or ranging.
Does running as a service improve reliability, if the problems occur when the app is active and running in the foreground?
If by reliability you mean that it doesn’t stop scanning. That definitely, I’m currently having the problem that onExitedRegion gets triggered without moving anything (not the beacon, nor the device), looking trough this forum it seems that is still an unsolved mystery… Anyhow, I’ve been scanning with a service for about 48h with my Samsung Note 4 and Nexus 7 (2013) and neither has stopped scanning as far as I am aware.
PS: set the service as START_STICKY so it will run indefinitely. Depending on your needs, don’t start it with bindService, there is no reason to do so anyway. You can change the configuration of your BeaconManager outside of the service since it’s a singleton it doesn’t matter where you change the state.
If you observe that scans have been stopped being reported, that means your phone has started dreaming (went into deep sleep mode) in order to save battery.
Where do you hold reference to BeaconManager? If you want to have it long running, you need to hold it either in your Application class or in a Service. The first approach is recommended. If you were to hold reference in Activity, it will be eventually paused and stopped.
What Android OS is on your Nexus 7 (from which year?)?
I’ve held the BeaconManager reference (and everything else) in a single MainActivity class and disabled auto-screen-turnoff on the phone, and I guess this wasn’t enough. I guess I’m going to try a service implementation next since eventually I want the scanning to resume after reboots too.
My Nexus 7 has Android 5.1.1 and I think it’s the 2013 model.
I’ve now changed the code so that BeaconManager is created and kept in a Service class. The application still stops receiving nearable notifications after an hour or so. Same happens on a Nexus 4 phone. In the logs I can see that it’s still trying to scan; these lines repeat every few seconds, but there are no nearables found:
11-22 13:37:57.111 12443-17540/kfalck.net.estimotegateway D/BluetoothLeScanner: onClientRegistered() - status=0 clientIf=5
11-22 13:37:58.136 12443-12650/kfalck.net.estimotegateway D/BluetoothAdapter: stopLeScan()
11-22 13:37:58.181 12443-12650/kfalck.net.estimotegateway D/BluetoothAdapter: startLeScan(): null
I implemented a similar continuous scan on an iPhone 5 which is laying next to the Nexus 4. It has been running for a week now with no problems; it keeps on receiving continuous nearable notifications (I’ve disabled screen auto-lock and the iOS app is in the foreground).
So for some reason, iOS is much more reliable in continuous long-term scanning than Android.
We’re about to roll out a new version of the SDK shortly, with improved background scanning—basically, we use Android 5.0/5.1 APIs if available for low-power background monitoring. Can you keep an eye out on this release (the easiest is just to watch our Android SDK GitHub repo, you’ll get an email from GitHub every time we push out a new release) and test with that?