Near the API, what is the right way to scan beacons in the background?

I use the Google API to search for beacons (Eddystone). The only current approach at the moment seems to be calling Nearby.Messages.subscribe() , as described here .

The problem is that it is not suitable for continuous scanning in the background. My application needs to monitor the beacons in the background, so if it becomes visible, it will make a REST API call. Basically, I need something similar to the beacon monitoring feature provided by the Estimote SDK.

Can this be achieved using a non-battery draining API?

+6
source share
3 answers

I understand that this question is about how to use the API, but I don’t know how to use it to meet your needs.

If you're open to alternatives, the free and open source Android Beacon Library has full Eddystone beacon support. Its API is modeled after the iOS monitoring / lighthouse API range, so it will do exactly what you want.

See here how to use this library to watch Eddystone beacons.

+1
source

It seems that the only way to do what you want is to either continuously scan, or use the location of the device and the beacon to start scanning when they are close. In any case, if you are scanning or it seems that Estimote beacons are using a location approach, which is probably the best approach to saving battery.

0
source

You can also subscribe in the background to receive Intent notifications instead of MessageListener notifications. Background scanning is a low-power scanning, so latency can be very long (even minutes to detect a beacon). Scanning is performed when an event occurs on the screen or when another application is requested. Thus, you get the results of other application scans.

You can create a GoogleApiClient using an application context instead of an activity context. Call it, that is, from a broadcast receiver that responds to a 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 ( onConnected ConnectionCallbacks method), you can subscribe using PendingIntent and create a Broadcast receiver that processes the intent.

In the broadcast receiver, you can process the intent using the Near.Messages.handleIntent method, which uses the same MessageListener as for the foreground scan.

One of the problems with this approach is access permissions in the neighborhood. To allow a user to approve access to a site, you need an interface. My solution was to wait with background scanning until the user first opened the application and accepted the permission. Once accepted, you can subscribe in the background.

0
source

All Articles