AltBeacon / android-beacon-library

Allows Android apps to interact with BLE beacons
Apache License 2.0
2.83k stars 834 forks source link

[Support] Advertising Beacon in background (Android O) #590

Open mpuchala opened 6 years ago

mpuchala commented 6 years ago

Hey,

I'm using AltBeacon library to advertise beacon in background. Right now I have a Service with BeaconTransmitter which is working in background. After latest changes in Android O it won't work anymore (Foreground Service is not acceptable in that situation). Is there any way to achieve this? I was looking for any solution but just found information about Beacon detecting. If I missed sth out I would be grateful for hint. Also if issue is duplicated just let me know and feel free to close.

Thanks!

android-Israel commented 6 years ago

need help too

davidgyoung commented 6 years ago

Unfortunately, Android 8's prohibition on long-running background services means that you can only transmit when a the app is active. So the app has to be in the foreground, or you get about 10 min of transmission time every 15-25 minutes when a background job runs.

One alternative is to use a foreground service, which can be long-running. This would still allow transmission in the background. The only real disadvantage is that the user will see an icon at the top if the screen.

The limits on transmission serve no power saving purpose as BLE transmissions use negligible power. However, until Android adds APIs to transmit when your app is not running, the above options are the best there are.

michaellee8 commented 4 years ago

@davidgyoung Can you kindly provide some sample code if I want to do background broadcasting using a Foreground Service? I can only see an example that demonstrates scanning but not advertisement.

davidgyoung commented 4 years ago

You can enable a foreground service in the same way as described for scanning here

For the foreground service to activate, you will need to start monitoring beacons, even if you don't need to otherwise. If you have no other use for the monitoring callbacks, you can do so just for a dummy region, and set the between scan period to minimal intervals to save battery:

beaconManager.setForegroundBetweenScanPeriod(99999999);
beaconManager.setBackgroundBetweenScanPeriod(99999999);
Region dummyRegion = new Region("com.example.backgroundRegion",
                Identifier.parse("01020304-0506-0708-090A-0B0C0D0E0F00"), 1, 2);
new RegionBootstrap(this, dummyRegion); // the enclosing class must implement BootstrapNotifier
michaellee8 commented 4 years ago

@davidgyoung Do you mean that I by activtating "foreground service" background scanning i can enable "foreground service" advertisement as well? Is it possible for me to do it without requesting for the locations permission since transmitting beacon singal only require bluetooth permissions?

davidgyoung commented 4 years ago

Yes, you just need to create a foreground service to keep your app alive. While you can transmit in the background without a foreground service, the OS will kill your app after ~10min in the background and stop transmission if you do not have one.

The option I show will activate the library's built in foreground service designed for scanning purposes. You do not need to request location permissions from the user or even declare them in the manifest. Without them no beacons will be detected, but that matters little if you have no need for detection.

You can also just set up your own dummy foreground service manually. I am just showing an easy way to leverage the library's built in one.

On Mon, Apr 6, 2020, 02:07 michaellee8 notifications@github.com wrote:

@davidgyoung https://github.com/davidgyoung Do you mean that I by activtating "foreground service" background scanning i can enable "foreground service" advertisement as well? Is it possible for me to do it without requesting for the locations permission since transmitting beacon singal only require bluetooth permissions?

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/AltBeacon/android-beacon-library/issues/590#issuecomment-609584032, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAH7C4NLYI7P57WFKGA77MDRLFWR3ANCNFSM4D2SQRHA .