Bật tính năng thực tế tăng cường (AR) trong ứng dụng Android

Bật tính năng Thực tế tăng cường để sử dụng các tính năng thực tế tăng cường trong ứng dụng mới hoặc ứng dụng hiện có của bạn.

Định cấu hình ứng dụng thành Bắt buộc thực tế tăng cường (AR) hoặc Thực tế tăng cường (AR) không bắt buộc

Để tiết kiệm không gian lưu trữ trên từng thiết bị, tất cả các tính năng thực tế tăng cường đều được lưu trữ trong một ứng dụng có tên là Dịch vụ Google Play cho Thực tế tăng cường. Ứng dụng này được Cửa hàng Play cập nhật riêng. Ứng dụng Android sử dụng các tính năng thực tế tăng cường giao tiếp với Dịch vụ Google Play cho Thực tế tăng cường bằng SDK ARCore. Bạn có thể định cấu hình cho một ứng dụng có hỗ trợ các tính năng thực tế tăng cường theo 2 cách: Cần có ARKhông bắt buộc sử dụng AR. Chỉ định này xác định cách ứng dụng tương tác với ứng dụng Dịch vụ Google Play cho Thực tế tăng cường.

Ứng dụng cần có AR không thể hoạt động mà không có ARCore. Giải pháp này yêu cầu một thiết bị hỗ trợ ARCore và đã cài đặt Dịch vụ Google Play cho Thực tế tăng cường.

  • Cửa hàng Google Play sẽ chỉ cung cấp các ứng dụng Yêu cầu thực tế tăng cường (AR) trên thiết bị hỗ trợ ARCore.
  • Khi người dùng cài đặt một ứng dụng Yêu cầu thực tế tăng cường (AR), Cửa hàng Google Play sẽ tự động cài đặt Dịch vụ Google Play cho Thực tế tăng cường trên thiết bị của họ. Tuy nhiên, ứng dụng của bạn vẫn phải thực hiện các bước kiểm tra bổ sung trong thời gian chạy phòng trường hợp Dịch vụ Google Play cho Thực tế tăng cường đã lỗi thời hoặc đã bị gỡ cài đặt theo cách thủ công.

Ứng dụng AR không bắt buộc dùng ARCore để cải thiện chức năng hiện có. Phiên bản này có các tính năng thực tế tăng cường (AR) không bắt buộc. Các tính năng này chỉ được kích hoạt trên những thiết bị hỗ trợ ARCore và đã cài đặt Dịch vụ Google Play cho Thực tế tăng cường.

  • Bạn có thể cài đặt và chạy các ứng dụng không bắt buộc thực tế tăng cường (AR) trên thiết bị không hỗ trợ ARCore.
  • Khi người dùng cài đặt một ứng dụng Thực tế tăng cường (AR), Cửa hàng Google Play sẽ không tự động cài đặt Dịch vụ Google Play cho Thực tế tăng cường trên thiết bị.
Yêu cầu thực tế tăng cườngThực tế tăng cường (AR) không bắt buộc
Mức sử dụng Tính năng thực tế tăng cường Ứng dụng của bạn cần có ARCore cho chức năng cơ bản. ARCore tăng cường chức năng cho ứng dụng của bạn. Ứng dụng của bạn có thể chạy mà không cần hỗ trợ ARCore.
Chế độ hiển thị trên Cửa hàng Play Ứng dụng của bạn chỉ có trong Cửa hàng Play trên các thiết bị hỗ trợ ARCore. Ứng dụng của bạn tuân theo quy trình thông thường đối với trang thông tin.
Phương thức cài đặt Dịch vụ Google Play cho Thực tế tăng cường Cửa hàng Play sẽ cài đặt Dịch vụ Google Play cho Thực tế tăng cường cùng với ứng dụng của bạn. Ứng dụng của bạn dùng ArCoreApk.requestInstall() tải xuống và cài đặt ARCore.
Yêu cầu về minSdkVersion của Android Android 7.0 (API cấp 24) Android 4.4 (API cấp 19), mặc dù việc chạy mọi chức năng thực tế tăng cường đều yêu cầu Android 7.0 (API cấp 24) trở lên
Phải sử dụng ArCoreApk.checkAvailability() hoặc ArCoreApk.checkAvailabilityAsync() để kiểm tra khả năng hỗ trợ và trạng thái cài đặt ARCore
Phải sử dụng ArCoreApk.requestInstall() để cài đặt Dịch vụ Google Play cho Thực tế tăng cường

Để yêu cầu thực tế tăng cường (AR) hoặc Thực tế tăng cường (AR) không bắt buộc, hãy cập nhật AndroidManifest.xml để thêm các mục sau:

Yêu cầu thực tế tăng cường

<uses-permission android:name="android.permission.CAMERA" />

<!-- Limits app visibility in the Google Play Store to ARCore supported devices
     (https://developers.google.com/ar/devices). -->
<uses-feature android:name="android.hardware.camera.ar" />

<application …>
    …

    <!-- "AR Required" app, requires "Google Play Services for AR" (ARCore)
         to be installed, as the app does not include any non-AR features. -->
    <meta-data android:name="com.google.ar.core" android:value="required" />
</application>

Thực tế tăng cường (AR) không bắt buộc

<uses-permission android:name="android.permission.CAMERA" />

<!-- If your app was previously AR Required, don't forget to remove the
     `<uses-feature android:name="android.hardware.camera.ar" />` entry, as
     this would limit app visibility in the Google Play Store to only
     ARCore supported devices. -->

<application …>
    …

    <!-- "AR Optional" app, contains non-AR features that can be used when
         "Google Play Services for AR" (ARCore) is not available. -->
    <meta-data android:name="com.google.ar.core" android:value="optional" />
</application>

Sau đó, hãy sửa đổi build.gradle của ứng dụng để chỉ định minSdkVersion tối thiểu là 24:

 android {
     defaultConfig {
         …
         minSdkVersion 24
     }
 }

Thêm phần phụ thuộc của bản dựng

Để thêm ARCore vào dự án Android Studio, hãy làm như sau:

  1. Đảm bảo tệp build.gradle của dự án có chứa kho lưu trữ Maven của Google.

    allprojects {
        repositories {
            google()
            …
        }
    }
    
  2. Thêm thư viện ARCore mới nhất làm phần phụ thuộc trong tệp build.gradle của ứng dụng.

    dependencies {
        …
        implementation 'com.google.ar:core:1.33.0'
    }
    

Thực hiện kiểm tra thời gian chạy

Trong thời gian chạy, hãy thực hiện những bước sau để đảm bảo các tính năng thực tế tăng cường trên ứng dụng hoạt động trơn tru.

Kiểm tra xem ARCore có được hỗ trợ hay không

Cả ứng dụng yêu cầu AR và AR không bắt buộc đều phải sử dụng ArCoreApk.checkAvailability() hoặc ArCoreApk.checkAvailabilityAsync() để xác định xem thiết bị hiện tại có hỗ trợ ARCore hay không. Trên các thiết bị không hỗ trợ ARCore, ứng dụng nên tắt chức năng liên quan đến AR và ẩn các phần tử giao diện người dùng được liên kết.

Kotlin

override fun onCreate(savedInstanceState: Bundle?) {
  super.onCreate(savedInstanceState)

  // Enable AR-related functionality on ARCore supported devices only.
  maybeEnableArButton()
  …
}

fun maybeEnableArButton() {
  ArCoreApk.getInstance().checkAvailabilityAsync(this) { availability ->
    if (availability.isSupported) {
      mArButton.visibility = View.VISIBLE
      mArButton.isEnabled = true
    } else { // The device is unsupported or unknown.
      mArButton.visibility = View.INVISIBLE
      mArButton.isEnabled = false
    }
  }
}

Java

@Override
protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);

  // Enable AR-related functionality on ARCore supported devices only.
  maybeEnableArButton();
  …
}

void maybeEnableArButton() {
  ArCoreApk.getInstance().checkAvailabilityAsync(this, availability -> {
    if (availability.isSupported()) {
      mArButton.setVisibility(View.VISIBLE);
      mArButton.setEnabled(true);
    } else { // The device is unsupported or unknown.
      mArButton.setVisibility(View.INVISIBLE);
      mArButton.setEnabled(false);
    }
  });
}
Mặc dù Dịch vụ Google Play cho Thực tế tăng cường được cài đặt cùng với ứng dụng Yêu cầu thực tế tăng cường của bạn, nhưng người dùng có thiết bị không được hỗ trợ có thể cài đặt ứng dụng này từ một nguồn bên ngoài. Việc sử dụng ArCoreApk.checkAvailability() hoặc ArCoreApk.checkAvailabilityAsync() để kiểm tra xem có hỗ trợ ARCore hay không sẽ giúp đảm bảo trải nghiệm nhất quán.

ArCoreApk.checkAvailability() có thể cần truy vấn tài nguyên mạng để xác định xem thiết bị có hỗ trợ ARCore hay không. Trong thời gian này, máy chủ sẽ trả về UNKNOWN_CHECKING. Để giảm độ trễ dự kiến và cửa sổ bật lên, ứng dụng nên gọi ArCoreApk.checkAvailability() ngay từ đầu vòng đời để bắt đầu truy vấn, bỏ qua giá trị được trả về. Bằng cách này, kết quả được lưu vào bộ nhớ đệm sẽ có ngay khi thành phần giao diện người dùng đang nhập AR có thể hiển thị.

Kiểm tra xem Dịch vụ Google Play cho Thực tế tăng cường đã được cài đặt hay chưa

Cả ứng dụng yêu cầu AR và AR không bắt buộc đều phải sử dụng ArCoreApk.requestInstall() trước khi tạo một phiên ARCore để kiểm tra xem phiên bản Dịch vụ Google Play cho Thực tế tăng cường tương thích đã được cài đặt chưa (vẫn) được cài đặt hay chưa và để đảm bảo rằng tất cả dữ liệu hồ sơ thiết bị ARCore bắt buộc đã được tải xuống.

Kotlin

// requestInstall(Activity, true) will triggers installation of
// Google Play Services for AR if necessary.
var mUserRequestedInstall = true

override fun onResume() {
  super.onResume()

  // Check camera permission.
  …

  // Ensure that Google Play Services for AR and ARCore device profile data are
  // installed and up to date.
  try {
    if (mSession == null) {
      when (ArCoreApk.getInstance().requestInstall(this, mUserRequestedInstall)) {
        ArCoreApk.InstallStatus.INSTALLED -> {
          // Success: Safe to create the AR session.
          mSession = Session(this)
        }
        ArCoreApk.InstallStatus.INSTALL_REQUESTED -> {
          // When this method returns `INSTALL_REQUESTED`:
          // 1. ARCore pauses this activity.
          // 2. ARCore prompts the user to install or update Google Play
          //    Services for AR (market://details?id=com.google.ar.core).
          // 3. ARCore downloads the latest device profile data.
          // 4. ARCore resumes this activity. The next invocation of
          //    requestInstall() will either return `INSTALLED` or throw an
          //    exception if the installation or update did not succeed.
          mUserRequestedInstall = false
          return
        }
      }
    }
  } catch (e: UnavailableUserDeclinedInstallationException) {
    // Display an appropriate message to the user and return gracefully.
    Toast.makeText(this, "TODO: handle exception " + e, Toast.LENGTH_LONG)
        .show()
    return
  } catch (…) {
    …
    return  // mSession remains null, since session creation has failed.
  }
  …
}

Java

// requestInstall(Activity, true) will trigger installation of
// Google Play Services for AR if necessary.
private boolean mUserRequestedInstall = true;

@Override
protected void onResume() {
  super.onResume();

  // Check camera permission.
  …

  // Ensure that Google Play Services for AR and ARCore device profile data are
  // installed and up to date.
  try {
    if (mSession == null) {
      switch (ArCoreApk.getInstance().requestInstall(this, mUserRequestedInstall)) {
        case INSTALLED:
          // Success: Safe to create the AR session.
          mSession = new Session(this);
          break;
        case INSTALL_REQUESTED:
          // When this method returns `INSTALL_REQUESTED`:
          // 1. ARCore pauses this activity.
          // 2. ARCore prompts the user to install or update Google Play
          //    Services for AR (market://details?id=com.google.ar.core).
          // 3. ARCore downloads the latest device profile data.
          // 4. ARCore resumes this activity. The next invocation of
          //    requestInstall() will either return `INSTALLED` or throw an
          //    exception if the installation or update did not succeed.
          mUserRequestedInstall = false;
          return;
      }
    }
  } catch (UnavailableUserDeclinedInstallationException e) {
    // Display an appropriate message to the user and return gracefully.
    Toast.makeText(this, "TODO: handle exception " + e, Toast.LENGTH_LONG)
        .show();
    return;
  } catch (…) {
    …
    return;  // mSession remains null, since session creation has failed.
  }
  …
}

Yêu cầu quyền truy cập vào camera

Cả ứng dụng AR không bắt buộc và ứng dụng Thực tế tăng cường đều phải đảm bảo rằng bạn đã cấp quyền truy cập vào máy ảnh trước khi tạo Phiên thực tế tăng cường.

Kotlin

override fun onResume() {
  super.onResume()

  // ARCore requires camera permission to operate.
  if (!CameraPermissionHelper.hasCameraPermission(this)) {
    CameraPermissionHelper.requestCameraPermission(this)
    return
  }

  …
}

Java

@Override
protected void onResume() {
  super.onResume();

  // ARCore requires camera permission to operate.
  if (!CameraPermissionHelper.hasCameraPermission(this)) {
    CameraPermissionHelper.requestCameraPermission(this);
    return;
  }

  …
}

Hoạt động thực tế tăng cường của bạn cũng phải triển khai onRequestPermissionsResult().

Kotlin

override fun onRequestPermissionsResult(
  requestCode: Int,
  permissions: Array<String>,
  results: IntArray
) {
  super.onRequestPermissionsResult(requestCode, permissions, results)
  if (!CameraPermissionHelper.hasCameraPermission(this)) {
    Toast.makeText(this, "Camera permission is needed to run this application", Toast.LENGTH_LONG)
      .show()
    if (!CameraPermissionHelper.shouldShowRequestPermissionRationale(this)) {
      // Permission denied with checking "Do not ask again".
      CameraPermissionHelper.launchPermissionSettings(this)
    }
    finish()
  }
}

Java

@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] results) {
  super.onRequestPermissionsResult(requestCode, permissions, results);
  if (!CameraPermissionHelper.hasCameraPermission(this)) {
    Toast.makeText(this, "Camera permission is needed to run this application", Toast.LENGTH_LONG)
        .show();
    if (!CameraPermissionHelper.shouldShowRequestPermissionRationale(this)) {
      // Permission denied with checking "Do not ask again".
      CameraPermissionHelper.launchPermissionSettings(this);
    }
    finish();
  }
}

Tuân thủ các yêu cầu về quyền riêng tư của người dùng

Để xuất bản ứng dụng lên Cửa hàng Play, hãy đảm bảo ứng dụng của bạn tuân thủ chính sách của ARCore Yêu cầu về quyền riêng tư của người dùng.

Bước tiếp theo