java.lang.Object | ||
↳ | com.google.android.gms.common.api.GoogleApi<com.google.android.gms.nearby.messages.MessagesOptions> | |
↳ | com.google.android.gms.nearby.messages.MessagesClient |
API which allows your app to publish simple messages and subscribe to receive those messages from nearby devices.
The API performs its operations in an unauthenticated manner, so it does not require a Google account. However, it requires that the developer has a project in the Google Developers Console with the following prerequisites:
<manifest ...> <application ...> <meta-data android:name="com.google.android.nearby.messages.API_KEY" android:value="SPECIFY_APPLICATION_API_KEY_HERE" /> <activity> ... </activity> </application> </manifest>
The Messages API should be accessed from the Nearby
entry point. For example:
@Override protected void onStart() { Task<Void> task = Nearby.getMessagesClient(context).publish(new Message(bytes)); }
All of the Messages APIs should be used from a foreground Activity, with the exception of the
variants of subscribe
that take a PendingIntent
parameter. Your Activity should
publish(Message)
or subscribe(MessageListener)
either in onStart()
or in
response to a user action in a visible Activity, and you should always symmetrically unpublish(Message)
or unsubscribe(MessageListener)
in onStop()
.
Public Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Extracts information from an Intent sent as a subscription callback, and calls the
corresponding methods on the given MessageListener.
| |||||||||||
Publishes a message so that it is visible to nearby devices, using the default options from
DEFAULT . | |||||||||||
Publishes a message so that it is visible to nearby devices.
| |||||||||||
Registers a status callback, which will be notified when significant events occur that affect
Nearby for your app.
| |||||||||||
Subscribes for published messages from nearby devices.
| |||||||||||
Subscribes for published messages from nearby devices, using the default options in
DEFAULT . | |||||||||||
Note: Currently, this method only finds messages attached to BLE beacons.
| |||||||||||
Note: Currently, this method only finds messages attached to BLE beacons.
| |||||||||||
Cancels an existing published message.
| |||||||||||
Unregisters a status callback previously registered with
registerStatusCallback(StatusCallback) . | |||||||||||
Cancels an existing subscription.
| |||||||||||
Cancels an existing subscription.
|
[Expand]
Inherited Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
![]() |
Extracts information from an Intent sent as a subscription callback, and calls the corresponding methods on the given MessageListener.
Note: Only onFound(Message)
and onLost(Message)
are supported.
For example:
PendingIntent pendingIntent = PendingIntent.getService(context, 0, new Intent(context, MyService.class), PendingIntent.FLAG_UPDATE_CURRENT); messagesClient.subscribe(pendingIntent, ...); public class MyService extends IntentService { protected void onHandleIntent(Intent intent) { messagesClient.handleIntent(intent, new MessageListener() { @Override public void onFound(Message message) {...} } }); }
Parameters | |
---|---|
intent |
Intent : An intent that was sent as a result of subscribe(PendingIntent, SubscribeOptions) |
messageListener |
MessageListener : This will be called immediately with information present in the Intent
|
Publishes a message so that it is visible to nearby devices.
The message is only delivered to apps that share the same project id in the Developer Console and have an active subscription. Create project identifiers and turn on the Nearby API in the Google Developers Console.
Parameters | |
---|---|
message |
Message : A Message to publish for nearby devices to see |
options |
PublishOptions : A PublishOptions object for this operation |
Returns | |
---|---|
Task<Void> |
The Task which can be used to determine if the call succeeded
|
Registers a status callback, which will be notified when significant events occur that affect Nearby for your app.
When your app first calls this API, it may be immediately called back with current status.
Parameters | |
---|---|
statusCallback |
StatusCallback : A callback to notify when events occur |
Returns | |
---|---|
Task<Void> |
The Task which can be used to determine if the call succeeded |
See also:
Subscribes for published messages from nearby devices.
Only messages published by apps sharing the same project id in the Developer Console will be delivered.
Parameters | |
---|---|
listener |
MessageListener : A MessageListener implementation to get callbacks of received messages |
options |
SubscribeOptions : A SubscribeOptions object for this operation |
Returns | |
---|---|
Task<Void> |
The Task which can be used to determine if the call succeeded
|
Subscribes for published messages from nearby devices, using the default options in DEFAULT
.
Parameters | |
---|---|
listener |
MessageListener : A MessageListener implementation to get callbacks of received messages |
Returns | |
---|---|
Task<Void> |
Note: Currently, this method only finds messages attached to BLE beacons. See Beacons.
Subscribes for published messages from nearby devices in a persistent and low-power manner. This uses less battery, but will have higher latency and lower reliability. Notably, updates may only be delivered when the device's screen turns on (or when Bluetooth turns on). The subscription will be retained after the client disconnects.
Call handleIntent(Intent, MessageListener)
to handle the Intents received from
Nearby while subscribed. Call unsubscribe(PendingIntent)
to cancel the subscription.
Each application can pass up to 5 PendingIntent
s. When the limit is reached, this
returns TOO_MANY_PENDING_INTENTS
, and the subscription is not
created.
Parameters | |
---|---|
pendingIntent |
PendingIntent : A PendingIntent to get callbacks about nearby messages |
options |
SubscribeOptions : A SubscribeOptions object for this operation |
Returns | |
---|---|
Task<Void> |
The Task which can be used to determine if the call succeeded
|
Note: Currently, this method only finds messages attached to BLE beacons. See Beacons.
Subscribes for published messages from nearby devices in a persistent and low-power manner,
using the default options in DEFAULT
.
Parameters | |
---|---|
pendingIntent |
PendingIntent : A PendingIntent to get callbacks about nearby messages |
Returns | |
---|---|
Task<Void> |
Unregisters a status callback previously registered with registerStatusCallback(StatusCallback)
.
Parameters | |
---|---|
statusCallback |
StatusCallback : A callback previously registered with registerStatusCallback(StatusCallback) |
Returns | |
---|---|
Task<Void> |
The Task which can be used to determine if the call succeeded |
See also:
Cancels an existing subscription.
Parameters | |
---|---|
listener |
MessageListener : A MessageListener implementation that is currently subscribed |
Returns | |
---|---|
Task<Void> |
The Task which can be used to determine if the call succeeded
|
Cancels an existing subscription.
Parameters | |
---|---|
pendingIntent |
PendingIntent : A PendingIntent that is currently subscribed |
Returns | |
---|---|
Task<Void> |
The Task which can be used to determine if the call succeeded
|