消息类型
默认情况下,订阅会查找与应用的 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(例如,使用您自己的信标注册库),则可以这样做。目前支持两种格式:
- Eddystone UID。
- 使用
MessageFilter.Builder#includeEddystoneUids
查找这些内容。
- 使用
- iBeacon ID。
- 使用
MessageFilter.Builder#includeIBeaconIds
查找这些内容。
- 使用
示例:
// 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);
}
};