Android的Google Fit API

问题描述 投票:1回答:1

Google Fit and Flutter

我正在遵循Google的以下文档:Google Fit

我不理解第5步。我不是Android开发人员,也不知道代码在哪里。 Step 5

例如,它说:您必须先连接到以下一个或多个以下API客户端(它们是Google Play服务的一部分,然后才能从Google Fit API调用方法:

  • 传感器客户端
  • 记录客户端
  • 历史客户
  • 会话客户端
  • 目标客户
  • BLE客户端
  • 配置客户端

现在,我尝试连接第一个SensorsClient

但是我不知道代码在哪里,哪个文件?我是一名开发人员,在步骤5中需要​​帮助。

build.grandle

dependencies {
    implementation 'com.google.android.gms:play-services-fitness:18.0.0'
    implementation 'com.google.android.gms:play-services-auth:17.0.0'
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test:runner:1.1.1'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'
}
android flutter google-fit
1个回答
0
投票

您应该使用Flutter插件fitKit来读取健康和健身数据。在iOS上包装HealthKit,在Android上包装GoogleFit。

    import 'package:fit_kit/fit_kit.dart';

void read() async {
  final results = await FitKit.read(
    DataType.HEART_RATE,
    dateFrom: DateTime.now().subtract(Duration(days: 5)),
    dateTo: DateTime.now(),
  );
}

void readLast() async {
  final result = await FitKit.readLast(DataType.HEIGHT);
}

void readAll() async {
  if (await FitKit.requestPermissions(DataType.values)) {
    for (DataType type in DataType.values) {
      final results = await FitKit.read(
        type,
        dateFrom: DateTime.now().subtract(Duration(days: 5)),
        dateTo: DateTime.now(),
      );
    }
  }
}

如果您正在编写特定于平台的自定义代码

在您可以从Google Fit API调用方法之前,您必须连接到以下一个或多个以下API客户端,它们是Google Play服务的一部分:

  1. Sensors Client:客户端,用于在本地和连接的设备中公开不同的健身数据源,并将实时事件传递给听众。
  2. Recording Client:此客户端可将低功耗,始终在线的传感器数据后台收集到Google Fit存储中。
  3. History Client:用于在Google Fit中插入,删除和读取数据的客户端。
  4. Sessions Client:用于在Google Fit中创建和管理用户活动的会话的客户端。
  5. Goals Client:用于读取用户在Google Fit中创建的健身目标的客户端。 BLE客户端
  6. Config Client:用于访问Google Fit中的自定义数据类型和设置的客户端。

您必须连接以上其中之一。

    DataReadRequest readRequest = new DataReadRequest.Builder()
                .aggregate(DataType.TYPE_STEP_COUNT_DELTA, DataType.AGGREGATE_STEP_COUNT_DELTA)
                .setTimeRange(startTime, endTime, TimeUnit.MILLISECONDS)
                .bucketByTime(1, TimeUnit.DAYS)
                .enableServerQueries()
                .build();
// History Client 
        Fitness.getHistoryClient(this, GoogleSignIn.getLastSignedInAccount(this))
                .readData(readRequest)
                .addOnSuccessListener(new OnSuccessListener<DataReadResponse>() {
                    @Override
                    public void onSuccess(DataReadResponse dataReadResponse) {
                        Log.d(TAG, "onSuccess()");
                    }
                })
                .addOnFailureListener(new OnFailureListener() {
                    @Override
                    public void onFailure(@NonNull Exception e) {
                        Log.e(TAG, "onFailure()", e);
                    }
                })
                .addOnCompleteListener(new OnCompleteListener<DataReadResponse>() {
                    @Override
                    public void onComplete(@NonNull Task<DataReadResponse> task) {
                        Log.d(TAG, "onComplete()");
                    }
                });
© www.soinside.com 2019 - 2024. All rights reserved.