Daniele Grattarola
Daniele Grattarola

Reputation: 1547

How to listen for messages with Nearby API Messages in a started service?

I am developing an Android application with Google's Nearby API Messages and I was able to succesfully exchange messages between users by following this tutorial https://developers.google.com/nearby/messages/overview

As it is now, the application needs to be actively started by the user to start listening for messages, so I wanted to make it listen for messages from a started service which would run at boot.

This is where it gets diffcult, because:

  1. I need my GoogleApiClient and MessageListener objects to be initialized and well (from what I gathered it is not ok for the service to just return START_STICKY from onStartCommand()since it only needs to run the MessageListener.onFound() method when triggered and not continuously execute code)
  2. The ErrorCheckingCallback class implemented in the tutorial needs a reference to an activity to work properly

I spent a whole day on this and I am totally lost. Can anyone help? Thank you in advance.

Upvotes: 2

Views: 2533

Answers (1)

Daniele Grattarola
Daniele Grattarola

Reputation: 1547

Ok, I got an answer to this, in case anyone is looking: https://developers.google.com/nearby/developer-guidelines

On Android, when publishing or subscribing with non-BLE devices, only invoke the Nearby Messages API from an Activity, and only keep that Activity running when the screen is on and your app is in the foreground. To do this, pass the Activity as an argument to the Context parameter of Nearby.getMessagesClient(). Invoking Nearby from a Service is only supported when subscribing to Bluetooth Low Energy (BLE) beacon messages.

Upvotes: 1

Related Questions