取得快照資料

本節將說明如何使用 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");
        }
    })

取得附近的訊號 beacons

如要取得附近信標的相關資訊,請呼叫 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() 即可傳回訊號塔狀態。

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

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

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