高级功能

消息类型

默认情况下,订阅会查找与 应用的 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 和距离回调

除了找到的和丢失的回调之外,前台订阅还可以更新 您的 MessageListener 当“附近”功能具有与消息关联的 BLE 信号的新信息时。

  • 这些额外的回调目前仅针对 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);
  }
};