高级功能
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
消息类型
默认情况下,订阅会查找与应用的 Google Cloud 控制台项目关联的所有消息。包括:
- 同一应用在其他设备上发布的消息。
- 该项目拥有的消息,附加到信标。请参阅向信标添加附件。
您的应用可以使用 MessageFilter
订阅更多类型的附近消息,包括公共信标附件和原始蓝牙低功耗 (BLE) 信标 ID。
公开信标附件
开发者可以将其信标附件命名空间标记为 PUBLIC
。这样,所有应用都可以检索这些数据,无论其所属的 Cloud 控制台项目如何。如需了解如何将附件命名空间设为公开,请参阅附件公开范围。
示例:
// 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 信标 ID
您可以使用 Google 的信标平台将云端的任意数据附加到信标,从而抽象出在 BLE 数据包中通告的实际信标 ID。系统会默认发现这些附件(请参阅消息类型)。
不过,如果您确实需要发现原始信标 ID(例如,使用您自己的信标注册库),则可以。目前支持两种格式:
示例:
// 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 和距离回调
除了 found 和 lost 回调之外,当附近设备有与消息关联的 BLE 信号的新信息时,前台订阅还可以更新您的 MessageListener。
- 目前,这些额外的回调仅针对 BLE 信标消息(附件和信标 ID)传送。
- 这些额外的回调不会传送到后台 (
PendingIntent
) 订阅。
示例:
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);
}
};
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-08-29。
[null,null,["最后更新时间 (UTC):2025-08-29。"],[[["\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 };"]]