Fitness getSensorsClient无法读取TYPE_ACTIVITY_SEGMENT实时数据

时间:2019-10-28 01:55:18

标签: android google-api google-fit google-fit-sdk fitness

目标-获取活动的连续调用(例如-步行,跑步等数据),读取活动的真实数据。

我只需更改为TYPE_ACTIVITY_SEGMENT,即可读取以下相同代码的TYPE_LOCATION_SAMPLE实时数据。我无法读取诸如步行,跑步等活动的真实数据。 示例-https://developers.google.com/fit/rest/v1/reference/activity-types [Google适合-使用getSensorsClient阅读]

阅读示例-步行* 7 步行(健身)93

关注-https://developers.google.com/fit/android/sensors

OnDataPointListener mListener = dataPoint -> {
        // this call back i want every time - i walk or move as per firebase documentation
        Util.showToastLong(context, " ACTIVITY CONTINUOUS data point---- dataType " + dataType + " " + dataPoint);
    };

我的观察-我可以获取位置数据类型的OnDataPointListener回调,但是同一示例不适用于活动数据。

我想要的输出- 从代码C块- registerFitnessDataListener-方法-OnDataPointListener-必须在代码编写的5秒钟内回传continiouse回调 但这不起作用,为什么不运行我不知道请帮忙

代码- A。 以下是登录选项-我已成功登录-

public static FitnessOptions getFitnessSignInOptions() {
    return FitnessOptions.builder()
            .addDataType(DataType.TYPE_ACTIVITY_SEGMENT, FitnessOptions.ACCESS_WRITE)
            .build();    }

B。

  public void readSensorsActivity(Context context) {
        Timber.d("readSensorsActivity ");
        GoogleSignInAccount lastSignedInAccount = GoogleSignIn.getLastSignedInAccount(context);
        if (lastSignedInAccount == null) {
            return;
        }

    Fitness.getSensorsClient(context, lastSignedInAccount)
            .findDataSources(
                    new DataSourcesRequest.Builder()
                            .setDataTypes(DataType.TYPE_ACTIVITY_SEGMENT)
                            .setDataSourceTypes(DataSource.TYPE_DERIVED)
                            .build())
            .addOnSuccessListener(
                    dataSources -> {
                        Timber.d("activity data---" + dataSources);
                        DataSource ds = new DataSource.Builder()
                                .setAppPackageName(context.getPackageName())
                                .setDataType(DataType.TYPE_ACTIVITY_SEGMENT)
                                .setType(DataSource.TYPE_RAW)
                                .build();

                        Util.showToastLong(context, " ACTIVITY DATA----" + dataSources);
                        registerFitnessDataListener(context, ds, DataType.TYPE_ACTIVITY_SEGMENT);
                    })
            .addOnFailureListener(
                    e -> {
                        e.printStackTrace();
                        Timber.d("failed" + e);
                    });
}

C。 侦听器代码-

private static void registerFitnessDataListener(Context context, DataSource dataSource, DataType dataType) {
    OnDataPointListener mListener = dataPoint -> {
        Util.showToastLong(context, " ACTIVITY CONTINUOUS data point---- dataType " + dataType + " " + dataPoint);
        for (Field field : dataPoint.getDataType().getFields()) {
            Value val = dataPoint.getValue(field);
            Util.showToastLong(context, " register continuous---- dataType " + dataType + " " + val);
            Timber.d("Detected DataPoint field: " + field.getName());
            Timber.d("Detected DataPoint value: " + val);
        }
    };

    Fitness.getSensorsClient(context, GoogleSignIn.getLastSignedInAccount(context))
            .add(
                    new SensorRequest.Builder()
                            .setDataSource(dataSource) // Optional but recommended for custom data sets.
                            .setDataType(dataType) // Can't be omitted.
                            .setSamplingRate(5, TimeUnit.SECONDS)
                            .build(),
                    mListener)
            .addOnCompleteListener(
                    task -> {
                        Util.showToastLong(context, " REGISTER ACTIVITY listener---- dataType " + dataType + " " + task);

                        if (task.isSuccessful()) {
                            Timber.d("Listener registered!");
                        } else {
                            Timber.d("Listener not registered." + task.getException());
                        }
                    });
}

注意要达到相同的结果-其他方法-

Google Fitness API,无法读取Android传感器数据-使用Fitness Sensor客户端-使用android手机/设备类型活动细分,OnDataPointListener

目标-无法获取TYPE_ACTIVITY_SEGMENT实时数据。

以下-https://developers.google.com/android/reference/com/google/android/gms/fitness/SensorsClient.html

这很好,我可以将下面的相同代码读取计数步长类型增量,只需将TYPE_STEP_COUNT_DELTA更改为TYPE_ACTIVITY_SEGMENT。但这不适用于TYPE_ACTIVITY_SEGMENT。

需要的输出- 从代码2块-OnDataPointListener-必须像代码中所写的那样在1秒钟内发出有争议的回调。

OnDataPointListener myStepCountListener = dataPoint -> {

但这不起作用,为什么不运行我不知道请帮忙

代码-1。 以下是登录选项-我已成功登录-

public static FitnessOptions getFitnessSignInOptions() {
return FitnessOptions.builder()
        .addDataType(DataType.TYPE_ACTIVITY_SEGMENT, FitnessOptions.ACCESS_WRITE)
        .build();    }

代码-2

public void readSensorActivity(Context context) {
    GoogleSignInAccount googleSignInAccount = GoogleSignIn.getLastSignedInAccount(context);
    if (googleSignInAccount == null) {
        return;
    }

    OnDataPointListener myStepCountListener = dataPoint -> {
        Util.showToastLong(context, dataPoint.toString());
        Timber.d(dataPoint.toString());
        Util.showToastLong(context, " CONTINUOUS data point----  " + " " + dataPoint);
        for (Field field : dataPoint.getDataType().getFields()) {
            Value val = dataPoint.getValue(field);
            Util.showToastLong(context, " register continuous----  " + " " + val);
            Timber.d("Detected DataPoint field: " + field.getName());
            Timber.d("Detected DataPoint value: " + val);
        }

    };
    Fitness.getSensorsClient(context, googleSignInAccount)
            .add(
                    new SensorRequest.Builder()
                            .setDataType(DataType.TYPE_ACTIVITY_SEGMENT)
                            .setSamplingRate(1, TimeUnit.SECONDS)  // sample once per minute
                            .build(),
                    myStepCountListener).
            addOnFailureListener(
                    e -> {
                        e.printStackTrace();
                        Timber.d("failed" + e);
                    })
            .addOnCompleteListener(
                    task -> {
                        Util.showToastLong(context, " REGISTER  listener----  " + " " + task);
                        if (task.isSuccessful()) {
                            Timber.d("Listener registered!");
                        } else {
                            Timber.d("Listener not registered." + task.getException());
                        }
                    });
}

0 个答案:

没有答案