问题
Objective - Get continuous call back of activity (like - walking, running, etc. data) , read the real data for activities.
I can read real time data for TYPE_LOCATION_SAMPLE for same code as below just changing to TYPE_ACTIVITY_SEGMENT. i can not read the real data for activities like - walking, running, etc. Example - https://developers.google.com/fit/rest/v1/reference/activity-types [Google fit - Read using getSensorsClient]
Read Example - Walking* 7 Walking (fitness) 93
Following - 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);
};
My Observation - I can get OnDataPointListener call back for Location Data type but same sample not working for activity data.
Output i want - From Code C block - registerFitnessDataListener - method - OnDataPointListener - must give continiouse call back within 5 seconds as wrote in code But this is not working, why not running i dont know please help
Code - A. Below is the sign in option - i sign in successfully-
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. Listener Code -
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());
}
});
}
Note to achieve the same result - other approach -
Google fitness API, Android sensor data can not read - using Fitness Sensor client - using android phone/device-type activity segment, OnDataPointListener
Objective - Unable to get TYPE_ACTIVITY_SEGMENT real time data.
Following - https://developers.google.com/android/reference/com/google/android/gms/fitness/SensorsClient.html
this is working well, i can read count step type delta for same code as below just changing TYPE_STEP_COUNT_DELTA to TYPE_ACTIVITY_SEGMENT. But this is not working for TYPE_ACTIVITY_SEGMENT.
Output needed - From Code 2 block - OnDataPointListener - must give contentious call back within 1 seconds as wrote in code.
OnDataPointListener myStepCountListener = dataPoint -> {
But this is not working, why not running i dont know please help
Code - 1. Below is the sign in option - i sign in successfully-
public static FitnessOptions getFitnessSignInOptions() {
return FitnessOptions.builder()
.addDataType(DataType.TYPE_ACTIVITY_SEGMENT, FitnessOptions.ACCESS_WRITE)
.build(); }
Code - 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());
}
});
}
来源:https://stackoverflow.com/questions/58584957/fitness-getsensorsclient-can-not-read-type-activity-segment-real-time-data