ฟีเจอร์ขั้นสูง

ประเภทข้อความ

โดยค่าเริ่มต้น การสมัครใช้บริการจะค้นหาข้อความทั้งหมดที่เชื่อมโยงกับโปรเจ็กต์ Google Cloud Console ของแอป ซึ่งรวมถึง

  • ข้อความที่แอปเดียวกันเผยแพร่ในอุปกรณ์อื่น
  • ข้อความที่เป็นของโปรเจ็กต์นั้นซึ่งแนบอยู่กับบีคอน โปรดดู เพิ่มไฟล์แนบในบีคอน

แอปของคุณสามารถใช้ MessageFilter เพื่อสมัครรับข้อความใกล้เคียงประเภทอื่นๆ รวมถึงไฟล์แนบบีคอนสาธารณะและรหัสบีคอนบลูทูธพลังงานต่ำ (BLE) ดิบได้

ไฟล์แนบบีคอนสาธารณะ

นักพัฒนาซอฟต์แวร์สามารถทำเครื่องหมายเนมสเปซของไฟล์แนบบีคอนเป็น PUBLIC ได้ การดำเนินการนี้จะอนุญาตให้แอปทั้งหมดดึงข้อมูลได้ โดยไม่คำนึงถึงโปรเจ็กต์ Cloud Console สำหรับข้อมูลเกี่ยวกับวิธีทำให้เนมสเปซของไฟล์แนบเป็นแบบสาธารณะ โปรดดูการเปิดเผยไฟล์แนบ

เช่น

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

คุณใช้แพลตฟอร์มบีคอนของ Google เพื่อแนบข้อมูลที่กำหนดเองในระบบคลาวด์กับบีคอนได้ โดยลบรหัสบีคอนจริงที่โฆษณาในแพ็กเก็ต BLE ระบบจะแสดงไฟล์แนบเหล่านี้โดยค่าเริ่มต้น (ดูประเภทข้อความ)

แต่หากจำเป็นต้องค้นหารหัสบีคอนดิบ (เช่น เพื่อใช้รีจิสทรีบีคอนของคุณเอง) คุณก็สามารถทำได้ ขณะนี้มีรูปแบบที่รองรับอยู่ 2 รูปแบบ ดังนี้

เช่น

// 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 และ Callback ระยะทาง

นอกจากการพบและการเรียกกลับที่หายไปแล้ว การสมัครใช้บริการที่ทำงานอยู่เบื้องหน้ายังสามารถอัปเดต MessageListener เมื่อ Nearby มีข้อมูลใหม่เกี่ยวกับสัญญาณ BLE ที่เชื่อมโยงกับข้อความได้ด้วย

  • ปัจจุบันระบบจะส่ง Callback เพิ่มเติมเหล่านี้สำหรับข้อความบีคอน BLE เท่านั้น (ทั้งไฟล์แนบและรหัสบีคอน)
  • จะไม่มีการส่ง Callback เพิ่มเติมเหล่านี้ไปยังการสมัครใช้บริการ ในเบื้องหลัง (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);
  }
};