Advanced Features
Stay organized with collections
Save and categorize content based on your preferences.
Message types
By default, a subscription finds all messages associated with the
app's Google Cloud Console project. This
includes:
- Messages published by the same app on another device.
- Messages owned by that project, attached to beacons. See
Add Attachments to Beacons.
Your app can use a MessageFilter
to subscribe for more types of nearby messages, including public beacon
attachments, and raw Bluetooth Low Energy (BLE) beacon IDs.
Public beacon attachments
A developer can mark their beacon attachment namespace as PUBLIC
. This allows
all apps to retrieve them, regardless of their Cloud Console Project. For
information on how to make attachment namespaces public, see
Attachment visibility.
Example:
// Subscribe for two different public beacon attachment types.
MessageFilter messageFilter = new MessageFilter.Builder()
.includeNamespacedType(EXAMPLE_PUBLIC_NAMESPACE_A, EXAMPLE_PUBLIC_TYPE_A)
.includeNamespacedType(EXAMPLE_PUBLIC_NAMESPACE_B, EXAMPLE_PUBLIC_TYPE_B)
.build();
SubscribeOptions options = new SubscribeOptions.Builder()
.setStrategy(Strategy.BLE_ONLY)
.setFilter(messageFilter)
.build();
MessageListener messageListener = new MessageListener() {
@Override
public void onFound(final Message message) {
// We may want to handle the two types of message differently.
if (EXAMPLE_PUBLIC_NAMESPACE_A.equals(message.getNamespace())
&& EXAMPLE_PUBLIC_TYPE_A.equals(message.getType())) {
// Handle a "type A" message.
} else if (EXAMPLE_PUBLIC_NAMESPACE_B.equals(message.getNamespace())
&& EXAMPLE_PUBLIC_TYPE_B.equals(message.getType())) {
// Handle a "type B" message.
}
}
};
Nearby.getMessagesClient(this).subscribe(messageListener, options);
BLE beacon IDs
You can use Google's beacon platform to attach arbitrary data in the cloud to
your beacons, abstracting away the actual beacon IDs that are advertised in BLE
packets. These attachments are discovered by default (see
Message types).
However, if you do need to discover raw beacon IDs (to use your own beacon
registry, for example), you can. There are currently two supported formats:
Example:
// Subscribe for all Eddystone UIDs whose first 10 bytes (the "namespace")
// match MY_EDDYSTONE_UID_NAMESPACE.
//
// Note that the Eddystone UID namespace is separate from the namespace
// field of a Nearby Message.
MessageFilter messageFilter = new MessageFilter.Builder()
.includeEddystoneUids(MY_EDDYSTONE_UID_NAMESPACE, null /* any instance */)
.build();
SubscribeOptions options = new SubscribeOptions.Builder()
.setStrategy(Strategy.BLE_ONLY)
.setFilter(messageFilter)
.build();
MessageListener messageListener = new MessageListener() {
@Override
public void onFound(final Message message) {
// Note: Checking the type shown for completeness, but is unnecessary
// if your message filter only includes a single type.
if (Message.MESSAGE_NAMESPACE_RESERVED.equals(message.getNamespace())
&& Message.MESSAGE_TYPE_EDDYSTONE_UID.equals(message.getType())) {
// Nearby provides the EddystoneUid class to parse Eddystone UIDs
// that have been found nearby.
EddystoneUid eddystoneUid = EddystoneUid.from(message);
Log.i(TAG, "Found Eddystone UID: " + eddystoneUid);
}
}
};
Nearby.getMessagesClient(this).subscribe(messageListener, options);
RSSI and distance callbacks
In addition to found and lost callbacks, a foreground subscription can update
your MessageListener
when Nearby has new information about the BLE signal associated with a message.
- These extra callbacks are currently only delivered for BLE beacon messages
(both attachments and beacon IDs).
- These extra callbacks are not delivered to background (
PendingIntent
)
subscriptions.
Example:
MessageListener messageListener = new MessageListener() {
/**
* Called when a message is discovered nearby.
*/
@Override
public void onFound(final Message message) {
Log.i(TAG, "Found message: " + message);
}
/**
* Called when the Bluetooth Low Energy (BLE) signal associated with a message changes.
*
* This is currently only called for BLE beacon messages.
*
* For example, this is called when we see the first BLE advertisement
* frame associated with a message; or when we see subsequent frames with
* significantly different received signal strength indicator (RSSI)
* readings.
*
* For more information, see the MessageListener Javadocs.
*/
@Override
public void onBleSignalChanged(final Message message, final BleSignal bleSignal) {
Log.i(TAG, "Message: " + message + " has new BLE signal information: " + bleSignal);
}
/**
* Called when Nearby's estimate of the distance to a message changes.
*
* This is currently only called for BLE beacon messages.
*
* For more information, see the MessageListener Javadocs.
*/
@Override
public void onDistanceChanged(final Message message, final Distance distance) {
Log.i(TAG, "Distance changed, message: " + message + ", new distance: " + distance);
}
/**
* Called when a message is no longer detectable nearby.
*/
@Override
public void onLost(final Message message) {
Log.i(TAG, "Lost message: " + message);
}
};
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2025-08-28 UTC.
[null,null,["Last updated 2025-08-28 UTC."],[[["\u003cp\u003eBy default, subscriptions encompass messages published within the app's Google Cloud project, including those from other devices and beacon-attached messages.\u003c/p\u003e\n"],["\u003cp\u003eUtilize \u003ccode\u003eMessageFilter\u003c/code\u003e to subscribe to specific nearby message types like public beacon attachments and raw BLE beacon IDs, including Eddystone UIDs and iBeacon IDs.\u003c/p\u003e\n"],["\u003cp\u003ePublic beacon attachments, marked with the \u003ccode\u003ePUBLIC\u003c/code\u003e namespace, are accessible to all apps for retrieval.\u003c/p\u003e\n"],["\u003cp\u003eForeground subscriptions provide RSSI and distance callbacks for BLE beacon messages, offering insights into signal strength and proximity.\u003c/p\u003e\n"]]],["Subscriptions by default find messages associated with the app's project, including those from the same app on other devices and project-owned beacon attachments. `MessageFilter` can expand this to include public beacon attachments and raw BLE beacon IDs, including Eddystone and iBeacon formats. Foreground subscriptions can receive updates on BLE signal changes and distance to messages. The code examples show how to subscribe to different public types, filter by Eddystone UIDs, and define actions when messages are found, have their BLE signal changed, their distance changed or are lost.\n"],null,["# Advanced Features\n\nMessage types\n-------------\n\nBy default, a subscription finds all messages associated with the\napp's [Google Cloud Console](https://console.cloud.google.com/) project. This\nincludes:\n\n- Messages published by the same app on another device.\n- Messages owned by that project, attached to beacons. See [Add Attachments to Beacons](/beacons/proximity/attachments).\n\nYour app can use a [`MessageFilter`](/android/reference/com/google/android/gms/nearby/messages/MessageFilter)\nto subscribe for more types of nearby messages, including public beacon\nattachments, and raw Bluetooth Low Energy (BLE) beacon IDs.\n\n### Public beacon attachments\n\nA developer can mark their beacon attachment namespace as `PUBLIC`. This allows\nall apps to retrieve them, regardless of their Cloud Console Project. For\ninformation on how to make attachment namespaces public, see\n[Attachment visibility](/beacons/proximity/attachments#attachment_visibility).\n\nExample: \n\n // Subscribe for two different public beacon attachment types.\n MessageFilter messageFilter = new MessageFilter.Builder()\n .includeNamespacedType(EXAMPLE_PUBLIC_NAMESPACE_A, EXAMPLE_PUBLIC_TYPE_A)\n .includeNamespacedType(EXAMPLE_PUBLIC_NAMESPACE_B, EXAMPLE_PUBLIC_TYPE_B)\n .build();\n SubscribeOptions options = new SubscribeOptions.Builder()\n .setStrategy(Strategy.BLE_ONLY)\n .setFilter(messageFilter)\n .build();\n\n MessageListener messageListener = new MessageListener() {\n @Override\n public void onFound(final Message message) {\n // We may want to handle the two types of message differently.\n if (EXAMPLE_PUBLIC_NAMESPACE_A.equals(message.getNamespace())\n && EXAMPLE_PUBLIC_TYPE_A.equals(message.getType())) {\n // Handle a \"type A\" message.\n } else if (EXAMPLE_PUBLIC_NAMESPACE_B.equals(message.getNamespace())\n && EXAMPLE_PUBLIC_TYPE_B.equals(message.getType())) {\n // Handle a \"type B\" message.\n }\n }\n };\n\n Nearby.getMessagesClient(this).subscribe(messageListener, options);\n\n### BLE beacon IDs\n\nYou can use Google's beacon platform to attach arbitrary data in the cloud to\nyour beacons, abstracting away the actual beacon IDs that are advertised in BLE\npackets. These attachments are discovered by default (see\n[Message types](#message_types)).\n\nHowever, if you do need to discover raw beacon IDs (to use your own beacon\nregistry, for example), you can. There are currently two supported formats:\n\n- [Eddystone UIDs](https://github.com/google/eddystone/tree/master/eddystone-uid).\n - Find these with [`MessageFilter.Builder#includeEddystoneUids`](https://developers.google.com/android/reference/com/google/android/gms/nearby/messages/MessageFilter.Builder.html#includeEddystoneUids(java.lang.String,%20java.lang.String)).\n- iBeacon IDs.\n - Find these with [`MessageFilter.Builder#includeIBeaconIds`](https://developers.google.com/android/reference/com/google/android/gms/nearby/messages/MessageFilter.Builder.html#includeIBeaconIds(java.util.UUID,%20java.lang.Short,%20java.lang.Short)).\n\nExample: \n\n // Subscribe for all Eddystone UIDs whose first 10 bytes (the \"namespace\")\n // match MY_EDDYSTONE_UID_NAMESPACE.\n //\n // Note that the Eddystone UID namespace is separate from the namespace\n // field of a Nearby Message.\n MessageFilter messageFilter = new MessageFilter.Builder()\n .includeEddystoneUids(MY_EDDYSTONE_UID_NAMESPACE, null /* any instance */)\n .build();\n SubscribeOptions options = new SubscribeOptions.Builder()\n .setStrategy(Strategy.BLE_ONLY)\n .setFilter(messageFilter)\n .build();\n\n MessageListener messageListener = new MessageListener() {\n @Override\n public void onFound(final Message message) {\n // Note: Checking the type shown for completeness, but is unnecessary\n // if your message filter only includes a single type.\n if (Message.MESSAGE_NAMESPACE_RESERVED.equals(message.getNamespace())\n && Message.MESSAGE_TYPE_EDDYSTONE_UID.equals(message.getType())) {\n // Nearby provides the EddystoneUid class to parse Eddystone UIDs\n // that have been found nearby.\n EddystoneUid eddystoneUid = EddystoneUid.from(message);\n Log.i(TAG, \"Found Eddystone UID: \" + eddystoneUid);\n }\n }\n };\n\n Nearby.getMessagesClient(this).subscribe(messageListener, options);\n\nRSSI and distance callbacks\n---------------------------\n\nIn addition to found and lost callbacks, a foreground subscription can update\nyour [MessageListener](/android/reference/com/google/android/gms/nearby/messages/MessageListener)\nwhen Nearby has new information about the BLE signal associated with a message.\n| **Note:**\n\n- These extra callbacks are currently only delivered for BLE beacon messages (both attachments and [beacon IDs](#ble_beacon_ids)).\n- These extra callbacks are not delivered to background (`PendingIntent`) subscriptions.\n\nExample: \n\n MessageListener messageListener = new MessageListener() {\n /**\n * Called when a message is discovered nearby.\n */\n @Override\n public void onFound(final Message message) {\n Log.i(TAG, \"Found message: \" + message);\n }\n\n /**\n * Called when the Bluetooth Low Energy (BLE) signal associated with a message changes.\n *\n * This is currently only called for BLE beacon messages.\n *\n * For example, this is called when we see the first BLE advertisement\n * frame associated with a message; or when we see subsequent frames with\n * significantly different received signal strength indicator (RSSI)\n * readings.\n *\n * For more information, see the MessageListener Javadocs.\n */\n @Override\n public void onBleSignalChanged(final Message message, final BleSignal bleSignal) {\n Log.i(TAG, \"Message: \" + message + \" has new BLE signal information: \" + bleSignal);\n }\n\n /**\n * Called when Nearby's estimate of the distance to a message changes.\n *\n * This is currently only called for BLE beacon messages.\n *\n * For more information, see the MessageListener Javadocs.\n */\n @Override\n public void onDistanceChanged(final Message message, final Distance distance) {\n Log.i(TAG, \"Distance changed, message: \" + message + \", new distance: \" + distance);\n }\n\n /**\n * Called when a message is no longer detectable nearby.\n */\n @Override\n public void onLost(final Message message) {\n Log.i(TAG, \"Lost message: \" + message);\n }\n };"]]