2017-03-16 46 views
1

注意

我在StackOverflow上找不到任何類似的問題。在我發現的唯一線索中,他們詢問有關閱讀,而不是寫作。Google Fit:保存醫學數據

問題

我整合Google健身,但我不能insert血壓數據到HistoryApi。我登錄成功,但添加數據的時候,我總是得到:

Status{statusCode=TIMEOUT, resolution=null}

我試圖把在AsyncTask的代碼,並與.await(1, TimeUnit.MINUTES)同步插入,但仍然得到同樣的錯誤。

我也嘗試卸載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

是否有代碼片段,關於如何使用Google健身爲READ血壓數據清楚地解釋了一個鏈接? –

+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)'並確保我在'manifest'中聲明瞭'''但沒有任何改變。仍然是'TIMEOUT'。 – JonZarate

0

按照該指南上Insert data

插入數據

將數據插入到健身的歷史,首先創建一個DataSet 實例:

// 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); 

您創建一個DataSet實例後,使用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