通过HistoryAPI读取插入Google Fit API的数据

时间:2017-11-15 13:59:16

标签: android google-fit

我正在开发Google Fit API应用程序,过去两天我一直试图存储和检索属于不同Android应用程序中的BPM数据类型的数据。同时检索它返回零点,虽然同时插入框架报告每个插入。我为这两个应用程序创建了OAuth密钥,每个应用程序都订阅了这种数据类型。我还使用enableServerQueries()进行检索。

数据源正在创建为

daataSource=new DataSource.Builder().setName("measurementsSource").setDataType(DataType.TYPE_HEART_RATE_BPM).
                            setAppPackageName(getApplicationContext())
                            .setType(TYPE_RAW).build();

有没有人有任何想法如何以适当的方式获取数据?

UPD。根据建议,我已经添加了完整的代码来添加我想要使用的1点自定义DataType。我已经尝试过很多次了,但它仍然说“好”和#39;并且没有可用于获取的数据(例如,使用API​​资源管理器)。



package test.com.cloudydiscoverer;

import android.content.Intent;
import android.content.IntentSender;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.Scopes;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.common.api.ResultCallback;
import com.google.android.gms.common.api.Scope;
import com.google.android.gms.common.api.Status;
import com.google.android.gms.fitness.Fitness;
import com.google.android.gms.fitness.data.DataPoint;
import com.google.android.gms.fitness.data.DataSet;
import com.google.android.gms.fitness.data.DataSource;
import com.google.android.gms.fitness.data.DataType;
import com.google.android.gms.fitness.data.Field;
import com.google.android.gms.fitness.request.DataTypeCreateRequest;
import com.google.android.gms.fitness.result.DataTypeResult;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import static android.content.ContentValues.TAG;
import static com.google.android.gms.fitness.data.DataSource.TYPE_RAW;

public class MainActivity extends AppCompatActivity {

    private static final int REQUEST_OAUTH = 1;
    private static boolean authInProgress = false;
    protected GoogleApiClient googleApiClient = null;
    private GoogleApiClient.ConnectionCallbacks connectionCallbacks = null;

    private void buildFitnessClient() {
        GoogleApiClient.Builder builder = new GoogleApiClient.Builder(this);

        builder.addApi(Fitness.RECORDING_API);
        builder.addApi(Fitness.SENSORS_API);
        builder.addApi(Fitness.BLE_API);
        builder.addApi(Fitness.SESSIONS_API);
        builder.addApi(Fitness.HISTORY_API);
        builder.addApi(Fitness.CONFIG_API);

        builder.addScope(new Scope(Scopes.FITNESS_BODY_READ_WRITE));            //TODO; verify scopes
        builder.addScope(new Scope(Scopes.FITNESS_ACTIVITY_READ_WRITE));
        builder.addScope(new Scope(Scopes.FITNESS_LOCATION_READ_WRITE));
        builder.addScope(new Scope(Scopes.FITNESS_BODY_READ_WRITE));

        googleApiClient = builder.build();
    }

    public boolean flag=false;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        connectionCallbacks = new GoogleApiClient.ConnectionCallbacks() {
            @Override
            public void onConnected(Bundle bundle) {

               /* try {
                    Thread.sleep(120000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
*/
                DataTypeCreateRequest request = new DataTypeCreateRequest.Builder()
                        .setName("test.com.cloudydiscoverer.cloudy_s")
                        .addField("value", Field.FORMAT_INT32)
                        .build();

                Fitness.ConfigApi.createCustomDataType(googleApiClient, request)
                        .setResultCallback(new ResultCallback<DataTypeResult>() {
                            @Override
                            public void onResult(@NonNull DataTypeResult dataTypeResult) {

                                if (!dataTypeResult.getStatus().isSuccess()) {
                                    Log.i(TAG, "DataType creation/retrieval failed with result: " + dataTypeResult.getStatus().getStatusMessage());
                                } else {

                                    Log.i(TAG, "created");

                                    DataSource wSource=new DataSource.Builder().setName(dataTypeResult.getDataType().getName()+".source").setDataType(DataType.TYPE_HEART_RATE_BPM).
                                            setType(TYPE_RAW).setAppPackageName("test.com.cloudydiscoverer").build();
                                  /*  DataSource wetSource = new DataSource.Builder()
                                            .setDataType(dataTypeResult.getDataType())
                                            .build();*/

                                    DataPoint b = DataPoint.create(wSource);
                                    b.setTimestamp(System.currentTimeMillis(), TimeUnit.MILLISECONDS);
                                   b.setFloatValues(5.0f);
                                    //b.getValue(dataTypeResult.getDataType().getFields().get(0)).setInt(42);

                                    DataSet dataSet = DataSet.create(wSource);
                                    dataSet.add(b);


                                    Fitness.HistoryApi.insertData(googleApiClient, dataSet).setResultCallback(new ResultCallback<Status>() {
                                                                                                                  @Override
                                                                                                                  public void onResult(@NonNull Status status) {
                                                                                                                      Log.i(TAG, String.valueOf(status.isSuccess()));
                                                                                                                  }
                                                                                                              }
                                    );


                                }
                            }
                        });
            }

            @Override
            public void onConnectionSuspended(int i) {
                if (i == GoogleApiClient.ConnectionCallbacks.CAUSE_NETWORK_LOST) {
                    Toast.makeText(MainActivity.this, "Network is unavailable. " +
                            "Connection will be restored automatically.", Toast.LENGTH_SHORT).show();
                } else if (i == GoogleApiClient.ConnectionCallbacks.CAUSE_SERVICE_DISCONNECTED) {
                    Toast.makeText(MainActivity.this, "Service has been disconnected. " +
                            "Connection will be restored automatically.", Toast.LENGTH_SHORT).show();
                }
            }
        };


        GoogleApiClient.OnConnectionFailedListener onConnectionFailedListener = new GoogleApiClient.OnConnectionFailedListener() {
            @Override
            public void onConnectionFailed(ConnectionResult result) {

                Log.i(TAG, "Connection failed with result: " + result.getErrorCode());

                if (!result.hasResolution()) {
                    return;
                }
                if (!authInProgress) {
                    try {
                        Toast.makeText(MainActivity.this, "You need to authorize.", Toast.LENGTH_SHORT).show();
                        authInProgress = true;
                        result.startResolutionForResult(MainActivity.this, REQUEST_OAUTH);
                    } catch (IntentSender.SendIntentException e) {
                        Toast.makeText(MainActivity.this, "Exception has been caught.", Toast.LENGTH_SHORT).show();
                    }
                }
            }
        };


        Log.i("1","1");
        buildFitnessClient();
        googleApiClient.registerConnectionCallbacks(connectionCallbacks);
        googleApiClient.registerConnectionFailedListener(onConnectionFailedListener);


        Log.i("2","2");
        googleApiClient.connect();

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {

        if (requestCode == REQUEST_OAUTH) {
            authInProgress = false;
            if (resultCode == RESULT_OK) {
                if ((!googleApiClient.isConnected()) || (!googleApiClient.isConnecting()))
                    googleApiClient.connect();
            } else {

                Log.i(TAG, "Nothing could be done.");
            }
        }
    }

}
&#13;
&#13;
&#13;

0 个答案:

没有答案