進階功能
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
訊息類型
根據預設,訂閱會找出與應用程式 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 和距離回呼
除了發現和遺失回呼之外,當 Nearby 有與訊息相關的 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);
}
};
除非另有註明,否則本頁面中的內容是採用創用 CC 姓名標示 4.0 授權,程式碼範例則為阿帕契 2.0 授權。詳情請參閱《Google Developers 網站政策》。Java 是 Oracle 和/或其關聯企業的註冊商標。
上次更新時間:2025-08-29 (世界標準時間)。
[null,null,["上次更新時間: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 };"]]