Fitur Lanjutan

Jenis pesan

Secara default, langganan menemukan semua pesan yang terkait dengan project Konsol Google Cloud aplikasi Anda. Ini mencakup:

  • Pesan yang dipublikasikan oleh aplikasi yang sama di perangkat lain.
  • Pesan milik project tersebut, yang dilampirkan ke beacon. Lihat Menambahkan Lampiran ke Beacon.

Aplikasi Anda dapat menggunakan MessageFilter untuk berlangganan lebih banyak jenis pesan di sekitar, termasuk suar publik lampiran, dan ID beacon Bluetooth Hemat Energi (BLE) mentah.

Lampiran beacon publik

Developer dapat menandai namespace lampiran beacon-nya sebagai PUBLIC. Hal ini memungkinkan semua aplikasi untuk mengambilnya, terlepas dari Project Konsol Cloud mereka. Sebagai informasi tentang cara membuat namespace lampiran menjadi publik, lihat Visibilitas lampiran.

Contoh:

// 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);

ID beacon BLE

Anda dapat menggunakan platform beacon Google untuk melampirkan data arbitrer di cloud ke beacon, mengabstraksi ID beacon sebenarnya yang diiklankan di BLE paket. Lampiran ini ditemukan secara default (lihat Jenis pesan).

Akan tetapi, jika Anda perlu menemukan ID beacon mentah (untuk menggunakan beacon Anda sendiri {i>registry<i}), Anda bisa melakukannya. Saat ini ada dua format yang didukung:

Contoh:

// 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 dan callback jarak

Selain callback yang ditemukan dan hilang, langganan latar depan dapat memperbarui MessageListener Anda ketika Nearby memiliki informasi baru tentang sinyal BLE yang terkait dengan pesan.

  • Callback tambahan ini saat ini hanya dikirimkan untuk pesan beacon BLE (lampiran dan ID beacon).
  • Callback tambahan ini tidak dikirim ke latar belakang (PendingIntent) langganan.

Contoh:

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);
  }
};