Tính năng nâng cao
Sử dụng bộ sưu tập để sắp xếp ngăn nắp các trang
Lưu và phân loại nội dung dựa trên lựa chọn ưu tiên của bạn.
Loại thông báo
Theo mặc định, một gói thuê bao sẽ tìm thấy tất cả thông báo liên kết với dự án Google Cloud Console của ứng dụng. Những thông tin này bao gồm:
- Tin nhắn do cùng một ứng dụng phát hành trên một thiết bị khác.
- Thông báo do dự án đó sở hữu, được đính kèm vào beacon. Xem phần Thêm tệp đính kèm vào beacon.
Ứng dụng của bạn có thể sử dụng MessageFilter
để đăng ký nhận nhiều loại thông báo lân cận hơn, bao gồm cả tệp đính kèm beacon công khai và mã nhận dạng beacon Bluetooth năng lượng thấp (BLE) thô.
Tệp đính kèm beacon công khai
Nhà phát triển có thể đánh dấu không gian tên tệp đính kèm của beacon là PUBLIC
. Điều này cho phép tất cả ứng dụng truy xuất các tệp này, bất kể Dự án Cloud Console của ứng dụng. Để biết thông tin về cách đặt không gian tên tệp đính kèm ở chế độ công khai, hãy xem phần Chế độ hiển thị tệp đính kèm.
Ví dụ:
// 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);
Mã beacon BLE
Bạn có thể sử dụng nền tảng beacon của Google để đính kèm dữ liệu tuỳ ý trên đám mây vào beacon, tóm tắt các mã beacon thực tế được quảng cáo trong gói BLE. Theo mặc định, các tệp đính kèm này sẽ được phát hiện (xem phần Loại thư).
Tuy nhiên, nếu cần khám phá mã nhận dạng beacon thô (ví dụ: để sử dụng sổ đăng ký beacon của riêng bạn), bạn có thể làm như vậy. Hiện có hai định dạng được hỗ trợ:
Ví dụ:
// 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);
Lệnh gọi lại RSSI và khoảng cách
Ngoài lệnh gọi lại tìm thấy và bị mất, gói thuê bao trên nền trước có thể cập nhật MessageListener khi tính năng Nearby có thông tin mới về tín hiệu BLE liên kết với một thông báo.
- Các lệnh gọi lại bổ sung này hiện chỉ được phân phối cho thông báo beacon BLE (cả tệp đính kèm và mã beacon).
- Các lệnh gọi lại bổ sung này không được phân phối đến các gói thuê bao ở chế độ nền (
PendingIntent
).
Ví dụ:
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);
}
};
Trừ phi có lưu ý khác, nội dung của trang này được cấp phép theo Giấy phép ghi nhận tác giả 4.0 của Creative Commons và các mẫu mã lập trình được cấp phép theo Giấy phép Apache 2.0. Để biết thông tin chi tiết, vui lòng tham khảo Chính sách trang web của Google Developers. Java là nhãn hiệu đã đăng ký của Oracle và/hoặc các đơn vị liên kết với Oracle.
Cập nhật lần gần đây nhất: 2025-08-29 UTC.
[null,null,["Cập nhật lần gần đây nhất: 2025-08-29 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 };"]]