Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google nearby API background scan doesn´t work after application kill

I have kontakt.io beacon and I try to write application for background scanning with nearby API.

I use this method to subscribe messages:

SubscribeOptions options = new SubscribeOptions.Builder()
            // Finds messages attached to BLE beacons. See
            // https://developers.google.com/beacons/
            .setStrategy(Strategy.BLE_ONLY)
            .build();

    Nearby.Messages.subscribe(mGoogleApiClient, getPendingIntent(), options)
            .setResultCallback(new ResultCallback<Status>() {
                @Override
                public void onResult(@NonNull Status status) {
                    if (status.isSuccess()) {
                        Log.i(TAG, "subscribed successfully");
                        mSubState = SubState.SUBSCRIBING;
                        // Start background service for handling the notification.
                        getActivity().startService(getBackgroundSubscribeServiceIntent());
                    } else {
                        Log.i(TAG, "could not subscribe");
                        handleUnsuccessfulNearbyResult(status);
                    }
                }
            });

My code is according to this sample: https://github.com/googlesamples/android-nearby/tree/master/messages/NearbyBackgroundBeacons

I´m receiving messages correctly, but when I kill application no more message come.

Is there any way to get messages from nearby after killing application?

like image 333
Michael Drdlíček Avatar asked Nov 08 '22 17:11

Michael Drdlíček


1 Answers

Haven't really tried using one yet, but have you considered/checked on using a Service? As per the description:

A service can essentially take two forms:

Started

A service is "started" when an application component (such as an activity) starts it by calling startService(). Once started, a service can run in the background indefinitely, even if the component that started it is destroyed. Usually, a started service performs a single operation and does not return a result to the caller. For example, it might download or upload a file over the network. When the operation is done, the service should stop itself.

Bound

A service is "bound" when an application component binds to it by calling bindService(). A bound service offers a client-server interface that allows components to interact with the service, send requests, get results, and even do so across processes with interprocess communication (IPC). A bound service runs only as long as another application component is bound to it. Multiple components can bind to the service at once, but when all of them unbind, the service is destroyed.

You could create a Service that runs the scan continuously until such time that you choose to stop/destroy it. The docs itself already has step on Creating a Started Service. Just read it thoroughly.

like image 129
AL. Avatar answered Nov 14 '22 23:11

AL.