取得快照資料

本節說明如何使用 Snapshot API 取得各支援內容類型的目前狀態。詳情請參閱「開始使用」一文。如要瞭解已淘汰的比對內容信號,請開啟下列可展開的通知:

取得目前的活動

如要取得使用者的目前活動,請呼叫 getDetectedActivity(),這會傳回 ActivityRecognitionResult,其中包含使用者最近活動的相關資訊。

getDetectedActivity() 方法需要 com.google.android.gms.permission.ACTIVITY_RECOGNITION 權限。將這項權限新增至 AndroidManifest.xml

如要取得使用者的目前活動,請按照下列步驟操作:

  1. 呼叫 getSnapshotClient() 建立 SnapshotClient 的執行個體。
  2. 使用 addOnSuccessListener 建立可監聽 DetectedActivityResponseOnSuccessListener
  3. 請致電 getStatus(),確認結果有效。
  4. 呼叫 DetectedActivityResponse.getActivityRecognitionResult() 傳回 ActivityRecognitionResult。您可以使用這項功能取得使用者目前活動的許多層面。例如:

下列程式碼範例使用 getMostProbableActivity() 取得最有可能偵測到的活動,並將結果記錄到控制台:

Awareness.getSnapshotClient(this).getDetectedActivity()
    .addOnSuccessListener(new OnSuccessListener<DetectedActivityResponse>() {
        @Override
        public void onSuccess(DetectedActivityResponse dar) {
            ActivityRecognitionResult arr = dar.getActivityRecognitionResult();
            DetectedActivity probableActivity = arr.getMostProbableActivity();

            int confidence = probableActivity.getConfidence();
            String activityStr = probableActivity.toString();
            mLogFragment.getLogView().println("Activity: " + activityStr
                + ", Confidence: " + confidence + "/100");
        }
    })

取得附近的 Beacon

如要取得附近信標的相關資訊,請呼叫 getBeaconState()。 信標資料包含任何附件的內容、類型和命名空間。

getBeaconState() 方法需要 android.permission.ACCESS_FINE_LOCATION 權限。將這項權限新增至 AndroidManifest.xml。 此外,您必須為 Google Developers Console 專案啟用 Nearby Messages API。詳情請參閱「註冊和 API 金鑰」和「開始使用」。

如要取得附近信標的資訊,請按照下列步驟操作:

  1. 檢查使用者是否已授予必要權限,以下範例會檢查是否已授予 android.permission.ACCESS_FINE_LOCATION 權限。如果沒有,系統會提示使用者提供同意聲明。

    if (ContextCompat.checkSelfPermission(
                MainActivity.this,
                Manifest.permission.ACCESS_FINE_LOCATION) !=
                PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(
                    MainActivity.this,
                    new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
                    MY_PERMISSION_LOCATION
            );
            return;
        }
    
  2. 定義 BeaconState.TypeFilter。 這項作業只會傳回已向指定命名空間和類型註冊的信標。您也可以根據附件內容的逐位元組比對結果進行篩選。以下範例說明如何建立類型篩選器:

    private static final List<BeaconState.TypeFilter> BEACON_TYPE_FILTERS = Arrays.asList(
            BeaconState.TypeFilter.with(
                "my.beacon.namespace",
                "my-attachment-type"),
            BeaconState.TypeFilter.with(
                "my.other.namespace",
                "my-attachment-type"));
    
  3. 呼叫 getSnapshotClient.getBeaconState()

  4. 使用 addOnSuccessListener 建立可監聽 BeaconStateResponseOnSuccessListener

  5. 請致電 getStatus(),確認結果有效。

  6. 呼叫 BeaconStateResponse.getBeaconState() 即可傳回 Beacon 狀態。

  7. 呼叫 BeaconState.getBeaconInfo() 取得 BeaconState.BeaconInfo

以下範例說明如何取得 Beacon 資訊:

Awareness.getSnapshotClient(this).getBeaconState(BEACON_TYPE_FILTERS)
    .addOnSuccessListener(new OnSuccessListener<BeaconStateResponse>() {
        @Override
        public void onSuccess(BeaconStateResponse beaconStateResponse) {
            BeaconStateResult beaconStateResult = beaconStateResponse.getBeaconState();
            BeaconState.BeaconInfo beaconInfo = beaconStateResponse.getBeaconInfo();
        }
    })

取得耳機狀態

如要偵測耳機是否已插入裝置,請呼叫 getHeadphoneState(),這會建立 HeadphoneStateResponse 偵測狀態,並將 OnSuccessListener 設為偵測。 接著,您可以呼叫 getHeadphoneState() 來取得 HeadphoneState

如要取得耳機的目前狀態,請執行下列步驟:

  1. 呼叫 getSnapshotClient.getHeadphoneState()
  2. 使用 addOnSuccessListener 建立可監聽 HeadphoneStateResponseOnSuccessListener
  3. 請致電 getStatus(),確認結果有效。
  4. 成功後,請呼叫 HeadphoneStateResponse.getHeadphoneState() 傳回耳機狀態。這個值為 PLUGGED_INUNPLUGGED

以下程式碼範例說明如何使用 getHeadphoneState()

Awareness.getSnapshotClient(this).getHeadphoneState()
    .addOnSuccessListener(new OnSuccessListener<HeadphoneStateResponse>() {
        @Override
        public void onSuccess(HeadphoneStateResponse headphoneStateResponse) {
            HeadphoneState headphoneState = headphoneStateResponse.getHeadphoneState();
            boolean pluggedIn = headphoneState.getState() == HeadphoneState.PLUGGED_IN;
            String stateStr =
                "Headphones are " + (pluggedIn ? "plugged in" : "unplugged");
            mLogFragment.getLogView().println(stateStr);
        }
    })
    .addOnFailureListener(new OnFailureListener() {
        @Override
        public void onFailure(@NonNull Exception e) {
            Log.e(TAG, "Could not get headphone state: " + e);
        }
    });

取得位置

您可以呼叫 getLocation() 取得使用者目前的位置 (經緯度),這會傳回 LocationResponse。接著,您可以呼叫 LocationResponse.getLocation(),取得包含目前位置資料的 Location

getLocation() 方法需要 android.permission.ACCESS_FINE_LOCATION 權限。將這項權限新增至 AndroidManifest.xml

如要取得目前位置,請按照下列步驟操作:

  1. 檢查使用者是否已授予必要權限,下列範例會檢查是否已授予 android.permission.ACCESS_FINE_LOCATION 權限。如果沒有,系統會提示使用者提供同意聲明。

    
    if (ContextCompat.checkSelfPermission(
                MainActivity.this,
                Manifest.permission.ACCESS_FINE_LOCATION) !=
                PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(
                    MainActivity.this,
                    new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
                    MY_PERMISSION_LOCATION
            );
            return;
        }
    
  2. 呼叫 getSnapshotClient.getLocation()

  3. 使用 addOnSuccessListener 建立可監聽 LocationResponseOnSuccessListener

  4. 請致電 getStatus(),確認結果有效。

  5. 呼叫 LocationResponse.getLocation() 以傳回目前的 Location

以下範例說明如何取得目前位置:

Awareness.getSnapshotClient(this).getLocation()
    .addOnSuccessListener(new OnSuccessListener<LocationResponse>() {
        @Override
        public void onSuccess(LocationResponse locationResponse) {
            Location loc = locationResponse.getLocationResult();
        }
    })