Nearby API, what's the correct way of scanning for beacons in the background?
Asked Answered
I

3

6

I'm looking at using Google's Nearby API to detect beacons (Eddystone). The only current approach at the moment seems to be calling Nearby.Messages.subscribe() as described here.

The problem is that this doesn't seem to be suitable for continuous scanning in the background. My app needs to keep monitoring beacons in the background so if one becomes visible, it would perform a call to a REST API. Basically, I'd need something similar to the beacon monitorning feature provided by the Estimote SDK.

Would this be possible to achieve with the Nearby API without draining the battery?

Isomagnetic answered 20/10, 2015 at 13:55 Comment(0)
A
1

I realize this question is about how to use the Nearby API, but I don't know of a way to use it to meet your requirements.

If you are open to alternatives, the free and open source Android Beacon Library has full support for Eddystone beacons. Its API is modeled after the iOS monitoring/ranging beacon APIs, so it will do precisely what you want.

See here for how to use this library to monitor for Eddystone beacons.

Antihistamine answered 20/10, 2015 at 19:33 Comment(0)
T
0

It seems like the only way to do what you want is to either continuously scan, or to use device and beacon location to trigger a scan when they are close. Either way, there is battery drain involved as you're either scanning or sharing location. It looks like the Estimote beacons use the location approach, which is probably the better approach in terms of saving battery.

Torque answered 20/10, 2015 at 16:42 Comment(0)
L
0

You can subscribe also in background to receive Intent instead of MessageListener notifications. The background scanning is low-power scanning so the latency can be very long (even minutes to detect beacon). Scan is performed on screen-on event or when other app requests it. So you receive results from other apps scans.

You can create GoogleApiClient using application context instead of activity context. Call this i.e. from a broadcast receiver reacting to BOOT_COMPLETED broadcast.

GoogleApiClient client = new GoogleApiClient.Builder(appContext)
    .addApi(Nearby.MESSAGES_API, new MessagesOptions.Builder()
        .setPermissions(NearbyPermissions.BLE)
        .build())
    .build();
client.connect();

Once the client is connected (the onConnected method of ConnectionCallbacks) you can subscribe using PendingIntent and create Broadcast receiver that handles the intent.

In the broadcast receiver, you can handle intent using the Nearby.Messages.handleIntent method which is using same MessageListener as foreground scanning.

One of the problems with this approach are permissions to access Nearby. To allow user to approve access to Nearby you need UI. My solution was to wait with background scanning until user first time opens the app and accept the permissions. Once accepted you can subscribe in background.

Leavy answered 27/10, 2016 at 17:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.