進階功能

訊息類型

根據預設,訂閱會找出與應用程式 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);
  }
};