고급 기능
컬렉션을 사용해 정리하기
내 환경설정을 기준으로 콘텐츠를 저장하고 분류하세요.
메시지 유형
기본적으로 구독은 앱의 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);
}
};
달리 명시되지 않는 한 이 페이지의 콘텐츠에는 Creative Commons Attribution 4.0 라이선스에 따라 라이선스가 부여되며, 코드 샘플에는 Apache 2.0 라이선스에 따라 라이선스가 부여됩니다. 자세한 내용은 Google Developers 사이트 정책을 참조하세요. 자바는 Oracle 및/또는 Oracle 계열사의 등록 상표입니다.
최종 업데이트: 2025-08-29(UTC)
[null,null,["최종 업데이트: 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 };"]]