2017-03-16 15 views
1

私はStackOverflowで類似の質問を見つけることができませんでした。私が見つけた唯一のスレッドでは、彼らは書くことではなく、読むことについて尋ねました。Google Fit:医療データを保存

問題

私はGoogleFitを統合していますが、私はHistoryApiinsert血圧データにはできませんよ。私はAsyncTaskでコードを入れて.await(1, TimeUnit.MINUTES)と同期して挿入するが、それでも同じエラーを取得しようとした

Status{statusCode=TIMEOUT, resolution=null}

:私は正常にログインしますが、データを追加するとき、私はいつも得ます。

また、GoogleFitをアンインストールしてみましたが、私はWiFi経由でインターネットにアクセスしています。

役立つ場合はS Healthが問題なく動作しています。

コード

public static void saveBloodPressure(Context context, long timestampMillis, int systolic, int diastolic){ 

    // Create DataSource 
    DataSource bloodPressureSource = new DataSource.Builder() 
      .setDataType(HealthDataTypes.TYPE_BLOOD_PRESSURE) 
      .setAppPackageName(context) 
      .setStreamName(TAG + " - blood pressure") 
      .setType(DataSource.TYPE_RAW) 
      .build(); 

    // Create DataPoint with DataSource 
    DataPoint bloodPressure = DataPoint.create(bloodPressureSource); 
    bloodPressure.setTimestamp(timestampMillis, TimeUnit.MILLISECONDS); 
    bloodPressure.getValue(HealthFields.FIELD_BLOOD_PRESSURE_SYSTOLIC).setFloat(systolic); 
    bloodPressure.getValue(HealthFields.FIELD_BLOOD_PRESSURE_DIASTOLIC).setFloat(diastolic); 

    // Create DataSet 
    DataSet dataSet = DataSet.create(bloodPressureSource); 
    dataSet.add(bloodPressure); 

    // Create Callback to manage Result 
    ResultCallback<com.google.android.gms.common.api.Status> callback = new ResultCallback<com.google.android.gms.common.api.Status>() { 
     @Override 
     public void onResult(@NonNull com.google.android.gms.common.api.Status status) { 

      if (status.isSuccess()) { 
       Log.v("GoogleFit", "Success: " + status); 
      }else{ 
       Log.v("GoogleFit", "Error: " + status); 
      } 
     } 
    }; 

    // Execute insert 
    Fitness.HistoryApi.insertData(mGoogleApiClient, dataSet) 
      .setResultCallback(callback, 1, TimeUnit.MINUTES); 
} 

はケースで誰かが、私はまた、以下のGoogleApiClient初期化を入れます、要求します。

GoogleApiClient初期

public static void initialize(final FragmentActivity activity){ 

    // Setup Callback listener 
    GoogleApiClient.ConnectionCallbacks connectionCallbacks = new GoogleApiClient.ConnectionCallbacks() { 
     @Override 
     public void onConnected(Bundle bundle) { 
      Log.i(TAG, "Connected! "); 
      // Now you can make calls to the Fitness APIs. 
      //subscribe(); 
     } 

     @Override 
     public void onConnectionSuspended(int i) { 
      // If your connection to the sensor gets lost at some point, 
      // you'll be able to determine the reason and react to it here. 
      if (i == GoogleApiClient.ConnectionCallbacks.CAUSE_NETWORK_LOST) { 
       Log.i(TAG, "1 Connection lost. Cause: Network Lost."); 
      } else if (i == GoogleApiClient.ConnectionCallbacks.CAUSE_SERVICE_DISCONNECTED) { 
       Log.i(TAG, "2 Connection lost. Reason: Service Disconnected"); 
      } 
     } 
    }; 

    // Handle Failed connection 
    GoogleApiClient.OnConnectionFailedListener connectionFailed = new GoogleApiClient.OnConnectionFailedListener() { 
     @Override 
     public void onConnectionFailed(@NonNull ConnectionResult result) { 

      Log.i(TAG, "3 Google Play services connection failed. Cause: " + result.toString()); 

      Toast.makeText(activity, "4 Exception while connecting to Google Play services: " + 
        result.getErrorMessage() + ":" + result.getErrorCode(), Toast.LENGTH_SHORT).show(); 

     } 
    }; 

    // Create Google Api Client 
    mGoogleApiClient = new GoogleApiClient.Builder(activity) 
      .addConnectionCallbacks(connectionCallbacks) 
      .enableAutoManage(activity, connectionFailed) 
      .addScope(new Scope(Scopes.FITNESS_BODY_READ_WRITE)) 
      .addApi(Fitness.HISTORY_API) 
      .build(); 
} 

ありがとう!

+1

GoogleFitを使用して血圧データを読み取る方法について明確に説明するコードスニペットへのリンクがありますか? –

+1

あなたが解決策を見つけたら答えを投稿できますか?私は同じ問題を抱えています。ありがとうございました –

答えて

0

接続タイムアウトエラーのように見えますが、何か不足しているようです。

FITNESS_BODY_READ_WRITEスコープには、この機能が役立つかどうかわかりませんが、アクセス許可が必要です。

Fitness.HistoryApi.insertDataを呼び出す前にFitness APIで認証していますか?

どのユーザーがデータを挿入していますか?

こちらをご覧ください:https://developers.google.com/android/guides/permissions

そして、ここで(認可):https://developers.google.com/android/reference/com/google/android/gms/fitness/Fitness

+0

リンクをチェックして、私はすでに 'ACCESS_FINE_LOCATION'権限を持っていると言います。ただし、ユーザーアカウントについてはわかりません。私は 'setAccountName(String)'や 'useDefaultAccount()'を思い出すことができないので、月曜日にチェックアウトして報告します。ありがとう。 – JonZarate

+0

私は 'setAccountName(String)'を追加し、私の 'マニフェスト'で ''を宣言しましたが、何も変わりませんでした。それでも同じ「タイムアウト」。 – JonZarate

0

、フィットネス履歴にデータを挿入するにはInsert data

挿入データ

のガイドに従ってください最初にデータセット を作成します。インスタンス:

// Set a start and end time for our data, using a start time of 1 hour before this moment. 
Calendar cal = Calendar.getInstance(); 
Date now = new Date(); 
cal.setTime(now); 
long endTime = cal.getTimeInMillis(); 
cal.add(Calendar.HOUR_OF_DAY, -1); 
long startTime = cal.getTimeInMillis(); 

// Create a data source 
DataSource dataSource = new DataSource.Builder() 
     .setAppPackageName(this) 
     .setDataType(DataType.TYPE_STEP_COUNT_DELTA) 
     .setStreamName(TAG + " - step count") 
     .setType(DataSource.TYPE_RAW) 
     .build(); 

// Create a data set 
int stepCountDelta = 950; 
DataSet dataSet = DataSet.create(dataSource); 
// For each data point, specify a start time, end time, and the data value -- in this case, 
// the number of new steps. 
DataPoint dataPoint = dataSet.createDataPoint() 
     .setTimeInterval(startTime, endTime, TimeUnit.MILLISECONDS); 
dataPoint.getValue(Field.FIELD_STEPS).setInt(stepCountDelta); 
dataSet.add(dataPoint); 

あなたがデータセットのインスタンスを作成した後、HistoryApi.insertData メソッドを使用して 挿入の状態を確認するためにコールバックメソッドを同期的に待つか提供しています。

// Then, invoke the History API to insert the data and await the result, which is // possible here because of the {@link AsyncTask}. Always include a timeout when calling // await() to prevent hanging that can occur from the service being shutdown because // of low memory or other conditions. Log.i(TAG, "Inserting the dataset in the History API."); com.google.android.gms.common.api.Status insertStatus 
= 
     Fitness.HistoryApi.insertData(mClient, dataSet) 
       .await(1, TimeUnit.MINUTES); 

// Before querying the data, check to see if the insertion succeeded. if (!insertStatus.isSuccess()) { 
    Log.i(TAG, "There was a problem inserting the dataset."); 
    return null; } 

// At this point, the data has been inserted and can be read. Log.i(TAG, "Data insert was successful!"); 
+1

このコードは、Googleが 'GitHub'で提供した' HistoryApiSample'のコードです。私はすでにそれを持っており、私はそれらを数回比較しました。相違点がある場合は、それらを指摘してください。コピー/ペーストする例は、IMOにはあまり貢献していません。 – JonZarate

関連する問題