เปิดใช้ AR เพื่อใช้ฟีเจอร์ Augmented Reality ในแอปใหม่หรือที่มีอยู่
กำหนดค่าแอปเป็น "ต้องใช้ AR" หรือ "ไม่บังคับให้ใช้ AR"
ฟีเจอร์ AR ทั้งหมดจะจัดเก็บไว้ในแอปชื่อ Google Play Services สำหรับ AR ซึ่ง Play Store จะอัปเดตแยกต่างหากเพื่อประหยัดพื้นที่ในอุปกรณ์แต่ละเครื่อง แอป Android ที่ใช้ฟีเจอร์ AR จะสื่อสารกับบริการ Google Play สำหรับ AR โดยใช้ ARCore SDK แอปที่รองรับฟีเจอร์ AR สามารถกําหนดค่าได้ 2 วิธี ได้แก่ AR ต้องระบุและ AR ไม่บังคับ การกำหนดนี้จะกำหนดวิธีที่แอปโต้ตอบกับแอปบริการ Google Play สำหรับ AR
แอปที่ต้องใช้ AR จะใช้งานไม่ได้หากไม่มี ARCore โดยต้องใช้อุปกรณ์ที่รองรับ ARCore ซึ่งติดตั้งบริการ Google Play สำหรับ AR แล้ว
- Google Play Store จะแสดงแอปที่ต้องใช้ AR บนอุปกรณ์ที่รองรับ ARCore เท่านั้น
- เมื่อผู้ใช้ติดตั้งแอปที่ต้องใช้ AR ทาง Google Play Store จะติดตั้งบริการ Google Play สำหรับ AR ในอุปกรณ์ของผู้ใช้โดยอัตโนมัติ อย่างไรก็ตาม แอปของคุณยังคงต้องทำการตรวจสอบรันไทม์เพิ่มเติมในกรณีที่บริการ Google Play สำหรับ AR เป็นเวอร์ชันเก่าหรือถูกถอนการติดตั้งด้วยตนเอง
แอปAR ไม่บังคับใช้ ARCore เพื่อเพิ่มประสิทธิภาพฟังก์ชันการทำงานที่มีอยู่ โดยจะมีฟีเจอร์ AR ที่ไม่บังคับซึ่งจะเปิดใช้งานในอุปกรณ์ที่รองรับ ARCore และติดตั้งบริการ Google Play สำหรับ AR ไว้เท่านั้น
- แอป AR ไม่บังคับจะติดตั้งและใช้งานในอุปกรณ์ที่ไม่รองรับ ARCore ได้
- เมื่อผู้ใช้ติดตั้งแอป AR ไม่บังคับ Google Play Store จะไม่ติดตั้งบริการ Google Play สำหรับ AR ในอุปกรณ์โดยอัตโนมัติ
ต้องระบุ AR | ไม่บังคับให้ใช้ AR | |
---|---|---|
การใช้ฟีเจอร์ AR | แอปของคุณต้องใช้ ARCore เพื่อฟังก์ชันพื้นฐาน | ARCore ช่วยเสริมฟังก์ชันการทำงานของแอป แอปของคุณจะทำงานได้โดยไม่ต้องรองรับ ARCore |
ระดับการเข้าถึง Play Store | แอปจะแสดงใน Play Store เฉพาะในอุปกรณ์ที่รองรับ ARCore | แอปของคุณเป็นไปตามกระบวนการแสดงข้อมูลปกติ |
วิธีติดตั้งบริการ Google Play สำหรับ AR | Play Store จะติดตั้งบริการ Google Play สำหรับ AR ควบคู่ไปกับแอปของคุณ | แอปของคุณใช้
ArCoreApk.requestInstall()
เพื่อดาวน์โหลดและติดตั้ง ARCore |
ข้อกำหนดของ Android minSdkVersion |
Android 7.0 (API ระดับ 24) | Android 4.4 (API ระดับ 19) แต่การเรียกใช้ฟังก์ชัน AR ใดๆ ต้องใช้ Android 7.0 เป็นอย่างน้อย (API ระดับ 24) |
ต้องใช้ ArCoreApk.checkAvailability() หรือ ArCoreApk.checkAvailabilityAsync() เพื่อตรวจสอบการรองรับและสถานะการติดตั้ง ARCore
|
||
ต้องใช้
ArCoreApk.requestInstall()
เพื่อติดตั้งบริการ Google Play สำหรับ AR |
หากต้องการทำให้แอปเป็น AR ที่จำเป็นหรือ AR ไม่บังคับ ให้อัปเดต AndroidManifest.xml
ให้รวมรายการต่อไปนี้
ต้องระบุ AR
<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>
ไม่บังคับให้ใช้ AR
<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>
จากนั้นแก้ไข build.gradle
ของแอปเพื่อระบุ minSdkVersion
เป็นอย่างน้อย 24
โดยทำดังนี้
android {
defaultConfig {
…
minSdkVersion 24
}
}
เพิ่มการพึ่งพิงบิวด์
หากต้องการเพิ่ม ARCore ลงในโปรเจ็กต์ Android Studio ให้ทําดังนี้
ตรวจสอบว่าไฟล์
build.gradle
ของโปรเจ็กต์มีที่เก็บ Maven ของ Googleallprojects { repositories { google() … } }
เพิ่มไลบรารี ARCore เวอร์ชันล่าสุดเป็น Dependency ในไฟล์
build.gradle
ของแอปdependencies { … implementation 'com.google.ar:core:1.33.0' }
ดำเนินการตรวจสอบรันไทม์
ในระหว่างรันไทม์ ให้ทำตามขั้นตอนต่อไปนี้เพื่อให้แน่ใจว่าฟีเจอร์ AR ในแอปทำงานได้อย่างราบรื่น
ตรวจสอบว่าอุปกรณ์รองรับ ARCore หรือไม่
ทั้งแอป AR ต้องระบุและแอป AR ไม่บังคับควรใช้ArCoreApk.checkAvailability()
หรือ ArCoreApk.checkAvailabilityAsync()
เพื่อระบุว่าอุปกรณ์ปัจจุบันรองรับ ARCore หรือไม่ ในอุปกรณ์ที่ไม่รองรับ ARCore แอปควรปิดใช้ฟังก์ชันการทำงานที่เกี่ยวข้องกับ AR และซ่อนองค์ประกอบ UI ที่เกี่ยวข้อง
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);
}
});
}
ArCoreApk.checkAvailability()
หรือ ArCoreApk.checkAvailabilityAsync()
เพื่อตรวจสอบการรองรับ ARCore จะช่วยให้ผู้ใช้ได้รับประสบการณ์การใช้งานที่สอดคล้องกัน
ArCoreApk.checkAvailability()
อาจต้องค้นหาทรัพยากรเครือข่ายเพื่อระบุว่าอุปกรณ์รองรับ ARCore หรือไม่ ในระหว่างนี้ ระบบจะแสดงผล UNKNOWN_CHECKING
หากต้องการลดเวลาในการตอบสนองที่รับรู้และการแสดงป๊อปอัป แอปควรเรียกใช้ ArCoreApk.checkAvailability()
1 ครั้งในช่วงต้นของวงจรเพื่อเริ่มการค้นหา โดยละเว้นค่าที่แสดงผล วิธีนี้จะทำให้ผลลัพธ์ที่แคชไว้พร้อมใช้งานทันทีเมื่อองค์ประกอบ UI สำหรับการเข้าสู่ AR อาจแสดง
ตรวจสอบว่าได้ติดตั้งบริการ Google Play สำหรับ AR แล้ว
ทั้งแอปที่ต้องใช้ AR และแอปที่ไม่ต้องใช้ AR ต้องใช้
ArCoreApk.requestInstall()
ก่อนสร้างเซสชัน ARCore เพื่อตรวจสอบว่า (ยังคง) มีการติดตั้ง Google Play Services สำหรับ AR เวอร์ชันที่เข้ากันได้อยู่หรือไม่ และเพื่อให้แน่ใจว่าได้ดาวน์โหลดข้อมูลโปรไฟล์อุปกรณ์ ARCore ที่จำเป็นทั้งหมดแล้ว
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.
}
…
}
ขอสิทธิ์เข้าถึงกล้อง
ทั้งแอป AR ไม่บังคับและแอป AR ต้องระบุสิทธิ์เข้าถึงกล้องก่อนที่จะสร้างเซสชัน AR
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;
}
…
}
กิจกรรม AR ของคุณต้องใช้ 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();
}
}
ปฏิบัติตามข้อกำหนดด้านความเป็นส่วนตัวของผู้ใช้
หากต้องการเผยแพร่แอปใน Play Store โปรดตรวจสอบว่าแอปของคุณเป็นไปตามข้อกําหนดด้านความเป็นส่วนตัวของผู้ใช้ของ ARCore
สิ่งที่จะเกิดขึ้นหลังจากนี้
- ดูวิธีกำหนดค่าเซสชัน ARCore