3

This is more of a general question, but to what extent can we use the Nearby Connections API in services?

I noticed that discovery and pairing is still working while the app is out of focus, so is it possible to extract the whole connections client into a foreground- or even a background service which is running while the app itself doesn't have to?

just_deko
  • 1,024
  • 1
  • 14
  • 29

2 Answers2

5

There are no restrictions on using Nearby Connections from a service. However, Android has always somewhat aggressively killed background services (and is more aggressive since Android Oreo). There's also no way to limit the power, so advertising, scanning, and maintaining a connection for a long period of time will adversely effect battery life. There's a plan to expose a low power flag, but there's no ETA.

Disclaimer: I work on Nearby Connections

Xlythe
  • 1,958
  • 1
  • 8
  • 12
  • Once your process dies, Nearby Connections will shut down any active advertising / scanning / connections your app created. – Xlythe Dec 04 '19 at 02:36
0

On devices running Android Q or higher, an ACCESS_FINE_LOCATION permission has to be requested to use Nearby Connections. When an app runs a background service, like a JobService, using Nearby Connections on a device running Android Q or higher, the app will not be able to discover nor to advertise, unless the ACCESS_BACKGROUND_LOCATION permission has been granted to the app by Google. To get it granted by Google, you have to explain in detail why your app needs it, and there is no guarantee that you will get it. This said, on Android versions lower that Q, an app can successfully discover and advertise in a background service, as long as it has requested all the nedded permissions (BLUETOOTH, BLUETOOTH_ADMIN, ACCESS_WIFI_STATE, CHANGE_WIFI_STATE and ACCESS_COARSE_LOCATION).